使用App.config配置文件封装连接字符串,方便重复使用
--->添加App.conifg配置文件
--->Add : ConnectionString:
--->添加引用
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <connectionStrings> <add name="conStr" connectionString="Data Source=.;Initial Catalog=;User ID=;Password="/> </connectionStrings> </configuration>
封装一个SQLHelper类方便使用
1 using System.Configuration; 2 using System.Data;//DatSet..Table SqlDataAdapter 3 using System.Data.SqlClient;//SqlConnection Command DataReader 4 namespace Common 5 { 6 public class SqlHelper 7 { 8 //连接字符串 9 //1、添加引用 2、导入命名空间 为了使用ConfigurationManager 10 private static string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; 11 12 //增删改查 13 //查找数据 ExecuteScalar()返回首行首列 ExecuteReader() DataTable 14 15 16 /// <summary> 17 /// 返回DataTable 18 /// </summary> 19 /// <param name="sql">所用的sql语句</param> 20 /// <param name="param">可变,可以传参也可以不传参数</param> 21 /// <returns></returns> 22 public static DataTable ExecuteDataTable(string sql, params SqlParameter[] param) 23 { 24 DataTable dt = new DataTable(); 25 using (SqlConnection con = new SqlConnection(conStr)) 26 { 27 using (SqlDataAdapter adapter = new SqlDataAdapter(sql, con)) 28 { 29 //添加参数 30 adapter.SelectCommand.Parameters.AddRange(param); 31 //1.打开链接,如果连接没有打开,则它给你打开;如果打开,就算了 32 //2.去执行sql语句,读取数据库 33 //3.sqlDataReader,把读取到的数据填充到内存表中 34 adapter.Fill(dt); 35 } 36 } 37 return dt; 38 } 39 40 /// <summary> 41 /// 执行查询,返回首行首列 42 /// </summary> 43 /// <param name="sql"></param> 44 /// <param name="param"></param> 45 /// <returns></returns> 46 public static object ExecuteScalar(string sql, params SqlParameter[] param) 47 { 48 object o = null; 49 using (SqlConnection con = new SqlConnection(conStr)) 50 { 51 using (SqlCommand cmd = new SqlCommand(sql, con)) 52 { 53 cmd.Parameters.AddRange(param); 54 con.Open(); 55 56 o = cmd.ExecuteScalar(); 57 } 58 } 59 return o; 60 } 61 62 63 /// <summary> 64 /// 执行查询,返回SqlDataReader对象 65 /// </summary> 66 /// <param name="sql"></param> 67 /// <param name="param"></param> 68 /// <returns></returns> 69 public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] param) 70 { 71 SqlDataReader reader = null; 72 using (SqlConnection con = new SqlConnection(conStr)) 73 { 74 using (SqlCommand cmd = new SqlCommand(sql, con)) 75 { 76 cmd.Parameters.AddRange(param); 77 con.Open(); 78 79 reader = cmd.ExecuteReader(); 80 } 81 } 82 return reader; 83 } 84 85 /// <summary> 86 /// 执行增删改,返回受影响的行数 87 /// </summary> 88 /// <param name="sql"></param> 89 /// <param name="param"></param> 90 /// <returns></returns> 91 public static int ExecuteNonQuery(string sql, params SqlParameter[] param) 92 { 93 int n = -1; 94 using (SqlConnection con = new SqlConnection(conStr)) 95 { 96 using (SqlCommand cmd = new SqlCommand(sql, con)) 97 { 98 cmd.Parameters.AddRange(param); 99 con.Open(); 100 n = cmd.ExecuteNonQuery(); 101 } 102 } 103 return n; 104 } 105 106 } 107 }
原文:http://www.cnblogs.com/mengxiao/p/6273820.html