1、首先是数据库的建立,给出的代码会引起中文乱码,所以就简单的改了一下
first:将varchar(50)改成了nvarchar(50);
second:将每个中文字符串前面加了一个N
2、Winform程序的建立,很简单的界面
DropDownStyle属性 改成了DropDownList
3、添加应用程序配置文件
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
< configuration >
< connectionStrings >
< add name = " ConnStr " connectionString = " Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\PC.mdf;Integrated Security=True;User Instance=True; " />
</ connectionStrings >
</ configuration >
< configuration >
< connectionStrings >
< add name = " ConnStr " connectionString = " Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\PC.mdf;Integrated Security=True;User Instance=True; " />
</ connectionStrings >
</ configuration >
把连接数据库的部分放到这里面,在IP地址变动的时候就只需要改配置文件,不用改源程序了
4、代码
winform窗体里面的代码,configuration类在引用里面本来是没有的,必须要手动添加
这里面定义了一个类
class ProvinceItem
{
public string name;
public int id;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
public int Id
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
}
因为版本是2005,就不能简写了
cb省.Items.Add(item); 这句里面其实添加的是一个item对象,而对象转换成string类型其实返回的是类名,因此对这部分需要改一下ComboBox的属性:将DisplayMember属性值改成Name
private
void
Form1_Load(
object
sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings[ " ConnStr " ].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr)) // 创建数据库连接
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand()) // 创建数据库交互
{
cmd.CommandText = " select * from promary " ;
using (SqlDataReader datareader = cmd.ExecuteReader())
{
while (datareader.Read())
{
ProvinceItem item = new ProvinceItem();
item.Id = datareader.GetInt32(datareader.GetOrdinal( " proID " ));
item.Name = datareader.GetString(datareader.GetOrdinal( " proName " ));
cb省.Items.Add(item);
}
}
}
}
}
{
string connStr = ConfigurationManager.ConnectionStrings[ " ConnStr " ].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr)) // 创建数据库连接
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand()) // 创建数据库交互
{
cmd.CommandText = " select * from promary " ;
using (SqlDataReader datareader = cmd.ExecuteReader())
{
while (datareader.Read())
{
ProvinceItem item = new ProvinceItem();
item.Id = datareader.GetInt32(datareader.GetOrdinal( " proID " ));
item.Name = datareader.GetString(datareader.GetOrdinal( " proName " ));
cb省.Items.Add(item);
}
}
}
}
}
private
void
cb省_SelectedIndexChanged(
object
sender, EventArgs e)
{
ProvinceItem item = (ProvinceItem)cb省.SelectedItem;
int proID = item.id;
cb市.Items.Clear();
string connStr = ConfigurationManager.ConnectionStrings[ " ConnStr " ].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr)) // 创建数据库连接
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand()) // 创建数据库交互
{
cmd.CommandText = " select * from city where proID=@proID " ;
cmd.Parameters.Add( new SqlParameter( " proID " , proID));
using (SqlDataReader datareader = cmd.ExecuteReader())
{
while (datareader.Read())
{
string cityName = datareader.GetString(datareader.GetOrdinal( " cityName " ));
cb市.Items.Add(cityName);
}
}
}
}
}
{
ProvinceItem item = (ProvinceItem)cb省.SelectedItem;
int proID = item.id;
cb市.Items.Clear();
string connStr = ConfigurationManager.ConnectionStrings[ " ConnStr " ].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr)) // 创建数据库连接
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand()) // 创建数据库交互
{
cmd.CommandText = " select * from city where proID=@proID " ;
cmd.Parameters.Add( new SqlParameter( " proID " , proID));
using (SqlDataReader datareader = cmd.ExecuteReader())
{
while (datareader.Read())
{
string cityName = datareader.GetString(datareader.GetOrdinal( " cityName " ));
cb市.Items.Add(cityName);
}
}
}
}
}
然后是cb省部分的代码
这里面涉及到一个强制类型转换,将object类型转换成了ProvinceItem类型