DataSet,SqlDataAdapter,Multiple select返回一个表

时间:2013-09-04 08:43:00

标签: c# asp.net dataset

我想对数据库进行一次调用(包含几个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();

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

好的,接下来我尝试使用datareader,不要指望它可以正常工作,但确实如此!我可以创建多个select语句,然后填充多个组件。我并不认为这是一个答案,因为我仍然认为知道如何使用数据集来做这件事会很有用。

适用于我的新代码(如果有用):

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();