使用App.config配置文件封裝連接字符串,,方便重復(fù)使用 <?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> 封裝一個(gè)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、導(dǎo)入命名空間 為了使用ConfigurationManager 10 private static string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; 11 12 //增刪改查 13 //查找數(shù)據(jù) ExecuteScalar()返回首行首列 ExecuteReader() DataTable 14 15 16 /// <summary> 17 /// 返回DataTable 18 /// </summary> 19 /// <param name="sql">所用的sql語(yǔ)句</param> 20 /// <param name="param">可變,,可以傳參也可以不傳參數(shù)</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 //添加參數(shù) 30 adapter.SelectCommand.Parameters.AddRange(param); 31 //1.打開鏈接,,如果連接沒有打開,則它給你打開,;如果打開,,就算了 32 //2.去執(zhí)行sql語(yǔ)句,讀取數(shù)據(jù)庫(kù) 33 //3.sqlDataReader,把讀取到的數(shù)據(jù)填充到內(nèi)存表中 34 adapter.Fill(dt); 35 } 36 } 37 return dt; 38 } 39 40 /// <summary> 41 /// 執(zhí)行查詢,,返回首行首列 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 /// 執(zhí)行查詢,,返回SqlDataReader對(duì)象 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 /// 執(zhí)行增刪改,返回受影響的行數(shù) 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 } C#namespace: 是為了防止命名重復(fù)的 ,。
比如你在兩個(gè)不同的命名空間中都可以有Student類,。
此命名空間范圍允許您組織代碼并為您提供了創(chuàng)建全局唯一類型的方法。
C#中Sqlparamater的用法:http://blog.csdn.net/zzp_403184692/article/details/8092408
下面這個(gè)是應(yīng)用sqlHelper和ComboBox來(lái)展示 省市聯(lián)動(dòng):
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //在應(yīng)用程序加載的時(shí)候 ,,去數(shù)據(jù)庫(kù)查找省的數(shù)據(jù),,給cboPro DataTable dt = SqlHelper.ExecuteDataTable("select * from promary"); //將返回的DataTable作為cboPro的數(shù)據(jù)源 //讓cboPro顯示proName這個(gè)字段的值,一般是顯示給客戶看的 cboPro.DisplayMember = "proName"; //讓valueMemberID,,綁定的是對(duì)應(yīng)的值,,綁定處理程序標(biāo)識(shí) 給程序員看的。 cboPro.ValueMember = "proID"; cboPro.DataSource = dt; } private void cboPro_SelectedIndexChanged(object sender, EventArgs e) { //之前的寫法 // MessageBox.Show(cboPro.Text);//獲得在cbo中選擇文本 //MessageBox.Show(cboPro.SelectedValue.ToString());//獲得關(guān)聯(lián)的數(shù)據(jù) //string sql = "select * from city where proID="+cboPro.SelectedValue.ToString(); //帶參數(shù)的sql語(yǔ)句 string sql = "select * from city where proID=@proID"; //準(zhǔn)備一個(gè)sql參數(shù) SqlParameter p = new SqlParameter("@proID", cboPro.SelectedValue.ToString()); //設(shè)置cboCity要顯示的數(shù)據(jù) cboCity.DisplayMember = "cityName"; //根據(jù)sql語(yǔ)句查詢到的數(shù)據(jù)集 cboCity.DataSource = SqlHelper.ExecuteDataTable(sql, p); } }
|
|