sqlHelper:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Configuration; 7 using System.Data.SqlClient; 8 9 namespace WindowsFormsApplication1 10 { 11 public class sqlHelper 12 { 13 private static readonly string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; 14 //增删改 15 public static int ExecuteNonquery(string sql, params SqlParameter[] ps) 16 { 17 using (SqlConnection con = new SqlConnection(str)) 18 { 19 using (SqlCommand cmd = new SqlCommand(sql, con)) 20 { 21 con.Open(); 22 if (ps != null) 23 { 24 cmd.Parameters.AddRange(ps); 25 } 26 return cmd.ExecuteNonQuery(); 27 } 28 } 29 30 } 31 //首行首列 32 public static object ExecuteScalar(string sql, params SqlParameter[] ps) 33 { 34 using (SqlConnection con = new SqlConnection(str)) 35 { 36 using (SqlCommand cmd = new SqlCommand(sql, con)) 37 { 38 con.Open(); 39 if (ps != null) 40 { 41 cmd.Parameters.AddRange(ps); 42 } 43 return cmd.ExecuteScalar(); 44 } 45 } 46 } 47 //查询 48 public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] ps) 49 { 50 SqlConnection con = new SqlConnection(str); 51 using (SqlCommand cmd = new SqlCommand(sql, con)) 52 { 53 if (ps != null) 54 { 55 cmd.Parameters.AddRange(ps); 56 } 57 try 58 { 59 con.Open(); 60 return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); 61 } 62 catch (Exception ex) 63 { 64 con.Close(); 65 con.Dispose(); 66 throw ex; 67 } 68 } 69 } 70 } 71 }
Form1设计:
Form1代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Data.SqlClient; 11 12 namespace WindowsFormsApplication1 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 private void Form1_Load(object sender, EventArgs e) 22 { 23 LoadShengById2(0); 24 } 25 26 private void LoadShengById2(int p) 27 { 28 string sql = "select id1, name from city where id2=" + p; 29 List<City> list = new List<City>(); 30 list.Add(new City() { Id1 = -1,Name = "请选择"});//加一个“请选择”,如果不加,点北京不出城市,为什么? 31 using (SqlDataReader reader = sqlHelper.ExecuteReader(sql)) 32 { 33 if (reader.HasRows) 34 { 35 while (reader.Read()) 36 { 37 City c = new City(); 38 c.Id1 = Convert.ToInt32(reader["id1"]); 39 c.Name = reader["name"].ToString(); 40 //c.Id2 = Convert.ToInt32(reader["id2"]); 41 list.Add(c); 42 } 43 44 } 45 } 46 //下面的放在using外面 47 cobSheng.DataSource = list; 48 cobSheng.DisplayMember = "Name";//显示的是哪个名字的信息 49 cobSheng.ValueMember = "Id1";//实际保存的信息 50 51 } 52 53 private void cobSheng_SelectedIndexChanged(object sender, EventArgs e) 54 { 55 //一定要加判断,有选中才运行! 56 if (cobSheng.SelectedIndex != 0) 57 { 58 //MessageBox.Show(cobSheng.SelectedValue.ToString()); 59 int id = Convert.ToInt32(cobSheng.SelectedValue); 60 string sql = "select * from city where id2=" + id; 61 List<City> list = new List<City>(); 62 using (SqlDataReader reader = sqlHelper.ExecuteReader(sql)) 63 { 64 if (reader.HasRows) 65 { 66 while (reader.Read()) 67 { 68 City c = new City(); 69 c.Id1 = Convert.ToInt32(reader["id1"]); 70 c.Name = reader["name"].ToString(); 71 c.Id2 = Convert.ToInt32(reader["id2"]); 72 list.Add(c); 73 } 74 cobShi.DataSource = list; 75 cobShi.DisplayMember = "name"; 76 } 77 } 78 } 79 80 81 } 82 } 83 }
City类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace WindowsFormsApplication1 8 { 9 public class City 10 { 11 private int _id1; 12 13 public int Id1 14 { 15 get { return _id1; } 16 set { _id1 = value; } 17 } 18 private string _name; 19 20 public string Name 21 { 22 get { return _name; } 23 set { _name = value; } 24 } 25 private int _id2; 26 27 public int Id2 28 { 29 get { return _id2; } 30 set { _id2 = value; } 31 } 32 } 33 }
App.config:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <startup> 4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 5 </startup> 6 <connectionStrings> 7 <add connectionString="Data Source =.; Initial Catalog = mysql; Integrated Security = True;" name="conStr" /> 8 </connectionStrings> 9 </configuration>
数据库: