继续将查询结果添加到数据表
Keep adding query results to datatable
我有一个包含两个表的数据集。 Table[0] 被多行填充。然后我需要迭代这些行并为每一行 运行 另一个查询并将这些结果填充到 Table[1]。在循环中用多行填充数据集的正确方法是什么?我所在位置的缩写代码示例:
DataSet ds = new DataSet();
sql = myquery1;
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds, "Products");
foreach (DataRow dr in ds.Tables[0].Rows)
{
productID = ds.Tables[0].Rows[0]["ProductID"].ToString();
if (productID != String.Empty)
{
sql = String.Empty;
sql = myquery2 where productID = 'xxxxx'
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, "ProductProperties");
}
}
这会在第一个循环中填充 "ProductProperties" 数据表,但不会添加未来的结果。向数据表中不断添加多个结果集的正确方法是什么?
你查询应该return两个记录集
SELECT ProductId, Name
FROM Products
SELECT ProductId, Property1, Property2
FROM ProductProperties
然后您可以将上述查询中的数据读取到数据集,如下所示。因此,您将在数据集中获得两个表,其中包含上述查询的两个记录集的结果
using (SqlConnection con = new System.Data.SqlClient.SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "query";
cmd.Connection = con;
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
con.Close();
}
}
你的做法是错误的。您只需要一个查询来填充子 table。例如
Dim ds As New DataSet
Using connection As New SqlConnection("connection string here"),
parentAdapter As New SqlDataAdapter("SELECT * FROM ParentTable",
connection),
childAdapter As New SqlDataAdapter("SELECT * FROM ChildTable WHERE ParentID IN (SELECT ParentID FROM ParentTable)",
connection)
connection.Open()
parentAdapter.Fill(ds, "Parent")
parentAdapter.Fill(ds, "Child")
End Using
您需要做的就是将父查询复制为子查询 table。如果需要,您甚至可以将这两个查询放在一个数据适配器中。关键是不需要遍历父记录并为每个记录执行单独的查询。
我有一个包含两个表的数据集。 Table[0] 被多行填充。然后我需要迭代这些行并为每一行 运行 另一个查询并将这些结果填充到 Table[1]。在循环中用多行填充数据集的正确方法是什么?我所在位置的缩写代码示例:
DataSet ds = new DataSet();
sql = myquery1;
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds, "Products");
foreach (DataRow dr in ds.Tables[0].Rows)
{
productID = ds.Tables[0].Rows[0]["ProductID"].ToString();
if (productID != String.Empty)
{
sql = String.Empty;
sql = myquery2 where productID = 'xxxxx'
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, "ProductProperties");
}
}
这会在第一个循环中填充 "ProductProperties" 数据表,但不会添加未来的结果。向数据表中不断添加多个结果集的正确方法是什么?
你查询应该return两个记录集
SELECT ProductId, Name
FROM Products
SELECT ProductId, Property1, Property2
FROM ProductProperties
然后您可以将上述查询中的数据读取到数据集,如下所示。因此,您将在数据集中获得两个表,其中包含上述查询的两个记录集的结果
using (SqlConnection con = new System.Data.SqlClient.SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "query";
cmd.Connection = con;
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
con.Close();
}
}
你的做法是错误的。您只需要一个查询来填充子 table。例如
Dim ds As New DataSet
Using connection As New SqlConnection("connection string here"),
parentAdapter As New SqlDataAdapter("SELECT * FROM ParentTable",
connection),
childAdapter As New SqlDataAdapter("SELECT * FROM ChildTable WHERE ParentID IN (SELECT ParentID FROM ParentTable)",
connection)
connection.Open()
parentAdapter.Fill(ds, "Parent")
parentAdapter.Fill(ds, "Child")
End Using
您需要做的就是将父查询复制为子查询 table。如果需要,您甚至可以将这两个查询放在一个数据适配器中。关键是不需要遍历父记录并为每个记录执行单独的查询。