sqlHelper:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient; namespace WindowsFormsApplication1
{
public class sqlHelper
{
private static readonly string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
//增删改
public static int ExecuteNonquery(string sql, params SqlParameter[] ps)
{
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
if (ps != null)
{
cmd.Parameters.AddRange(ps);
}
return cmd.ExecuteNonQuery();
}
} }
//首行首列
public static object ExecuteScalar(string sql, params SqlParameter[] ps)
{
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
if (ps != null)
{
cmd.Parameters.AddRange(ps);
}
return cmd.ExecuteScalar();
}
}
}
//查询
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] ps)
{
SqlConnection con = new SqlConnection(str);
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (ps != null)
{
cmd.Parameters.AddRange(ps);
}
try
{
con.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
con.Close();
con.Dispose();
throw ex;
}
}
}
}
}

Form1设计:

c#省市联动(sqlHelper的应用)-LMLPHP

Form1代码:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
LoadShengById2();
} private void LoadShengById2(int p)
{
string sql = "select id1, name from city where id2=" + p;
List<City> list = new List<City>();
list.Add(new City() { Id1 = -,Name = "请选择"});//加一个“请选择”,如果不加,点北京不出城市,为什么?
using (SqlDataReader reader = sqlHelper.ExecuteReader(sql))
{
if (reader.HasRows)
{
while (reader.Read())
{
City c = new City();
c.Id1 = Convert.ToInt32(reader["id1"]);
c.Name = reader["name"].ToString();
//c.Id2 = Convert.ToInt32(reader["id2"]);
list.Add(c);
} }
}
//下面的放在using外面
cobSheng.DataSource = list;
cobSheng.DisplayMember = "Name";//显示的是哪个名字的信息
cobSheng.ValueMember = "Id1";//实际保存的信息 } private void cobSheng_SelectedIndexChanged(object sender, EventArgs e)
{
//一定要加判断,有选中才运行!
if (cobSheng.SelectedIndex != )
{
//MessageBox.Show(cobSheng.SelectedValue.ToString());
int id = Convert.ToInt32(cobSheng.SelectedValue);
string sql = "select * from city where id2=" + id;
List<City> list = new List<City>();
using (SqlDataReader reader = sqlHelper.ExecuteReader(sql))
{
if (reader.HasRows)
{
while (reader.Read())
{
City c = new City();
c.Id1 = Convert.ToInt32(reader["id1"]);
c.Name = reader["name"].ToString();
c.Id2 = Convert.ToInt32(reader["id2"]);
list.Add(c);
}
cobShi.DataSource = list;
cobShi.DisplayMember = "name";
}
}
} }
}
}

City类:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WindowsFormsApplication1
{
public class City
{
private int _id1; public int Id1
{
get { return _id1; }
set { _id1 = value; }
}
private string _name; public string Name
{
get { return _name; }
set { _name = value; }
}
private int _id2; public int Id2
{
get { return _id2; }
set { _id2 = value; }
}
}
}

App.config:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add connectionString="Data Source =.; Initial Catalog = mysql; Integrated Security = True;" name="conStr" />
</connectionStrings>
</configuration>

数据库:

c#省市联动(sqlHelper的应用)-LMLPHP

05-12 11:04