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 ...
随机推荐
- 从相对路径说开来(从C++到Qt)
从相对路径说开来(从C++到Qt) 转载自:http://blog.csdn.net/dbzhang800/article/details/6363165 在Qt论坛经常看到网友抱怨: QPixmap ...
- ExtJs + Struts2 + JSON
最近一直都在看EXTJS的东西,然后自己实践了下,界面倒是蛮漂亮的,但是一旦涉及到与服务器端进行数据互动麻烦就出来了,本来下了个例子确发现是 用DWR的,觉得我既然用了STRUTS2作为MVC的框架, ...
- Hibernate 配置详解(5)
9) hibernate.batch_fetch_style: 该配置是hibernate4.2.0新添加的,使用这个设置可以配置hibernate在做batch-fetch的时候,生成SQL的策略. ...
- width:auto; 和 width:100%;的不同
width:auto:会将元素撑开至整个父元素width,但是会减去子节点自己的margin,padding或者border的大小.width:100%:会强制将元素变成和父元素一样的宽,并且添加额外 ...
- 常用的用户状态命令包括:whoami、id、groups、newgrp 等
用户状态命令 常用的用户状态命令包括:whoami.id.groups.newgrp 等.
- COJ 0047 20702最大乘积
20702最大乘积 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 输入n个元素组成的序列s,你需要找出一个乘积最大的连续子序列 ...
- 图论(网络流):SCOI 2007 修车
同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均等待的时间最小 ...
- 网络流(最大流):POJ 1149 PIGS
PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) ...
- 动态树(LCT):HDU 4010 Query on The Trees
Query on The Trees Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Othe ...
- java编码问题深入总结
Java语言能够这么普遍的应用,与其国际化的能力是 分不开的,国际化的编码是Java国际化中最重要的一个组成部分,Java的国际化编码能力与其使用Unicode编码是直接相关的.在Java中,任何 ...