Table SQL
/****** Object: Table [dbo].[ Ierror] Script date:09/05/2012 17:00:41 ******/set ansi_nulls ongoset quoted_identifier ongoset ANSI_PADDING Offgocreate TABLE [dbo]. [Ierror] ([Errormoduleid] [varchar] (+) not null,[errorclassname] [varchar] (a) null,[errormethodname] [varchar] (a) null,[ ErrorMessage] [varchar] (+) not null,[errorsource] [varchar] (+) null,[errorstacktrace] [varchar] (+) null,[ Errortargetsite] [varchar] (+) null,[errorsql] [varchar] (8000) null,[subcode] [varchar] (null,[subname] [varchar ] (+) null,[submachine] [varchar] (null,[subtime] [datetime] NULL) on [Primary]goset ansi_padding Offgo
To save the exception information to the Ierror stored procedure:
/* ENERGY: Save exception information to Ierror and table: Ierror*/alter PROCEDURE [dbo].[ Pt_errorsaveerror] @ErrorModuleID varchar, @ErrorClassName varchar (+), @ErrorMethodName varchar (50), @ ErrorMessage varchar (+), @ErrorSource varchar (+), @ErrorStackTrace varchar (+), @ErrorTargetsite varchar (1000 ), @subCode varchar, @subName varchar (@subMachine varchar), @ErrorSQL varchar (4000) Asdeclare @subTime Datetimeset @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 class:
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 {PR Ivate string strconnection = "Data source=127.0.0.1;initial catalog=sysadmin; Persist Security info=true; User Id=sa; Password=123 ";//connection string private SqlConnection conn;//database connection Private SqlCommand Comm;//sqlcommand//Open connection public bool Openconn () {bool bresult = false; if (This.conn = = null) {StringBuilder STRBD = new StringBuilder (); Strbd.append ("Time:" + System.DateTime.Now.ToString () + System.Environment.NewLine); Strbd.append ("Message: Database connection Failed, SqlConnection is empty" + 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; }//Release close connection public void Disposeconn () {if (this.conn! = null && This.conn.State = = System.Data.ConnectionState.Open) {this.conn.Close (); This.conn = null; }}///<summary>///Get login user information, save to error Log///</summary> <param name= "Usercode" ></param>//<param name= "UserName" ></param>//< Returns></returns> public bool GetUserInfo (out string usercode, out string UserName) {U Sercode = "01211231"; UserName = "Test"; return true; } #region Save the exception professional stored procedures, using a dedicated method, to prevent the error in this method if there is a text file in the wrong record, public bool Saveerrormessagetodb (System.Exception e, str ing 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 ", Error SQL); 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)//open Connection {return False } this.comm.ExecuteNonQuery (); This.conn.Close (); Brelult = true; } catch (SqlException ex) {StringBuilder STRBD = new StringBuilder (); Strbd.append ("Time:" + 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 ("Time:" + 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 ("Time:" + 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 ();//Release connection This.comm.Dispose (); }} return Brelult; }///<summary>//If an error occurred while saving the exception, write the error log file///</summary>//<param name= "error Message "></param>// <returns></returns> public bool Saveerrormessagetofile (string errormessage) {bool B Result = 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}}
Class Call Method:
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 the 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 statement:" + this.comm.CommandText; Err. Saveerrormessagetodb (E, errorsql);//Save error message }
C # exception handling tables, classes, SQL