使用sqldatareader中的行填充sqldatatable [英] Populate a sqldatatable with a row from sqldatareader

查看:168
本文介绍了使用sqldatareader中的行填充sqldatatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有点问题,我需要您的支持..

这种情况​​:

我做了一个查询使用sqlcommand ed一个slqdatareader来读取结果,foreach行来自这个查询,我需要进行另一个查询。

我写了一个while循环,如果datareader有行,而datareader read,我需要在另一个表中执行另一个查询,使用datareader [0]值作为参数,我会在表中插入每一行;当循环结束时,该表将成为数据网格的数据源。我的问题是如何从表格中的sqldatareader插入每一行?

语言是C#。

提前致谢。

Domenico 。



我尝试了什么:



我用谷歌搜索了,我我试过varius解决方案,但没有什么适合我的问题。

Hello,
I've a little problem, I need your support..
This is the situation:
I made a query using a sqlcommand ed a slqdatareader to read the result, foreach row resulting from this query, I need to make another query.
I've write a while loop, if the datareader has rows, while datareader read, I need to do another query in a different table, using the datareader[0] value as parameter, and I would insert every row in a table; when loop has ended, the table will be the data source of a data grid. My question is how can I insert every single row from the sqldatareader in the table ?
Language is C#.
Thanks in advance.
Domenico.

What I have tried:

I googled and I've tried varius solutions, but nothing fit to my problem.

推荐答案





更多一般来说,如果你正在寻找将记录插入SQL数据库,你可以在下面的链接中找到一个很好的,非常好的解释文章:



使用C#进行简单的ADO.NET数据库读取,插入,更新和删除。 [ ^ ]



使用ado.net通过C#将值插入SQL Server数据库 - Stack Overflow [ ^ ]



确保您知道如何通过ADO.Net对数据库进行CRUD后,我们可以继续执行以下步骤:



您现在需要在sqlDataReader中存储查询结果,您需要创建包含相同列的自定义 DataTable DataColumns )你的sqlDataReader包含然后使用 Rows 属性 DataTable 逐个添加记录一个,最后将 DataTable 绑定到DataGridView。



Hi,

In a more general way if you are looking for inserting a record into a SQL database you can find a nice and very well explain article in the below links:

Simple ADO.NET Database Read, Insert, Update and Delete using C#.[^]

Inserting values into a SQL Server database using ado.net via C# - Stack Overflow[^]

After making sure that you know how to do CRUD on a db via ADO.Net then we can continue with the following steps:

You stored the result of query in a sqlDataReader now you need to create a custom DataTable that contains the same columns (DataColumns) that your sqlDataReader contains and then use Rows property of DataTable to add records one-by-one and at the end bind your DataTable to your DataGridView.

var dataTable = new DataTable();
dataTable.Columns.Add("Col1");
dataTable.Columns.Add("Col2");
dataTable.Rows.Add(new object[]{"Rod", "Stephens"});
var bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
DataGridView1.DataSource = bindingSource;





更完整的例子可以在以下链接找到:

构建DataTable和将它绑定到C#C#Helper 中的DataGridView [ ^ ]





如果这并没有解决你的问题然后请发表评论,我会帮助你改善我的解决方案直到你的问题得到解决。



干杯,

AH



A more complete example can be found at the following link:
Build a DataTable and bind it to a DataGridView in C#C# Helper[^]


If this did not solve your problem then please leave a comment and I will assist you by improving my solution until your problem gets solved.

Cheers,
AH


有两种方法可以做到这一点

1)当你得到sqldatareader对象然后读取该对象如果读者有数据你又做另一个查询在读者对象的基础上,您再次获得了读者对象,您需要创建Datarow对象并插入读者对象值到datarow然后将datarow添加到datatable.finally将datatable分配给grid

2)创建一个存储过程,创建一个游标。将数据收集到cursor中。逐行删除游标然后写另一个查询使用游标行的参数将数据收集到temp变量中然后返回临时变量。
There are 2 way to do this
1)When you got the sqldatareader object then read that object if reader has data the you do another query on basis of reader object the again you got the reader object that time you need to create the Datarow object and insert the reader object values into datarow then add datarow into datatable.finally assign datatable to grid
2)Create 1 stored procedure into which create one cursor.Collect the data into cursor.Read the cursor row by row then write the another query with parameter of cursor row the collect the data into temp variable then return the temp variable.


谢谢,

但这不是我正在寻找的对于;我知道如何在SQL数据库上进行CRUD操作。我的问题是另一个。我对数据库表进行查询,并对其返回的每条记录进行查询,我需要对不同的表执行另一个查询,并将每个结果插入到sqldatatable中,并将此最后一个表用作网格视图的数据源。此表不是SQL数据库对象。

问候。

DR。
Thank you,
but this isn't what I'm looking for; I know how to make a CRUD operations on SQL database. My problem is another. I make a query on a database table and with every record returned by it, I need to perform another query to different table and insert every result in a sqldatatable, and use this last table as data source for a grid view. This table isn't an SQL database object.
Regards.
DR.


这篇关于使用sqldatareader中的行填充sqldatatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