连接到数据库中的多个表 [英] Connect to more than one table in database

查看:94
本文介绍了连接到数据库中的多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有2个分贝表(TableOne,TableTwo).我需要比较TableOne中的代码在TableTwo中是否可用.以下是我创建命令的方式..猜测它是错误的.

Hi guys,
I''m having 2 db table(TableOne,TableTwo). I need to compare whether the Code in TableOne available in TableTwo. Below is how I created the command..guess it is wrong.

SqlCommand cmdOne = new SqlCommand("select Code from TableOne", conn);
rdrOne = cmdOne.ExecuteReader();

SqlCommand cmdTwo = new SqlCommand("select ID from TableTwo", conn);
rdrTwo = cmdTwo.ExecuteReader();

while (rdrOne.Read())
{
     string Code1 = (string)rdrOne["Code"];
     while (rdrTwo.Read())
     {
         string Code2 = (string)rdrTwo["ID"];
     }
}


我在粗体显示错误:
已经有与此命令关联的打开的DataReader,必须先关闭它."

有人可以帮我吗?

Skunkhead:)


I''m getting error at the bold line :
"There is already an open DataReader associated with this Command which must be closed first."

Could anyone help me out?

Skunkhead :)

推荐答案

您必须先关闭第一个阅读器,然后才能使用第二个阅读器......

其背后的原因是,每个连接只能同时激活一个datareader.
因此,您不仅需要创建另一个阅读器,还需要创建另一个
连接以同时使用其中两个.

代替您正在执行的操作,您可以使用DataSet并将两个表都检索到其中....

然后只需比较两个表值,如

you have first close your first reader then only you can use the second one ......

The reason behind this is,only one datareader per connection can be active at the same time.
So, you will have to create not only another reader but also another
connection to use two of them at the same time.

Instead of that what you are doing you can use DataSet and Retrieve both table into it....

And then simply compare both table values like,

ds.Table[0].rows[0]["Column_name"] == ds.Table[1].rows[0]["Column_name"]



希望对您有所帮助.



I hope this will help you.


您好,朋友普拉蒂克说.更好地使用数据集或联接是执行这些操作的更好方法.希望这一方法对您有所帮助
尝试dis
hello friend as pratik said. better go for dataset or join is the better way to do these things... hope this one will help u
try dis


好吧,这是因为DataReader是连接模式的,只能转发东西.只要阅读器处于打开状态,连接就处于活动状态并正在工作.因此,如果您尝试使用其他阅读器,则会出现错误.

看看这些:
建议采用变通方法 [类似的讨论 [此处 [
Well, it''s because DataReaders are connected-mode, forward only thing. As long as a reader is open, connection is active and working on it. Thus if you try to use another reader then, you get the error.

Have a look at these:
MSDN blog describing the same in much more detail[^]
Workaround suggested with implication[^]
Similar discussion[^]

If needed, look here[^] for more.


这篇关于连接到数据库中的多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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