将Recordset转换为DataView [英] Converting Recordset to DataView

查看:68
本文介绍了将Recordset转换为DataView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据之前的帖子,我编写了一个将

记录集转换为数据视图的函数。对给定

记录集的函数的第一次调用工作正常,但第二次调用总是返回一个

数据视图,其中count = 0.有人可以解释为什么以及如何我可能

解决这个问题?

这是我的函数的代码:

公共共享函数GetViewFromRS(ByVal pRS as ADODB.Recordset) _

As DataView


Dim sAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter

Dim sTa​​ble As System.Data.DataTable =新的DataTable


sAdapter.Fill(sTable,pRS)


GetViewFromRS =新的DataView(sTable)


结束函数


我调用函数传递从SQL Server

存储过程创建的记录集,其中包含以下属性:

CursorLocation = adUseClient

CursorType = adOpenStatic

LockType = adLockBatchOptimistic


对函数的第二次调用始终返回datavie带计数的w

= 0.但是,我可以两次通过不同的

记录集调用该函数并且它成功,但每个记录集的第二次调用

失败。


这是我用来测试这个问题的函数:

公共共享函数RunTwice(ByVal pRS As ADODB.Recordset)

Dim sView1 As DataView = GetViewFromRS(pRS)

MessageBox.Show(" First count:" &安培; sView1.Count.ToString)

Dim sView2 As DataView = GetViewFromRS(pRS)

MessageBox.Show(" Second count:"& sView2.Count.ToString)

结束功能


有趣的是,当我通过

记录集的克隆时,问题仍然存在:

公共共享函数RunTwiceWithClone(ByVal pRS As ADODB.Recordset)

Dim sView1 As DataView = GetViewFromRS(pRS.Clone)

