返回多个结果集使用datadreader [英] Returning multiple resultsets usind datadreader

查看:74
本文介绍了返回多个结果集使用datadreader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此编码通过datareader获取2个结果集,然后将它们加载到2个数据表中并将数据表绑定到datagridviews。但

sdrGrid.NextResult()由于某种原因返回false。是否可能

连接已关闭?

Dim strConn As String =" Server = localhost; Database = northwind;" + _

集成安全性= SSPI

Dim cnnNwind作为SqlConnection =新的SqlConnection(strConn)

尝试

Dim strSql As String =" select * from customers; select * from

products"

Dim cmdGrid As New SqlCommand(strSql,cnnNwind)

cmdGrid.CommandType = CommandType.Text

cnnNwind.Open()

Dim sdrGrid As SqlDataReader = cmdGrid.ExecuteReader

Dim dt1 As新的DataTable

dt1.Load(sdrGrid)

DGVP.DataSource = dt1

Dim dt2 As New DataTable

如果sdrGrid.NextResult()= True那么

dt2.Load(sdrGrid)

DGVC.DataSource = dt2

结束如果

Catch exc As Exception

MsgBox(exc.Message)

最后

cnnNwind.Close()

结束尝试

I''m using this coding to get 2 resultsets thru datareader and then load them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextResult() is returning false for some reason. Is that possible
the connection is closed?

Dim strConn As String = "Server=localhost;Database=northwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(strConn)
Try
Dim strSql As String = "select * from customers;select * from
products"
Dim cmdGrid As New SqlCommand(strSql, cnnNwind)
cmdGrid.CommandType = CommandType.Text
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.ExecuteReader
Dim dt1 As New DataTable
dt1.Load(sdrGrid)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextResult() = True Then
dt2.Load(sdrGrid)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Message)
Finally
cnnNwind.Close()
End Try

推荐答案

彼得,


你能解释一下为什么你使用这种方法,我现在可以看到它,它是
是从纽约到Wasinton到泽西市的LA。


通常我们直接使用DataAdapter Fill,但是应该有一个

的原因?

它不能更快​​,因为那是AFAIK的罪魁祸首,你必须

写出至少更多的代码而结果是相同的,虽然你需要

可能在你现在做的事情之后至少接受一个接受改变,当然是

由填充自动完成。


但是,必须有一个理由和我很讨厌吗?


Cor


" Peter" < pe *** @ discussion.microsoft.comschreef在bericht

新闻:DF ************************* ********* @ microsof t.com ...
Peter,

Can you explain me why you use this method, as far as I can see it now, it
is going from NY to Wasinton to Jersey City over LA.

Normally we use direct the DataAdapter Fill, however there should be a
reason?
It cannot be that it is quicker, because that is AFAIK culprit, you have to
write at least more code while the result is the same, although you need
probably after what you do now at least an acceptchanges of course, which is
automaticly done by the fill.

However, there must be a reason and I am currious about that?

Cor

"Peter" <Pe***@discussions.microsoft.comschreef in bericht
news:DF**********************************@microsof t.com...

我正在使用此编码通过datareader获取2个结果集然后加载

他们

到2个数据表并将数据表绑定到datagridviews。但

sdrGrid.NextResult()由于某种原因返回false。这是

可能

连接关闭了吗?

Dim strConn As String =" Server = localhost; Database = northwind ;" + _

集成安全性= SSPI

Dim cnnNwind作为SqlConnection =新的SqlConnection(strConn)

尝试

Dim strSql As String =" select * from customers; select * from

products"

Dim cmdGrid As New SqlCommand(strSql,cnnNwind)

cmdGrid.CommandType = CommandType.Text

cnnNwind.Open()

Dim sdrGrid As SqlDataReader = cmdGrid.ExecuteReader

Dim dt1 As新的DataTable

dt1.Load(sdrGrid)

DGVP.DataSource = dt1

Dim dt2 As New DataTable

如果sdrGrid.NextResult()= True那么

dt2.Load(sdrGrid)

DGVC.DataSource = dt2

结束如果

Catch exc As Exception

MsgBox(exc.Message)

最后

cnnNwind.Close()

结束尝试
I''m using this coding to get 2 resultsets thru datareader and then load
them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextResult() is returning false for some reason. Is that
possible
the connection is closed?

Dim strConn As String = "Server=localhost;Database=northwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(strConn)
Try
Dim strSql As String = "select * from customers;select * from
products"
Dim cmdGrid As New SqlCommand(strSql, cnnNwind)
cmdGrid.CommandType = CommandType.Text
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.ExecuteReader
Dim dt1 As New DataTable
dt1.Load(sdrGrid)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextResult() = True Then
dt2.Load(sdrGrid)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Message)
Finally
cnnNwind.Close()
End Try


你好Cor,


我我只是尝试不同的方法来检索数据:datareader,dataadapter,

tableadapter,DAAB和LINQ。


其中一个目标是检索master -details结果集并将它们填充到datagridviews中以获得只读目的。这可能是作为一般用法类实现的。


彼得


" Cor Ligthert [ MVP] QUOT;写道:
Hi Cor,

I''m just trying different ways to retrieve data: datareader, dataadapter,
tableadapter, DAAB , and LINQ.

One of the objectives is to retrieve master-details resultsets and populate
them into datagridviews for read-only purpose. This probably will be
implemented as a class for generic usage.

Peter

"Cor Ligthert[MVP]" wrote:

