1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data.SqlClient; 6 using System.Configuration; 7 using System.Data; 8 9 namespace adonet10 {11 class SqlHelper12 {13 private static string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;14 15 public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)16 {17 using (SqlConnection conn = new SqlConnection(connStr))18 {19 conn.Open();20 using (SqlCommand cmd = conn.CreateCommand())21 {22 cmd.CommandText = sql;23 cmd.Parameters.AddRange(parameters);24 return cmd.ExecuteNonQuery();25 }26 }27 }28 29 public static object ExecuteScalar(string sql, params SqlParameter[] parameters)30 {31 using (SqlConnection conn = new SqlConnection(connStr))32 {33 conn.Open();34 using (SqlCommand cmd = conn.CreateCommand())35 {36 cmd.CommandText = sql;37 cmd.Parameters.AddRange(parameters);38 return cmd.ExecuteScalar();39 }40 }41 }42 43 public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)44 {45 using (SqlConnection conn = new SqlConnection(connStr))46 {47 conn.Open();48 using (SqlCommand cmd = conn.CreateCommand())49 {50 cmd.CommandText = sql;51 cmd.Parameters.AddRange(parameters);52 53 SqlDataAdapter adapter = new SqlDataAdapter(cmd);54 DataSet dataset=new DataSet();55 adapter.Fill(dataset);56 return dataset.Tables[0];57 }58 }59 }60 }61 }