简体   繁体   English

如何从SQL Server中的表中循环数据以及如何使用返回的数据查询另一个表。 请参见

[英]How to loop data from a table in sql server and using the returned data to query another table. Please see

This is what I am trying to achieve : 这是我要实现的目标:

I have a table in a database in which there is a column named "item_code". 我在数据库中有一个表,其中有一个名为“ item_code”的列。

I need to query this table, returning ONE row at a time. 我需要查询该表,一次返回一行。 Now, I need to use the value of the column "item_code" for the one row which has been returned to query ANOTHER table where I'll use it to fetch a bunch of row(s). 现在,我需要对返回到查询ANOTHER表的一行使用“ item_code”列的值,在这里我将使用它来获取一堆行。 How do I do this? 我该怎么做呢?

I tried using a datareader object in a while loop, fetch one row at a time and then query the other table inside this loop to fetch the rows required but I couldn't figure out how to put this data in a gridview (use datatable? if yes, how?) in such a way that the previous rows in the gridview don't get erased after each iteration of the while loop. 我尝试在while循环中使用datareader对象,一次获取一行,然后查询该循环内的另一张表以获取所需的行,但是我不知道如何将这些数据放入gridview中(使用datatable?如果是,怎么办?)的方式是,在while循环的每次迭代之后都不会擦除gridview中的前几行。

The only way I know for putting data into a gridview is by using .Fill() but obviously, Fill method wouldn't do in this case as it would wipe out the previous entries in the gridview. 我知道的将数据放入gridview的唯一方法是使用.Fill(),但是显然,在这种情况下Fill方法不会这样做,因为它将清除gridview中的先前条目。

Please help. 请帮忙。

I need to query this table, returning ONE row at a time. 我需要查询该表,一次返回一行。 Now, I need to use the value of the column "item_code" for the one row which has been returned to query ANOTHER table where I'll use it to fetch a bunch of row(s). 现在,我需要对返回到查询ANOTHER表的一行使用“ item_code”列的值,在这里我将使用它来获取一堆行。 How do I do this? 我该怎么做呢?

You could use a single SQL query that joins the two tables on "item_code" and retrieves the results from the second table. 您可以使用单个SQL查询将“ item_code”上的两个表连接起来,然后从第二个表中检索结果。

Your solution will work, but you are correct, Fill() will erase the contents of the table. 您的解决方案将起作用,但是您是正确的,Fill()将擦除表中的内容。 Instead, use Merge() 而是使用Merge()

var myMainTable = new DataTable();

foreach(var itemId in itemIds)
{
  var currentTable = new DataTable();
  // submit new query
  myAdapter.Fill(currentTable)
  myMainTable.Merge(currentTable);
}

You can make it in SQL level in one step, like this: 您可以一步一步将其设置为SQL级别,如下所示:

SELECT Table2.* 
FROM Table2
INNER JOIN Table1 ON Table1.item_code = Table2.item_code
ORDER BY Table2.item_code

And of course if you need to make a smaller list, you can wirte the WHERE too. 当然,如果您需要做一个较小的列表,也可以在WHERE进行筛选。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Transact-SQL上:可以构建一条语句来创建新列,同时使用所有来自同一表的另一列的数据。 - On Transact-SQL: possible to build a statement to create a new column while using the data of another column all from the same table. 如何从另一个服务器的数据库表中循环数据? - How to loop data from a table of a database from another server? 如何使用.NET Framework将数据添加到SQL Server中另一个表的列中? - How do I add data to column from another table in a SQL Server using .NET Framework? 如何在SQL Server 2008中使用子查询从表中获取数据? - How to get data from table using sub query in sql server 2008? 如何将数据从特定表中的一列复制到SQL Server中另一表中的相同命名列 - How to copy data from one column in certain table to same named column in another table in SQL Server 当数据来自while循环函数并显示在列表视图中时,如何在SQL Server表中存储数据? - How to store data in a SQL Server table when the data comes from a while loop function and shown in a list view? 从另一个表中选择数据SQL Select查询 - Selecting Data from another table SQL Select Query 查询以根据日期条件从SQL Server表返回数据 - Query to return data from SQL Server table based on date criteria 我想使用 SQL Server 中的存储过程将数据从一个表同时插入到另一个表中 - I want to insert data from one table into another at same time using stored procedure in SQL server 如何让C#Query组件识别从sql存储过程中的临时表返回的数据列 - How do I get the C# Query component to recognise columns returned data from a temporary table in a sql stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM