SqlHelper类(V1)

 1 static class SqlHelper
 2     {
 3         //app.config文件的继承:
 4 
 5         public static readonly string connstr = 
 6             ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
 7 
 8         public static int ExecuteNonQuery(string sql, 
 9             params SqlParameter[] parameters)
10         {
11             using (SqlConnection conn = new SqlConnection(connstr))
12             {
13                 conn.Open();
14                 using (SqlCommand cmd = conn.CreateCommand())
15                 {
16                     cmd.CommandText = sql;
17                     cmd.Parameters.AddRange(parameters);
18                     return cmd.ExecuteNonQuery();
19                 }
20             }
21         }
22 
23         public static object ExecuteScalar(string sql,
24             params SqlParameter[] parameters)
25         {
26             using (SqlConnection conn = new SqlConnection(connstr))
27             {
28                 conn.Open();
29                 using (SqlCommand cmd = conn.CreateCommand())
30                 {
31                     cmd.CommandText = sql;
32                     cmd.Parameters.AddRange(parameters);
33                     return cmd.ExecuteScalar();
34                 }
35             }
36         }
37 
38         public static DataTable ExecuteDataTable(string sql,
39             params SqlParameter[] parameters)
40         {
41             using (SqlConnection conn = new SqlConnection(connstr))
42             {
43                 conn.Open();
44                 using (SqlCommand cmd = conn.CreateCommand())
45                 {
46                     cmd.CommandText = sql;
47                     cmd.Parameters.AddRange(parameters);
48 
49                     DataSet dataset = new DataSet();
50                     SqlDataAdapter adapter = new SqlDataAdapter(cmd);
51                     adapter.Fill(dataset);
52                     return dataset.Tables[0];
53                 }
54             }
55         }
56 
57         /// <summary>
58         /// 从数据库读取字段时,防止nul值报错
59         /// </summary>
60         /// <param name="value"></param>
61         /// <returns></returns>
62         public static object FromDbValue(object value)
63         {
64             if (value == DBNull.Value)
65             {
66                 return null;
67             }
68             else
69             {
70                 return value;
71             }
72         }
73 
74         /// <summary>
75         /// 写入数据库字段值时,防止Null值报错
76         /// </summary>
77         /// <param name="value"></param>
78         /// <returns></returns>
79         public static object ToDbValue(object value)
80         {
81             if (value == null)
82             {
83                 return DBNull.Value;
84             }
85             else
86             {
87                 return value;
88             }
89         }
90     }

 

SqlHelper类(V1),古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。