数据库操作中的SqlHelper方法

 1 class SqlHelper
2 {
3 private static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
4
5 public static int ExecuteNonQuery(string cmdStr,params SqlParameter[] parameters)
6 {
7 using (SqlConnection conn = new SqlConnection(connStr))
8 {
9 conn.Open();
10 using (SqlCommand cmd = conn.CreateCommand())
11 {
12 cmd.CommandText = cmdStr;
13 cmd.Parameters.AddRange(parameters);
14 return cmd.ExecuteNonQuery();
15 }
16 }
17 }
18
19 public static object ExecuteScalar(string cmdStr, params SqlParameter[] parameters)
20 {
21 using (SqlConnection conn = new SqlConnection(connStr))
22 {
23 conn.Open();
24 using (SqlCommand cmd = conn.CreateCommand())
25 {
26 cmd.CommandText = cmdStr;
27 cmd.Parameters.AddRange(parameters);
28 return cmd.ExecuteScalar();
29 }
30 }
31 }
32
33 public static DataTable ExecuteDataTable(string cmdStr, params SqlParameter[] parameters)
34 {
35 using (SqlConnection conn = new SqlConnection(connStr))
36 {
37 conn.Open();
38 using (SqlCommand cmd = conn.CreateCommand())
39 {
40 DataTable dt = new DataTable();
41 cmd.CommandText = cmdStr;
42 cmd.Parameters.AddRange(parameters);
43 SqlDataAdapter adapter = new SqlDataAdapter(cmd);
44 adapter.Fill(dt);
45 return dt;
46 }
47 }
48
49 }
50 }




 

你可能感兴趣的:(help)