MessageBox.Show(" First count :&&;& sView1.Count.ToString)

Dim sView2 As DataView = GetViewFromRS(pRS.Clone)

MessageBox.Show(" Second count:&& ; sView2.Count.ToString)

结束功能


Kees VanTilburg

VanTilburg Enterprises

解决方案

基斯,

我不知道你为什么要将一个记录集复制到两个DataTables中,我将会b / b
建议您将单个记录集复制到单个DataTable,然后

在单个表上创建两个DataView。


类似于:

Dim sAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim sTa​​ble As System.Data.DataTable = New DataTable


sAdapter.Fill(sTable,pRS)


Dim sView1作为DataView = New DataView(sTable)

MessageBox.Show(" First count:" &安培; sView1.Count.ToString)


Dim sView2 As DataView = New DataView(sTable)

MessageBox.Show(" Second count:"& sView2 .Count.ToString)


如果你确实需要重复的数据副本,那么你可能想要在第一个OleDbDataAdapter时检查pRS的位置.Fill方法是用它完成的
,我怀疑它是EOF。因此第二个OleDbDataAdapter.Fill

去,EOF =没有数据,我已经完成了。否则我不确定因为我不使用

.NET中的记录集。


如果你没有它你可能要考虑得到David Sceppa的书

" Microsoft ADO.NET - Core Reference"来自MS Press这是一个非常好的学习ADO.NET的教程,以及使用ADO.NET获得

后的优秀桌面参考。


希望这有帮助

Jay


" kjvt" <做******** @ excite.com>在留言中写道

新闻:lb ******************************** @ 4ax.com ...

根据之前的帖子,我编写了一个将
记录集转换为数据视图的函数。对给定
记录集的第一次函数调用工作正常,但第二次调用总是返回一个count = 0的数据视图。有人可以解释为什么以及如何解决这个问题问题?

这是我的函数的代码:
公共共享函数GetViewFromRS(ByVal pRS as ADODB.Recordset)_
As DataView

Dim sAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim sTa​​ble As System.Data.DataTable = New DataTable

GetViewFromRS =新的DataView(sTable)

结束函数

我调用函数传递从SQL Server
存储过程创建的记录集,其中包含以下属性:
CursorLocation = adUseClient
CursorType = adOpenStatic
LockType = adLockBatchOptimistic

第二次调用函数总是返回一个带有计数的数据视图
= 0.但是,我可以通过不同的记录集并且它成功,但每个记录集的第二次调用失败。

这是我用来测试这个问题的函数:
公共共享函数RunTwice (ByVal pRS As ADODB.Recordset)
Dim sView1 As DataView = GetViewFromRS(pRS)
MessageBox.Show(" First count:" &安培; sView1.Count.ToString)
Dim sView2 As DataView = GetViewFromRS(pRS)
MessageBox.Show(" Second count:"& sView2.Count.ToString)
End Function

有趣的是,当我传递
记录集的克隆时,问题仍然存在:
公共共享函数RunTwiceWithClone(ByVal pRS as ADODB.Recordset)
Dim sView1 As DataView = GetViewFromRS(pRS.Clone)
MessageBox.Show(" First count:"& sView1.Count.ToString)
Dim sView2 As DataView = GetViewFromRS(pRS.Clone)
MessageBox。显示(" Second count:"& sView2.Count.ToString)
结束功能

Kees VanTilburg
VanTilburg Enterprises



Hi Kees,


你想要的是什么,


数据集即使你是一个断开连接的数据集从

记录集填充它,为什么不直接从数据库填充数据集,我o不是

理解它并且很好玩吗?


Cor


Cor,


我正在为现有的应用程序添加新的.Net功能,这个应用程序是在vb6中构建的,并且最近转换为.Net。该应用程序有一个数据访问层

内置在vb6中,目前尚未转换。与此同时,我想要使用.Net控件和数据绑定,所以我需要转换为

数据视图,可能会在记录集中的数据发生变化时多次转换。


Kees


" Cor" < no*@non.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...

嗨Kees ,

你想要实现的目标,

即使你从
记录集中填充数据集,数据集也会保持断开连接的数据集,为什么不直接从数据集中填充数据集。数据库,我不了解它并且很好吃吗?

Cor



Based on a prior posting, I''ve written a function to convert a
recordset to a dataview. The first call to the function for a given
recordset works perfectly, but the second call always returns a
dataview with a count = 0. Can someone explain why and how I might
work around this problem?
Here is the code for my function:
Public Shared Function GetViewFromRS(ByVal pRS As ADODB.Recordset) _
As DataView

Dim sAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim sTable As System.Data.DataTable = New DataTable

sAdapter.Fill(sTable, pRS)

GetViewFromRS = New DataView(sTable)

End Function

I call the function passing a recordset created from a SQL Server
stored procedure with the following properties:
CursorLocation = adUseClient
CursorType = adOpenStatic
LockType = adLockBatchOptimistic

The second call to the function always returns a dataview with a count
= 0. However, I can call the function twice passing different
recordsets and it succeeds, but the second call for each recordset
fails.

Here''s a function that I use to test this problem:
Public Shared Function RunTwice(ByVal pRS As ADODB.Recordset)
Dim sView1 As DataView = GetViewFromRS(pRS)
MessageBox.Show("First count: " & sView1.Count.ToString)
Dim sView2 As DataView = GetViewFromRS(pRS)
MessageBox.Show("Second count: " & sView2.Count.ToString)
End Function

Interestingly, the problem still occurs when I pass a clone of the
recordset:
Public Shared Function RunTwiceWithClone(ByVal pRS As ADODB.Recordset)
Dim sView1 As DataView = GetViewFromRS(pRS.Clone)
MessageBox.Show("First count: " & sView1.Count.ToString)
Dim sView2 As DataView = GetViewFromRS(pRS.Clone)
MessageBox.Show("Second count: " & sView2.Count.ToString)
End Function

Kees VanTilburg
VanTilburg Enterprises

解决方案

Kees,
I''m not sure why you want to copy a single recordset into two DataTables, I
would recommend you copy a single recordset to a single DataTable, then
create two DataViews over the single table.

Something like:

Dim sAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim sTable As System.Data.DataTable = New DataTable

sAdapter.Fill(sTable, pRS)

Dim sView1 As DataView = New DataView(sTable )
MessageBox.Show("First count: " & sView1.Count.ToString)

Dim sView2 As DataView = New DataView(sTable )
MessageBox.Show("Second count: " & sView2.Count.ToString)

If you really do need duplicate Copies of the data, then you may want to
check the position of pRS when the first OleDbDataAdapter.Fill method is
done with it, I suspect it is EOF. Hence the second OleDbDataAdapter.Fill
goes, EOF = no data, I''m done. Otherwise I''m not sure as I do not use
Recordsets in .NET.

If you don''t have it you may want to consider getting David Sceppa''s book
"Microsoft ADO.NET - Core Reference" from MS Press it is a very good
tutorial on learning ADO.NET as well as a good desk reference once your
using ADO.NET.

Hope this helps
Jay

"kjvt" <do********@excite.com> wrote in message
news:lb********************************@4ax.com...

Based on a prior posting, I''ve written a function to convert a
recordset to a dataview. The first call to the function for a given
recordset works perfectly, but the second call always returns a
dataview with a count = 0. Can someone explain why and how I might
work around this problem?
Here is the code for my function:
Public Shared Function GetViewFromRS(ByVal pRS As ADODB.Recordset) _
As DataView

Dim sAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim sTable As System.Data.DataTable = New DataTable

sAdapter.Fill(sTable, pRS)

GetViewFromRS = New DataView(sTable)

End Function

I call the function passing a recordset created from a SQL Server
stored procedure with the following properties:
CursorLocation = adUseClient
CursorType = adOpenStatic
LockType = adLockBatchOptimistic

The second call to the function always returns a dataview with a count
= 0. However, I can call the function twice passing different
recordsets and it succeeds, but the second call for each recordset
fails.

Here''s a function that I use to test this problem:
Public Shared Function RunTwice(ByVal pRS As ADODB.Recordset)
Dim sView1 As DataView = GetViewFromRS(pRS)
MessageBox.Show("First count: " & sView1.Count.ToString)
Dim sView2 As DataView = GetViewFromRS(pRS)
MessageBox.Show("Second count: " & sView2.Count.ToString)
End Function

Interestingly, the problem still occurs when I pass a clone of the
recordset:
Public Shared Function RunTwiceWithClone(ByVal pRS As ADODB.Recordset)
Dim sView1 As DataView = GetViewFromRS(pRS.Clone)
MessageBox.Show("First count: " & sView1.Count.ToString)
Dim sView2 As DataView = GetViewFromRS(pRS.Clone)
MessageBox.Show("Second count: " & sView2.Count.ToString)
End Function

Kees VanTilburg
VanTilburg Enterprises



Hi Kees,

What you want to archieve,

The dataset stays a disconnected dataset even if you fill it from the
recordset, why not fill the dataset direct from the database, I do not
understand it and am currious?

Cor


Cor,

I''m adding new .Net functionality to an existing application that was built
in vb6 and recently converted to .Net. The app has a data access layer
built in vb6 that is not being converted at this time. In the meantime, I
want to use .Net controls and databinding, so I need to convert to a
dataview, potentially multiple times as the data in the recordset changes.

Kees

"Cor" <no*@non.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

Hi Kees,

What you want to archieve,

The dataset stays a disconnected dataset even if you fill it from the
recordset, why not fill the dataset direct from the database, I do not
understand it and am currious?

Cor



这篇关于将Recordset转换为DataView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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