首页 > 解决方案 > 有没有办法在 C# 中重用数据集?我想使用断开连接的架构

问题描述

我有一个数据集,其中已经包含来自 DB 的表,我必须在下拉列表事件中使用相同的数据集。但是,我了解为什么数据集在到达我的程序中之前为空。除了在r​​egionDropDownList_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; }
}

标签: c#

解决方案


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

    }

推荐阅读