473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Closing of OleDbConnection and OleDbDataReader. Is there a better way?

Mel
I am performing the same recordset multiple times, just passing
different parameters each time. Is there a way to do this more
efficiently without having to close and re-open the connection and
reader? I thought there might be a cleaner way, this code seems to
take "forever" to run.

'-------BEGINNING OF CODE EXAMPLE-------
Dim SBInvNum As String
Dim strConInv As String = "Provider=Microsoft.JET.OLEDB.4.0;Data
Source =" & "\\myserver\invwhs.mdb"
Dim strRecInv As String = "SELECT * FROM
[invwhs_detail_local_with_desc] WHERE [whs] = ? and [ITEM_NBR]
LIKE ?;"
Dim conInv As New System.Data.OleDb.OleDbConnection(strConInv)
Dim comInv As New System.Data.OleDb.OleDbCommand(strRecInv, conInv)
Dim recInv As System.Data.OleDb.OleDbDataReader
For x = 0 To UBound(PartsArray, 2)
SBInvNum = GetInvNum(PartsArray(0, x), PartsArray(1, x),
PartsArray(2, x)) 'generate part number
conInv.Open()

'see if the part is available at all plants, if it isn't clear the
plant from the array.
For j = 0 To i
comInv.Parameters.AddWithValue("P1", GoodPlants(0, j))
comInv.Parameters.AddWithValue("P2", SBInvNum & "%")
recInv = comInv.ExecuteReader
If Not recInv.Read() Then
GoodPlants(0, j) = ""
GoodPlants(1, j) = ""
GoodPlants(2, j) = ""
GoodPlants(3, j) = ""
End If
recInv.Close() 'IS THERE A BETTER WAY? INSTEAD OF CLOSING AND
RE-OPENING?
comInv.Parameters.Clear()
Next j
conInv.Close()
Next x
'-------END OF CODE EXAMPLE-------

Aug 2 '07 #1
1 2055
Hello Mel,

You could just close the reader (you need to pass it a special parameter
so it won't close the connection while it's at it) and open a new reader
on the same connection. You can use the same command as before if you want
to.

By the way. OleDBCommand also implements IDisposable and should be disposed
when you're done with it.

I prefer using the using clause in C#, there's probably a similar thing in
VB.NET. The syntax in C# is:

using(OleDBConnection conInv = new OleDBConnection(...))
{

}

this makes sure that when the end } is passed, close will be called automatically.
Even if an exception occurs. Makes your code a lot easier to read if you
use it consistently.

Jesse
I am performing the same recordset multiple times, just passing
different parameters each time. Is there a way to do this more
efficiently without having to close and re-open the connection and
reader? I thought there might be a cleaner way, this code seems to
take "forever" to run.

'-------BEGINNING OF CODE EXAMPLE-------
Dim SBInvNum As String
Dim strConInv As String = "Provider=Microsoft.JET.OLEDB.4.0;Data
Source =" & "\\myserver\invwhs.mdb"
Dim strRecInv As String = "SELECT * FROM
[invwhs_detail_local_with_desc] WHERE [whs] = ? and [ITEM_NBR]
LIKE ?;"
Dim conInv As New System.Data.OleDb.OleDbConnection(strConInv)
Dim comInv As New System.Data.OleDb.OleDbCommand(strRecInv, conInv)
Dim recInv As System.Data.OleDb.OleDbDataReader
For x = 0 To UBound(PartsArray, 2)
SBInvNum = GetInvNum(PartsArray(0, x), PartsArray(1, x),
PartsArray(2, x)) 'generate part number
conInv.Open()
'see if the part is available at all plants, if it isn't clear the
plant from the array.
For j = 0 To i
comInv.Parameters.AddWithValue("P1", GoodPlants(0, j))
comInv.Parameters.AddWithValue("P2", SBInvNum & "%")
recInv = comInv.ExecuteReader
If Not recInv.Read() Then
GoodPlants(0, j) = ""
GoodPlants(1, j) = ""
GoodPlants(2, j) = ""
GoodPlants(3, j) = ""
End If
recInv.Close() 'IS THERE A BETTER WAY? INSTEAD OF CLOSING AND
RE-OPENING?
comInv.Parameters.Clear()
Next j
conInv.Close()
Next x
'-------END OF CODE EXAMPLE-------

Aug 2 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Grant | last post by:
My C# web application connects to an Access database using the OleDbConnection and OleDbDataReader. I have 3 other computers that connect to this server but very frequently I get an 'Unspecified...
2
by: Grant | last post by:
My C# web application connects to an Access database using the OleDbConnection and OleDbDataReader. I have 3 other computers that connect to this server but very frequently I get an 'Unspecified...
4
by: Jeff User | last post by:
Hi all I am using an OleDbDataReader. I need to establish and then keep the connection that I use, but I do not need to keep the data reader, after this operation is over. Therefore, regardless...
1
by: Keith | last post by:
Hi, if have this method in a cs file that connections to a database and returns a datareader to my web form. public OleDbDataReader GetDataReader(string theSQL) { theConn = new...
0
by: Brent | last post by:
I wrote a simple class to supply a datareader object (code at bottom of post) back to a page. I access it on the APSX page like this: Dim myDR as New getDataReader(strSQL) If myDR.ResultFailed...
3
by: Ludvig | last post by:
This Exception is executed when using SQLConnection. OleDbConnection connects fine. System.EnterpriseServices.Platform.Initialize() +503...
7
by: Arsalan | last post by:
I have a function which return datareader Public Shared Function ReturnDReader(ByVal query As String) As OleDbDataReader Dim Connection_String As String =...
6
by: bokiteam | last post by:
Hi All, I have this error msg, but I have already import lib, could you please advice? Imports System.Data.OleDb Public Class Form1 Inherits System.Windows.Forms.Form Dim cn As...
1
by: Jerry Spence1 | last post by:
I have been doing the following is VB.Net 2003: Dim dr1 As OleDbDataReader .....Code here If Not dr1 Is Nothing Then
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.