彼得,


你能解释一下为什么你使用这种方法,据我现在所见,它b / b
从纽约到Wasinton再到LA市的泽西市。


通常我们直接使用DataAdapter Fill,但是应该有一个

原因?

它不能更快​​,因为这是AFAIK的罪魁祸首,你必须要写b
至少写更多代码,而结果是同样,虽然你需要

,但是你现在做的事情至少是一个接受改变,当然是

由填充自动完成。


然而,必须有一个理由,我对此感到好奇吗?


Cor


" Peter" < pe *** @ discussion.microsoft.comschreef在bericht

新闻:DF ************************* ********* @ microsof t.com ...
Peter,

Can you explain me why you use this method, as far as I can see it now, it
is going from NY to Wasinton to Jersey City over LA.

Normally we use direct the DataAdapter Fill, however there should be a
reason?
It cannot be that it is quicker, because that is AFAIK culprit, you have to
write at least more code while the result is the same, although you need
probably after what you do now at least an acceptchanges of course, which is
automaticly done by the fill.

However, there must be a reason and I am currious about that?

Cor

"Peter" <Pe***@discussions.microsoft.comschreef in bericht
news:DF**********************************@microsof t.com...

我正在使用此编码通过datareader获取2个结果集然后加载

他们

到2个数据表并将数据表绑定到datagridviews。但

sdrGrid.NextResult()由于某种原因返回false。这是

可能

连接关闭了吗?

Dim strConn As String =" Server = localhost; Database = northwind ;" + _

集成安全性= SSPI

Dim cnnNwind作为SqlConnection =新的SqlConnection(strConn)

尝试

Dim strSql As String =" select * from customers; select * from

products"

Dim cmdGrid As New SqlCommand(strSql,cnnNwind)

cmdGrid.CommandType = CommandType.Text

cnnNwind.Open()

Dim sdrGrid As SqlDataReader = cmdGrid.ExecuteReader

Dim dt1 As新的DataTable

dt1.Load(sdrGrid)

DGVP.DataSource = dt1

Dim dt2 As New DataTable

如果sdrGrid.NextResult()= True那么

dt2.Load(sdrGrid)

DGVC.DataSource = dt2

结束如果

Catch exc As Exception

MsgBox(exc.Message)

F inally

cnnNwind.Close()

结束尝试
I''m using this coding to get 2 resultsets thru datareader and then load
them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextResult() is returning false for some reason. Is that
possible
the connection is closed?

Dim strConn As String = "Server=localhost;Database=northwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(strConn)
Try
Dim strSql As String = "select * from customers;select * from
products"
Dim cmdGrid As New SqlCommand(strSql, cnnNwind)
cmdGrid.CommandType = CommandType.Text
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.ExecuteReader
Dim dt1 As New DataTable
dt1.Load(sdrGrid)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextResult() = True Then
dt2.Load(sdrGrid)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Message)
Finally
cnnNwind.Close()
End Try


Hello Peter,


你是否在连接字符串中将MARS参数设置为true?

Dim objConn As New SqlConnection(" MultipleActiveResultSets = True; Persi st

安全信息=假;用户ID = sa;密码= pwd;初始目录= tempdatabase")

" Peter" < pe *** @ discussion.microsoft.comschreef在bericht

新闻:DF ************************* ********* @ microsof t.com ...
Hello Peter ,

did you set the MARS parameter to true in the connection string ?

Dim objConn As New SqlConnection("MultipleActiveResultSets=True;Persi st
Security Info=False;User ID=sa;Password=pwd;Initial Catalog=tempdatabase")
"Peter" <Pe***@discussions.microsoft.comschreef in bericht
news:DF**********************************@microsof t.com...

我正在使用此编码通过datareader获取2个结果集然后加载

他们

到2个数据表并将数据表绑定到datagridviews。但

sdrGrid.NextResult()由于某种原因返回false。这是

可能

连接关闭了吗?

Dim strConn As String =" Server = localhost; Database = northwind ;" + _

集成安全性= SSPI

Dim cnnNwind作为SqlConnection =新的SqlConnection(strConn)

尝试

Dim strSql As String =" select * from customers; select * from

products"

Dim cmdGrid As New SqlCommand(strSql,cnnNwind)

cmdGrid.CommandType = CommandType.Text

cnnNwind.Open()

Dim sdrGrid As SqlDataReader = cmdGrid.ExecuteReader

Dim dt1 As新的DataTable

dt1.Load(sdrGrid)

DGVP.DataSource = dt1

Dim dt2 As New DataTable

如果sdrGrid.NextResult()= True那么

dt2.Load(sdrGrid)

DGVC.DataSource = dt2

结束如果

Catch exc As Exception

MsgBox(exc.Message)

最后

cnnNwind.Close()

结束尝试
I''m using this coding to get 2 resultsets thru datareader and then load
them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextResult() is returning false for some reason. Is that
possible
the connection is closed?

Dim strConn As String = "Server=localhost;Database=northwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(strConn)
Try
Dim strSql As String = "select * from customers;select * from
products"
Dim cmdGrid As New SqlCommand(strSql, cnnNwind)
cmdGrid.CommandType = CommandType.Text
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.ExecuteReader
Dim dt1 As New DataTable
dt1.Load(sdrGrid)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextResult() = True Then
dt2.Load(sdrGrid)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Message)
Finally
cnnNwind.Close()
End Try



这篇关于返回多个结果集使用datadreader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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