c# - 有没有办法在 C# 中重用数据集?我想使用断开连接的架构
问题描述
我有一个数据集,其中已经包含来自 DB 的表,我必须在下拉列表事件中使用相同的数据集。但是,我了解为什么数据集在到达我的程序中之前为空。除了在regionDropDownList_SelectedIndexChanged事件中再次建立新的连接,还有其他的重写方式吗?下面是我的代码。非常感谢:)
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
regionDropDown();
}
}
DataSet mySet;
public void regionDropDown() {
// Define ADO.NET objects.
string connectionString = WebConfigurationManager.ConnectionStrings["northwindConString"].ConnectionString;
SqlConnection myConn = new SqlConnection(connectionString);
myConn.Open();
SqlCommand cmd = new SqlCommand("Select * FROM Region", myConn);
SqlDataAdapter daRegion = new SqlDataAdapter(cmd);
DataSet dsRegion = new DataSet();
daRegion.Fill(dsRegion, "Region");
mySet = dsRegion;
foreach (DataRow row in dsRegion.Tables["Region"].Rows)
{
ListItem ls = new ListItem();
ls.Text = row["RegionID"].ToString();
ls.Value = row["RegionID"].ToString();
regionDropDownList.Items.Add(ls);
}
myConn.Close();
}
public void regionDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
regionDropDown();
foreach (DataRow row in mySet.Tables["Regions"].Rows)
{
if (regionDropDownList.SelectedValue == row["RegionID"].ToString())
{
regionDescriptionLabel.Text = row["RegionDescription"].ToString();
}
}
}
catch (Exception ex) { regionDescriptionLabel.Text = "Caught!!" + ex; }
}
解决方案
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
regionDropDown();
}
}
DataSet mySet; // ?
public void regionDropDown() {
// Define ADO.NET objects.
string connectionString = WebConfigurationManager.ConnectionStrings["northwindConString"].ConnectionString;
SqlConnection myConn = new SqlConnection(connectionString);
myConn.Open();
SqlCommand cmd = new SqlCommand("Select * FROM Region", myConn);
SqlDataAdapter daRegion = new SqlDataAdapter(cmd);
DataSet dsRegion = new DataSet();
daRegion.Fill(dsRegion, "Region");
mySet = dsRegion;
// If you want,custom logic then you might use this code,
// else just provide datatextfield and datavalue field before binding..
foreach (DataRow row in dsRegion.Tables["Region"].Rows)
{
ListItem ls = new ListItem();
ls.Text = row["RegionID"].ToString();
ls.Value = row["RegionID"].ToString();
regionDropDownList.Items.Add(ls);
}
myConn.Close();
}
现在在 selectedindexchanged 事件的情况下?
public void regionDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
regionDropDown();
foreach (DataRow row in mySet.Tables["Regions"].Rows)
{
if (regionDropDownList.SelectedValue == row["RegionID"].ToString())
{
regionDescriptionLabel.Text = row["RegionDescription"].ToString();
}
}
}
catch (Exception ex) { regionDescriptionLabel.Text = "Caught!!" + ex; }
}
为什么在这里再次调用 regionDropDown() ?
public void regionDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
regionDescriptionLabel.Text = regionDropDownList.SelectedItem.Text; // If value needed then regionDropDownList.SelectedValue
}
推荐阅读
- android - Android 应用程序和邮递员中的 API 响应时间不同
- sql - 使用日期时间计算目录中的所有文件
- algorithm - 查找最大岛屿的区域(未正确返回的区域)
- python-3.x - 我正在从芯片交易中抓取数据,其中包括产品名称和包装尺寸。我想将产品名称和包装尺寸分开
- amazon-web-services - AWS Copilot - 将导入的策略附加到任务角色
- javascript - 使用某种全局变量或道具从starting.tsx文件更改style.css文件中的属性或另一个tsx文件中的变量?
- python-3.x - 使用 pip 从依赖项中安装依赖项
- python - 使用 Python 绘制一个圆圈并为其中的区域着色?
- javascript - 拖动全景bg图像并对指南针箭头产生影响
- html - Apache vhost:我的文件夹结构的正确设置?