我试图填补数据集包含2台具有一对多的关系。 我使用的DataReader来实现这一点:
public DataSet SelectOne(int id)
{
DataSet result = new DataSet();
using (DbCommand command = Connection.CreateCommand())
{
command.CommandText = "select * from table1";
var param = ParametersBuilder.CreateByKey(command, "ID", id, null);
command.Parameters.Add(param);
Connection.Open();
using (DbDataReader reader = command.ExecuteReader())
{
result.MainTable.Load(reader);
}
Connection.Close();
}
return result;
}
但我有只有一个充满了桌子。 如何实现我的目标 - 填补这两个表?
我想,如果有可能使用的DataReader,而不是DataAdapter的。
Answer 1:
如果您正在发行与多个SELECT语句一条命令,你可以使用NextResult方法来DataReader的范围内移动到下一个结果集: http://msdn.microsoft.com/en-us/library/system.data.idatareader.nextresult。 ASPX
我展示它可能看起来波纹管:
public DataSet SelectOne(int id)
{
DataSet result = new DataSet();
using (DbCommand command = Connection.CreateCommand())
{
command.CommandText = @"
select * from table1
select * from table2
";
var param = ParametersBuilder.CreateByKey(command, "ID", id, null);
command.Parameters.Add(param);
Connection.Open();
using (DbDataReader reader = command.ExecuteReader())
{
result.MainTable.Load(reader);
reader.NextResult();
result.SecondTable.Load(reader);
// ...
}
Connection.Close();
}
return result;
}
Answer 2:
填充DataSet中多个表可以通过发送多个请求的数据库中进行,或以更快的方式:多个SELECT语句可以在单个请求被发送到数据库服务器。 这里的问题是,从查询产生的表有自动名称表和表1。 但是,生成的表名可以映射到应该在DataSet中使用的名称。
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT * FROM Customers; SELECT * FROM Orders", connection);
adapter.TableMappings.Add("Table", "Customer");
adapter.TableMappings.Add("Table1", "Order");
adapter.Fill(ds);
Answer 3:
这是一个老话题,但对于一些人来说可能是有用的:
DataSet someDataSet = new DataSet();
SqlDataAdapter adapt = new SqlDataAdapter();
using(SqlConnection connection = new SqlConnection(ConnString))
{
connection.Open();
SqlCommand comm1 = new SqlCommand("SELECT * FROM whateverTable", connection);
SqlCommand comm2g = new SqlCommand("SELECT * FROM whateverTable WHERE condition = @0", connection);
commProcessing.Parameters.AddWithValue("@0", "value");
someDataSet.Tables.Add("Table1");
someDataSet.Tables.Add("Table2");
adapt.SelectCommand = comm1;
adapt.Fill(someDataSet.Tables["Table1"]);
adapt.SelectCommand = comm2;
adapt.Fill(someDataSet.Tables["Table2"]);
}
Answer 4:
这是你的问题很好的答案
见上面的MSDN页提到的例子: -
Answer 5:
public DataSet GetDataSet()
{
try
{
DataSet dsReturn = new DataSet();
using (SqlConnection myConnection = new SqlConnection(Core.con))
{
string query = "select * from table1; select* from table2";
SqlCommand cmd = new SqlCommand(query, myConnection);
myConnection.Open();
SqlDataReader reader = cmd.ExecuteReader();
dsReturn.Load(reader, LoadOption.PreserveChanges, new string[] { "tableOne", "tableTwo" });
return dsReturn;
}
}
catch (Exception)
{
throw;
}
}
Answer 6:
方法Load
的DataTable
执行NextResult
在DataReader
,所以你不应该调用NextResult
使用时明确Load
,序列中另有奇表将被省略。
这里是一个通用的解决方案来加载使用多个表DataReader
。
// your command initialization code here
// ...
DataSet ds = new DataSet();
DataTable t;
using (DbDataReader reader = command.ExecuteReader())
{
while (!reader.IsClosed)
{
t = new DataTable();
t.Load(rs);
ds.Tables.Add(t);
}
}
Answer 7:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=.;uid=sa;pwd=123;database=shop");
//SqlCommand cmd = new SqlCommand("select * from tblemployees", con);
//SqlCommand cmd1 = new SqlCommand("select * from tblproducts", con);
//SqlDataAdapter da = new SqlDataAdapter();
//DataSet ds = new DataSet();
//ds.Tables.Add("emp");
//ds.Tables.Add("products");
//da.SelectCommand = cmd;
//da.Fill(ds.Tables["emp"]);
//da.SelectCommand = cmd1;
//da.Fill(ds.Tables["products"]);
SqlDataAdapter da = new SqlDataAdapter("select * from tblemployees", con);
DataSet ds = new DataSet();
da.Fill(ds, "em");
da = new SqlDataAdapter("select * from tblproducts", con);
da.Fill(ds, "prod");
GridView1.DataSource = ds.Tables["em"];
GridView1.DataBind();
GridView2.DataSource = ds.Tables["prod"];
GridView2.DataBind();
}
Answer 8:
string connetionString = null;
SqlConnection connection ;
SqlCommand command ;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
int i = 0;
string firstSql = null;
string secondSql = null;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
firstSql = "Your First SQL Statement Here";
secondSql = "Your Second SQL Statement Here";
connection = new SqlConnection(connetionString);
try
{
connection.Open();
command = new SqlCommand(firstSql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds, "First Table");
adapter.SelectCommand.CommandText = secondSql;
adapter.Fill(ds, "Second Table");
adapter.Dispose();
command.Dispose();
connection.Close();
//retrieve first table data
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]);
}
//retrieve second table data
for (i = 0; i <= ds.Tables[1].Rows.Count - 1; i++)
{
MessageBox.Show(ds.Tables[1].Rows[i].ItemArray[0] + " -- " + ds.Tables[1].Rows[i].ItemArray[1]);
}
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
文章来源: How to fill Dataset with multiple tables?