【问题标题】:DataSet, SqlDataAdapter, Multiple select returns one tableDataSet、SqlDataAdapter、多选返回一张表
【发布时间】:2013-09-04 17:15:22
【问题描述】:

我想对数据库进行一次调用(包含多个 SELECT 语句),然后将结果数据绑定到多个组件。

我使用 DataSet 和 SqlDataAdapter 填充表格,然后绑定到组件。

问题是第一个 SELECT 语句的结果被放入两个表中,所以当我尝试使用第二批数据时出现“'System.Data.DataRowView' 不包含属性...”错误第二个组件。

我是否误解了它的工作原理?

DataSet ds = new DataSet();

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myString"].ConnectionString);

StringBuilder topicDropDownListSQL = new StringBuilder();
topicDropDownListSQL.Append("SELECT topic.topic_ID, topic.topic_title FROM FPL2012_TOPIC as topic WHERE topic.topic_isEnabled = 1;");
topicDropDownListSQL.Append("SELECT explain.itemExplanationType_ID, explain.itemExplanationType_type FROM FPL2012_ITEM_EXPLANATION_TYPE as explain;");

SqlDataAdapter da = new SqlDataAdapter(topicDropDownListSQL.ToString(), connection);

ds.Tables.Add("Topics");
ds.Tables.Add("ExplainType");

ds.EnforceConstraints = false;

ds.Tables["Topics"].BeginLoadData();
da.Fill(ds.Tables[0]);
ds.Tables["Topics"].EndLoadData();

ds.Tables["ExplainType"].BeginLoadData();
da.Fill(ds.Tables[1]);
ds.Tables["ExplainType"].EndLoadData();

topicDropDownList.DataValueField = "topic_ID";
topicDropDownList.DataTextField = "topic_title";
topicDropDownList.DataSource = ds.Tables["Topics"];
topicDropDownList.DataBind();

explanationTypeDropDownList.DataValueField = "itemExplanationType_ID";
explanationTypeDropDownList.DataTextField = "itemExplanationType_type";
explanationTypeDropDownList.DataSource = ds.Tables["ExplainType"];
explanationTypeDropDownList.DataBind();

connection.Close();

【问题讨论】:

    标签: c# asp.net dataset


    【解决方案1】:

    您可以通过索引而不是名称来访问表

     DataSet ds = new DataSet();
    
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myString"].ConnectionString);
    
    String qry="SELECT topic_ID,topic_title FROM FPL2012_TOPIC WHERE topic_isEnabled = 1; SELECT itemExplanationType_ID, itemExplanationType_type FROM FPL2012_ITEM_EXPLANATION_TYPE ";
    
    SqlDataAdapter da = new SqlDataAdapter(qry, connection);
    
    da.Fill(ds)
    
    topicDropDownList.DataValueField = "topic_ID";
    topicDropDownList.DataTextField = "topic_title";
    topicDropDownList.DataSource = ds.Tables[0];
    topicDropDownList.DataBind();
    
    explanationTypeDropDownList.DataValueField = "itemExplanationType_ID";
    explanationTypeDropDownList.DataTextField = "itemExplanationType_type";
    explanationTypeDropDownList.DataSource = ds.Tables[1];
    explanationTypeDropDownList.DataBind();
    
    connection.Close();
    

    【讨论】:

    • 我试过了,但仍然会导致同样的问题。 Tables[0] 和 Tables[1] 仍然只包含第一个 SELECT STATEMENT 的结果。
    • 我编辑答案用断点调试它并查看数据集中包含多少表
    • 行得通!我的原始代码一定是错误的。我想知道哪种方法(此方法或数据读取器)是最佳实践/最有效?
    • 如果你只有一个表返回,然后使用 sqldatareader,它会返回一个列表 f 记录,像这种情况下使用数据集,你的两个表都存储在同一个数据集中
    【解决方案2】:

    好的,我接下来尝试使用数据读取器,没想到它会起作用,但确实如此!我可以制作多个选择语句,然后填充多个组件。我没有将此标记为答案,因为我仍然认为知道如何使用数据集来做这件事会很有用。

    对我有用的新代码(如果有用的话):

    string connectionString = WebConfigurationManager.ConnectionStrings["myString"].ConnectionString;
    SqlConnection connection = new SqlConnection(connectionString);
    
    StringBuilder sql = new StringBuilder();
    sql.Append("SELECT topic.topic_ID, topic.topic_title FROM FPL2012_TOPIC as topic WHERE topic.topic_isEnabled = 1;");
    sql.Append("SELECT explain.itemExplanationType_ID, explain.itemExplanationType_type FROM FPL2012_ITEM_EXPLANATION_TYPE as explain;");
    
    SqlCommand command = new SqlCommand(sql.ToString(), connection);
    connection.Open();
    SqlDataReader reader = command.ExecuteReader();
    
    topicDropDownList.DataSource = reader;
    topicDropDownList.DataValueField = "topic_ID";
    topicDropDownList.DataTextField = "topic_title";
    topicDropDownList.DataBind();
    
    reader.NextResult();
    
    explanationTypeDropDownList.DataSource = reader;
    explanationTypeDropDownList.DataValueField = "itemExplanationType_ID";
    explanationTypeDropDownList.DataTextField = "itemExplanationType_type";
    explanationTypeDropDownList.DataBind();
    
    reader.Close();
    connection.Close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      相关资源
      最近更新 更多