C#异常处理表、类、SQL
表SQL
/****** Object: Table [dbo].[IError] Script Date: 09/05/2012 17:00:41 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO SET ANSI_PADDING OFF
GO CREATE TABLE [dbo].[IError](
[ErrorModuleID] [varchar](500) NOT NULL,
[ErrorClassName] [varchar](50) NULL,
[ErrorMethodName] [varchar](50) NULL,
[ErrorMessage] [varchar](1000) NOT NULL,
[ErrorSource] [varchar](1000) NULL,
[ErrorStackTrace] [varchar](1000) NULL,
[ErrorTargetSite] [varchar](1000) NULL,
[ErrorSQL] [varchar](8000) NULL,
[subCode] [varchar](20) NULL,
[subName] [varchar](20) NULL,
[subMachine] [varchar](20) NULL,
[subTime] [datetime] NULL
) ON [PRIMARY] GO SET ANSI_PADDING OFF
GO
保存异常信息到IError存储过程:
/*
功 能 :保存异常信息到IError
涉 及 表 :IError
*/
ALTER PROCEDURE [dbo].[pt_ErrorSaveError]
@ErrorModuleID varchar(500),
@ErrorClassName varchar(50),
@ErrorMethodName varchar(50),
@ErrorMessage varchar(1000),
@ErrorSource varchar(1000),
@ErrorStackTrace varchar(1000),
@ErrorTargetsite varchar(1000),
@subCode varchar(50),
@subName varchar(50),
@subMachine varchar(50),
@ErrorSQL varchar(4000)
AS
declare @subTime datetime
set @subTime = getdate()
insert into IError (ErrorModuleID,ErrorClassName,ErrorMethodName,ErrorMessage,ErrorSource,ErrorStackTrace,ErrorTargetsite,ErrorSQL,subCode,subName,subMachine,subTime)
values (@ErrorModuleID,@ErrorClassName,@ErrorMethodName,@ErrorMessage,@ErrorSource,@ErrorStackTrace,@ErrorTargetsite,@ErrorSQL,@subCode,@subName,@subMachine,@subTime)
SaveError 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO; namespace Common
{
public partial class SaveError
{
private string strConnection = "Data Source=127.0.0.1;Initial Catalog=SysAdmin;Persist Security Info=True;User ID=sa;Password=123"; //连接字符串
private SqlConnection conn; //数据库连接
private SqlCommand comm; //SqlCommand //打开连接
public bool OpenConn()
{
bool bResult = false; if (this.conn == null)
{
StringBuilder strBd = new StringBuilder();
strBd.Append("时间: " + System.DateTime.Now.ToString() + System.Environment.NewLine);
strBd.Append("Message: 数据库连接失败,SqlConnection为空" + System.Environment.NewLine);
this.SaveErrorMessageToFile(strBd.ToString());
bResult = false;
}
else
{
if (this.conn.State != System.Data.ConnectionState.Open)
{
if (this.conn.ConnectionString.Length == 0)
{
this.conn.ConnectionString = this.strConnection;
}
try
{
this.conn.Open();
bResult = true;
}
catch (Exception)
{
bResult = false;
}
}
else
{
bResult = true;
}
} return bResult;
} //释放关闭连接
public void DisposeConn()
{
if (this.conn != null && this.conn.State == System.Data.ConnectionState.Open)
{
this.conn.Close();
this.conn = null;
}
} /// <summary>
/// 获取登陆用户信息,保存到错误日志中
/// </summary>
/// <param name="UserCode"></param>
/// <param name="UserName"></param>
/// <returns></returns>
public bool GetUserInfo(out string UserCode, out string UserName)
{
UserCode = "01211231";
UserName = "测试";
return true;
} #region 保存异常专业存储过程,使用专用方法,防止第归错误,此方法中如果出现错误记录文本文件 public bool SaveErrorMessageToDB(System.Exception e, string ErrorSQL)
{
bool bRelult = false;
using (this.conn = new SqlConnection(this.strConnection))
{
string UserCode;
string UserName;
this.GetUserInfo(out UserCode, out UserName); System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(true);
// int deep = st.FrameCount;
string CallClassName = st.GetFrame(2).GetMethod().ReflectedType.FullName;
string CallMethodName = st.GetFrame(3).GetMethod().Name; string ProcedureName = "SP_SaveError";
this.comm = new SqlCommand(ProcedureName, this.conn);
this.comm.CommandType = CommandType.StoredProcedure;
this.comm.Parameters.Add("@ErrorModuleID", "IDAL");
this.comm.Parameters.Add("@ErrorClassName", CallClassName);
this.comm.Parameters.Add("@ErrorMethodName", CallMethodName);
this.comm.Parameters.Add("@ErrorMessage", e.Message);
this.comm.Parameters.Add("@ErrorSource", e.Source);
this.comm.Parameters.Add("@ErrorStackTrace", "");//e.StackTrace
this.comm.Parameters.Add("@ErrorSQL", ErrorSQL);
this.comm.Parameters.Add("@ErrorTargetsite", e.TargetSite.ReflectedType.FullName);
this.comm.Parameters.Add("@subCode", UserCode);
this.comm.Parameters.Add("@subName", UserName);
this.comm.Parameters.Add("@subMachine", System.Environment.MachineName); try
{
if (OpenConn() == false) //打开连接
{
return false;
}
this.comm.ExecuteNonQuery();
this.conn.Close();
bRelult = true;
}
catch (SqlException ex)
{
StringBuilder strBd = new StringBuilder();
strBd.Append("时间: " + System.DateTime.Now.ToString() + System.Environment.NewLine);
strBd.Append("UserCode: " + UserCode + System.Environment.NewLine);
strBd.Append("UserName: " + UserName + System.Environment.NewLine);
strBd.Append("Message: " + ex.Message + System.Environment.NewLine);
strBd.Append("ConnectionString: " + this.conn.ConnectionString + System.Environment.NewLine);
strBd.Append("Source: " + ex.Source + "ErrorSQL:" + ErrorSQL + System.Environment.NewLine);
strBd.Append("Server: " + ex.Server + System.Environment.NewLine);
strBd.Append("Procedure: " + ex.Procedure + System.Environment.NewLine);
strBd.Append("Number: " + ex.Number + System.Environment.NewLine);
strBd.Append("StackTrace: " + ex.StackTrace + System.Environment.NewLine + System.Environment.NewLine);
strBd.Append("TargetSite: " + ex.TargetSite + System.Environment.NewLine + System.Environment.NewLine);
this.SaveErrorMessageToFile(strBd.ToString());
}
catch (SystemException ex)
{
StringBuilder strBd = new StringBuilder();
strBd.Append("时间: " + System.DateTime.Now.ToString() + System.Environment.NewLine);
strBd.Append("UserCode: " + UserCode + System.Environment.NewLine);
strBd.Append("UserName: " + UserName + System.Environment.NewLine);
strBd.Append("Message: " + ex.Message + System.Environment.NewLine);
strBd.Append("ConnectionString: " + this.conn.ConnectionString + System.Environment.NewLine);
strBd.Append("Source: " + ex.Source + "ErrorSQL:" + ErrorSQL + System.Environment.NewLine);
strBd.Append("StackTrace: " + ex.StackTrace + System.Environment.NewLine + System.Environment.NewLine);
strBd.Append("TargetSite: " + ex.TargetSite + System.Environment.NewLine + System.Environment.NewLine);
this.SaveErrorMessageToFile(strBd.ToString());
}
catch (Exception ex)
{
StringBuilder strBd = new StringBuilder();
strBd.Append("时间: " + System.DateTime.Now.ToString() + System.Environment.NewLine);
strBd.Append("UserCode: " + UserCode + System.Environment.NewLine);
strBd.Append("UserName: " + UserName + System.Environment.NewLine);
strBd.Append("Message: " + ex.Message + System.Environment.NewLine);
strBd.Append("ConnectionString: " + this.conn.ConnectionString + System.Environment.NewLine);
strBd.Append("Source: " + ex.Source + "ErrorSQL:" + ErrorSQL + System.Environment.NewLine);
strBd.Append("StackTrace: " + ex.StackTrace + System.Environment.NewLine);
strBd.Append("TargetSite: " + ex.TargetSite + System.Environment.NewLine + System.Environment.NewLine);
this.SaveErrorMessageToFile(strBd.ToString());
}
finally
{
this.DisposeConn(); //释放连接
this.comm.Dispose();
}
}
return bRelult;
} /// <summary>
/// 如果保存异常时出现了错误,写入错误日志文件
/// </summary>
/// <param name="ErrorMessage"></param>
/// <returns></returns>
public bool SaveErrorMessageToFile(string ErrorMessage)
{
bool bResult = false; string path = "";//System.Environment.SystemDirectory;
string ServerMapPath = System.Windows.Forms.Application.StartupPath; if (ServerMapPath != null && ServerMapPath != string.Empty)
{
path = ServerMapPath + @"\ErrorDBLog";
}
if (Directory.Exists(path) == false)
{
Directory.CreateDirectory(path);
} string filePath = path + @"\DBError.log";
StreamWriter sw = null;
try
{
sw = new StreamWriter(filePath, true);
sw.WriteLine(ErrorMessage);
bResult = true;
}
catch (Exception ex)
{
string Message = ex.Message;
}
finally
{
if (sw != null)
{
sw.Close();
}
}
return bResult;
}
#endregion }
}
类调用方法:
try
{
DataSet ds = new DataSet();
string strConn="Data Source=127.0.0.1;Initial Catalog=SysAdmin;Persist Security Info=True;User ID=sa;Password=123";
string strSQL = "selecl SysID,[SysName] from SubSystem where SysState='OK' order by sysid asc";
SqlConnection connection = new SqlConnection(strConn);
comm = new SqlCommand(strSQL, connection);
connection.Open();
object o = comm.ExecuteScalar();
}
catch (Exception e)
{
string ErrorSQL = "SQL语句:" + this.comm.CommandText;
err.SaveErrorMessageToDB(e, ErrorSQL); //保存错误信息
}
C#异常处理表、类、SQL的更多相关文章
- EF:根据实体类生成表结构SQL
根据实体类生成表结构SQL: PM> Enable-Migrations -ProjectName Domain -StartUpProjectName Handler -Force PM> ...
- SqlServer数据库表生成C# Model实体类SQL语句——补充
在sql语句最前边加上 use[数据库名] 原链接:https://www.cnblogs.com/jhli/p/11552105.html --[SQL骚操作]SqlServer数据库表生成C ...
- 浅谈数据库优化方案--表和SQL
1.数据类型的选择 1.字段最好设置为非空.若字段为char(8),即便是NULL也会现有8个字符的空间. 2.尽量使用数字型字段,若只含数值信息的字段尽量不要设计为字符型,这会降低查询和连接的性能, ...
- 第三百零七节,Django框架,models.py模块,数据库操作——表类容的增删改查
Django框架,models.py模块,数据库操作——表类容的增删改查 增加数据 create()方法,增加数据 save()方法,写入数据 第一种方式 表类名称(字段=值) 需要save()方法, ...
- 五 Django框架,models.py模块,数据库操作——表类容的增删改查
Django框架,models.py模块,数据库操作——表类容的增删改查 增加数据 create()方法,增加数据 save()方法,写入数据 第一种方式 表类名称(字段=值) 需要save()方法, ...
- 查看Oracle中是否有锁表的sql
1.查看是否有锁表的sql 代码如下: select 'blocker('||lb.sid||':'||sb.username||')-sql:'|| qb.sql_text blockers, 'w ...
- phpmyadmin查看创建表的SQL语句
本人菜鸟 发现创建表的SQL语句还不会 直接phpmyadmin解决的 查看见表的语句除了直接到处SQL格式文件 打开查看外 就是执行语句查询 语句:show create table 表名 貌似大 ...
- 16 BasicHashTable基本哈希表类(三)——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- 15 BasicHashTable基本哈希表类(二)——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- 14 BasicHashTable基本哈希表类(一)——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
随机推荐
- BZOJ 1072 排列
Description 给一个数字串\(s\)和正整数\(d\), 统计\(s\)有多少种不同的排列能被\(d\)整除(可以有前导\(0\)).例如\(123434\)有\(90\)种排列能被\(2\ ...
- IAR FOR ARM 各版本,需要的大家可以收藏了
原创,原帖地址是在阿莫论坛:http://www.amobbs.com/thread-5400051-1-1.html,这里也在博客贴上来供大家参考. 用过Keil和IAR,个人感觉是IAR还是很不错 ...
- struts2 Convention插件零配置,使用注解开发
从struts21开始,struts2不再推荐使用codebehind作为零配置插件,而是改用Convention插件来支持零配置.与以前相比较,Convention插件更彻底. 使用Conventi ...
- How to add alias on Mac(It's common for most system)
Since these files are hidden you will have to do an ls -a to list them. If you don't have one you ca ...
- 字符串(LCT,后缀自动机):BZOJ 2555 SubString
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1620 Solved: 471 Description 懒得写背景了 ...
- 【模拟】Codeforces 691A Fashion in Berland
题目链接: http://codeforces.com/problemset/problem/691/A 题目大意: n个数0或1,要求恰好n-1个1,如果n为1则那个数一定要是1 题目思路: [模拟 ...
- Linked List Cycle——LeetCode
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- HDOJ(HDU) 2317 Nasty Hacks(比较、)
Problem Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of mali ...
- OpenWrt简要刷机教程
准备工作 1. 下载openwrt中文固件到PC.(当然其他英文固件也可) 2 找到路由器的RST键. 3 找到路由器刷机口---姑且称之为“WAN口” 4. 关闭路由器的电源. 5. 将PC网口 ...
- kvm编译安装及常见问题解决
一.KVM的编译安装 1.安装基本系统和开发工具 1.1 编译内核 mkdir /root/kvm cd /root/kvm wget http://www.kernel.org/pub/linux/ ...