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