数据库操作异常写入日志文件

 1
 2public class ErrorOperate:System.Web.UI.Page,System.IDisposable
 3    {
 4        public ErrorOperate()
 5        {
 6            //
 7            // TODO: 在此处添加构造函数逻辑
 8            //
 9        }

10public void writeLog(string astrMsg)
11        {
12            try
13            {
14                string  ErrorFile=System.Configuration.ConfigurationSettings.AppSettings["ErrorFile"];
15                if(!File.Exists(ErrorFile))
16                {
17                    //错误日志不存在创建
18                
19                    StreamWriter sr = File.CreateText(ErrorFile);
20                    sr.WriteLine ("错误日志.");                    
21                    sr.Close();
22                }

23                FileInfo fileinfo=new FileInfo(ErrorFile);
24
25                using(FileStream fs=fileinfo.OpenWrite())
26                {
27                    StreamWriter sw=new StreamWriter(fs);
28                    sw.BaseStream.Seek(0,SeekOrigin.End);
29                    sw.WriteLine("=====================================");
30                    sw.Write("添加日期为:" + DateTime.Now.ToString() +"\r\n");
31                    sw.Write("错误信息:" + astrMsg + "\r\n");
32                    sw.WriteLine("=====================================");
33                    sw.Flush();
34                    sw.Close();
35                }

36            }

37            catch(Exception ex)
38            {
39                ex.ToString();
40            }

41        }

42}

43
44/// <summary>
45        /// 插入新的数据
46        /// astrColName 存放要插入的数据的字段名的数组
47        /// astrColValue 存放插入数据的数组
48        /// </summary>
49        /// <param name="strTabName"></param>
50        /// <param name="astrColName"></param>
51        /// <param name="astrColValue"></param>

52
53        public void InsRow(string[] astrColName,string[] astrColValue)
54        {
55            string strSQL = "";
56            string strColName = " ";
57            string strColValue = " ";
58            try
59            {
60                //形成insert语句
61                for(int i=0;i<astrColName.Length;i++)
62                {
63                    strColName = strColName + astrColName[i] + ",";
64                    strColValue = strColValue + "'" + astrColValue[i] + "'" + ",";
65                }

66
67                //去掉最右边的","
68                strColName = strColName.Substring(0,strColName.Length-1);
69                strColValue = strColValue.Substring(0,strColValue.Length-1);
70
71                //增加上右括号"(" 与 左括号")"
72                strColName = "(" + strColName + ")";
73                strColValue = "(" + strColValue + ")";
74
75                //形成最终的SQL
76                strSQL="insert into " + strTabName + strColName + " values " + strColValue;
77                ExecuteSQL(strSQL);
78            }

79            catch(Exception er)
80            {
81                
82                ErrorOperate error=new ErrorOperate();
83                error.writeLog(er.Message+"\r\n"+"错误地址:"+er.StackTrace+"\r\n"+"错误语句:"+strSQL);
84                
85                throw  new  System.ObjectDisposedException( er.Message);
86            }

87            finally
88            {
89
90            }

91
92        }
原文地址:https://www.cnblogs.com/pbc1984/p/625640.html