asp.net 写的 国家,省,城市三级联动——所想
今天在 csdn 看到一个朋友用 asp.net 写的 国家,省,城市三级联动,有了些想法。
...
今天在 csdn 看到一个朋友用 asp.net 写的 国家,省,城市三级联动,有了些想法。
这个联动所有数据都存储在数据库中,并且每取一次数据都打开关闭数据库一次,这对于数据库服务器会不会有额外的开销呢?我的意思是指增加数据库服务器的负担。
我个人认为并没有必要这样,我们可以一直让数据库连接打开,等取完所有数据后再关闭。也许你认为这样更耗资源,不是有数据库连接池吗?
还有,这些数据我们可以将它看成是静态数据(省市不会有太大变动吧?),我们为什么不可以将它写成一个 .xml 文件呢?然后用 xml 的类操作取得数据填充即可,这样做的优势不言而喻。
附上那位朋友的代码:
******************
BinderToNationOrArea()绑定国家或地区,省份或洲,城市#region BinderToNationOrArea()绑定国家或地区,省份或洲,城市
/**//// <summary>
/// 绑定国家或地区
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToNationOrArea()
...{
//打开数据库的连接
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//国家或地区
SqlCommand cmd = new SqlCommand("select * from NationArea where NationArea = '中国' ",cn);
SqlDataReader sdr = cmd.ExecuteReader();
this.ddlNationOrArea.DataSource = sdr;
this.ddlNationOrArea.DataTextField = "NationArea";
this.ddlNationOrArea.DataValueField = "NationAreaID";
this.ddlNationOrArea.DataBind();
sdr.Close();
//省份或洲
//得到国家的ID
string NationAreaID = this.ddlNationOrArea.SelectedValue;
cmd.CommandText = "select * from Province where NationAreaID = '" + NationAreaID + "'";
sdr = cmd.ExecuteReader();
this.ddlProvince.DataSource = sdr;
this.ddlProvince.DataTextField = "ProvinceName";
this.ddlProvince.DataValueField = "ProvinceID";
this.ddlProvince.DataBind();
sdr.Close();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
cmd.CommandText = "select * from City where ProvinceID = '" + ProvinceID + "'";
sdr = cmd.ExecuteReader();
this.ddlCity.DataSource = sdr;
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
sdr.Close();
//关闭数据库的连接
cn.Close();
//本个控件的值
//国家
this.txtNationOrArea.Text = this.ddlNationOrArea.SelectedItem.Text.Trim();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示邮政编码
this.PostCode();
}
#endregion
BinderToProvince()绑定各个省,或洲#region BinderToProvince()绑定各个省,或洲
/**//// <summary>
/// 绑定各个省,或洲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToProvince()
...{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//省份或洲
//得到国家的ID
string NationAreaID = this.ddlNationOrArea.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter("select * from Province where NationAreaID = '" + NationAreaID + "'",cn);
DataSet ds = new DataSet();
sda.Fill(ds,"Province");
this.ddlProvince.DataSource = ds.Tables["Province"];
this.ddlProvince.DataTextField = "ProvinceName";
this.ddlProvince.DataValueField = "ProvinceID";
this.ddlProvince.DataBind();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
sda.Fill(ds,"City");
this.ddlCity.DataSource =ds.Tables["City"];
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
//关闭数据库的连接
cn.Close();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//城市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示相应邮政编码
this.PostCode();
}
#endregion
BinderToCity() 绑定各个城市#region BinderToCity() 绑定各个城市
/**//// <summary>
/// 绑定各个城市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToCity()
...{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter("select * from City where ProvinceID = '" + ProvinceID + "'",cn);
DataSet ds = new DataSet();
sda.Fill(ds,"City");
this.ddlCity.DataSource = ds.Tables["City"];
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
//关闭数据库的连接
cn.Close();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示相应的邮政编码
this.PostCode();
}
#endregion
这个联动所有数据都存储在数据库中,并且每取一次数据都打开关闭数据库一次,这对于数据库服务器会不会有额外的开销呢?我的意思是指增加数据库服务器的负担。
我个人认为并没有必要这样,我们可以一直让数据库连接打开,等取完所有数据后再关闭。也许你认为这样更耗资源,不是有数据库连接池吗?
还有,这些数据我们可以将它看成是静态数据(省市不会有太大变动吧?),我们为什么不可以将它写成一个 .xml 文件呢?然后用 xml 的类操作取得数据填充即可,这样做的优势不言而喻。
附上那位朋友的代码:
******************
BinderToNationOrArea()绑定国家或地区,省份或洲,城市#region BinderToNationOrArea()绑定国家或地区,省份或洲,城市
/**//// <summary>
/// 绑定国家或地区
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToNationOrArea()
...{
//打开数据库的连接
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//国家或地区
SqlCommand cmd = new SqlCommand("select * from NationArea where NationArea = '中国' ",cn);
SqlDataReader sdr = cmd.ExecuteReader();
this.ddlNationOrArea.DataSource = sdr;
this.ddlNationOrArea.DataTextField = "NationArea";
this.ddlNationOrArea.DataValueField = "NationAreaID";
this.ddlNationOrArea.DataBind();
sdr.Close();
//省份或洲
//得到国家的ID
string NationAreaID = this.ddlNationOrArea.SelectedValue;
cmd.CommandText = "select * from Province where NationAreaID = '" + NationAreaID + "'";
sdr = cmd.ExecuteReader();
this.ddlProvince.DataSource = sdr;
this.ddlProvince.DataTextField = "ProvinceName";
this.ddlProvince.DataValueField = "ProvinceID";
this.ddlProvince.DataBind();
sdr.Close();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
cmd.CommandText = "select * from City where ProvinceID = '" + ProvinceID + "'";
sdr = cmd.ExecuteReader();
this.ddlCity.DataSource = sdr;
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
sdr.Close();
//关闭数据库的连接
cn.Close();
//本个控件的值
//国家
this.txtNationOrArea.Text = this.ddlNationOrArea.SelectedItem.Text.Trim();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示邮政编码
this.PostCode();
}
#endregion
BinderToProvince()绑定各个省,或洲#region BinderToProvince()绑定各个省,或洲
/**//// <summary>
/// 绑定各个省,或洲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToProvince()
...{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//省份或洲
//得到国家的ID
string NationAreaID = this.ddlNationOrArea.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter("select * from Province where NationAreaID = '" + NationAreaID + "'",cn);
DataSet ds = new DataSet();
sda.Fill(ds,"Province");
this.ddlProvince.DataSource = ds.Tables["Province"];
this.ddlProvince.DataTextField = "ProvinceName";
this.ddlProvince.DataValueField = "ProvinceID";
this.ddlProvince.DataBind();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
sda.Fill(ds,"City");
this.ddlCity.DataSource =ds.Tables["City"];
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
//关闭数据库的连接
cn.Close();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//城市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示相应邮政编码
this.PostCode();
}
#endregion
BinderToCity() 绑定各个城市#region BinderToCity() 绑定各个城市
/**//// <summary>
/// 绑定各个城市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToCity()
...{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter("select * from City where ProvinceID = '" + ProvinceID + "'",cn);
DataSet ds = new DataSet();
sda.Fill(ds,"City");
this.ddlCity.DataSource = ds.Tables["City"];
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
//关闭数据库的连接
cn.Close();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示相应的邮政编码
this.PostCode();
}
#endregion