473,757 Members | 9,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reusing same reader object throughout lifetime of app

Hi,

I am fairly new to .net and objects.

I learned to create a reader object in method 1, however if I wanted
to create multiple select queries in the same module I did not know
how to reuse the same code, I would like to verify if method 2 is the
right way to reuse the same reader object.

If this is the case could I open a reader object (that is global) at
the beginning of my program and just change run a .commandtext
and .executereader commands whenever I wanted to load it with
different data. My database is single user and contains very small
datasets.

(method 1)
Dim SQL As String = "SELECT Max(ID) FROM TempInventory WHERE Test =
'SRT'"
Dim cmd As New OleDbCommand(SQ L, con)
Dim reader As OleDb.OleDbData Reader = cmd.ExecuteRead er()

Do While reader.Read
FindLastSRTNumb er = reader(0)
Loop

reader.Close()

(end method 1)

and/or

(method 2)

Dim SQL as String = "SELECT Max(ID) FROM TempInventory WHERE Test =
'SRT'"
Dim cmd As New OleDbCommand(sq l, con)
Dim reader As OleDbDataReader

reader = cmd.ExecuteRead er()

Do While reader.Read
. (whatever)
. (whatever)
Loop

reader.Close()

'2nd query in same module (private sub)
sql = "SELECT ID1, ID2 FROM TempInventory WHERE TestNumber =
'03097'
cmd.CommandText = sql
reader = cmd.ExecuteRead er()

Do While reader.Read

(end method 2)

Mar 27 '07 #1
1 4094

You need to create and dispose of you IDataReader objects as late (create)
and as soon (destroy) as you can.
Global DataReader object for reuse = BAD IDEA
...

On the other side of things, you can use the
(IDataReader.Ne xtResult();)
method.
http://sholliday.spaces.live.com/blog/
5/24/2006
Custom Objects/Collections and Tiered Development

CHeck out that example. Its in C#, but you'll be able to pick up the
principles.

AGain, I'm going to say this again so you're absolutely clear:

//quote
If this is the case could I open a reader object (that is global) at
the beginning of my program and just change run a .commandtext
and .executereader commands whenever I wanted to load it with
different data.
//end quote

This is a BAD idea. No matter how small your app or resultsets are.

"vbDavidC" <ch************ *@yahoo.comwrot e in message
news:11******** **************@ l77g2000hsb.goo glegroups.com.. .
Hi,

I am fairly new to .net and objects.

I learned to create a reader object in method 1, however if I wanted
to create multiple select queries in the same module I did not know
how to reuse the same code, I would like to verify if method 2 is the
right way to reuse the same reader object.

If this is the case could I open a reader object (that is global) at
the beginning of my program and just change run a .commandtext
and .executereader commands whenever I wanted to load it with
different data. My database is single user and contains very small
datasets.

(method 1)
Dim SQL As String = "SELECT Max(ID) FROM TempInventory WHERE Test =
'SRT'"
Dim cmd As New OleDbCommand(SQ L, con)
Dim reader As OleDb.OleDbData Reader = cmd.ExecuteRead er()

Do While reader.Read
FindLastSRTNumb er = reader(0)
Loop

reader.Close()

(end method 1)

and/or

(method 2)

Dim SQL as String = "SELECT Max(ID) FROM TempInventory WHERE Test =
'SRT'"
Dim cmd As New OleDbCommand(sq l, con)
Dim reader As OleDbDataReader

reader = cmd.ExecuteRead er()

Do While reader.Read
. (whatever)
. (whatever)
Loop

reader.Close()

'2nd query in same module (private sub)
sql = "SELECT ID1, ID2 FROM TempInventory WHERE TestNumber =
'03097'
cmd.CommandText = sql
reader = cmd.ExecuteRead er()

Do While reader.Read

(end method 2)

Mar 27 '07 #2

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

Similar topics

4
9060
by: Old Wolf | last post by:
#include <stdio.h> #include <stdarg.h> Is this safe: void foo(const char *fmt, ...) { va_list ap; va_start(ap,fmt);
100
5277
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
14
3166
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects it makes difference to explicitly put them to null after working with them is done - it somehow makes the GC collect them sooner, like here: void SomeFunc() { MyClass c = new MyClass();
16
13991
by: ed_p | last post by:
Hello, I have implemented the singleton pattern for a class to hold a SqlConnection object that will be used thruout the application to create commands. My application is a simple Windows Form Application that connects to a MSDE Database. I was reading the thread yesterday on this very same subject, but it only mentioned ASP.NET Applications. Can anyone tell me if it's a good idea
37
21686
by: kplkumar | last post by:
I want to get the memory address of an object. Can someone tell me how to do that in C#. Also, I tried using the unsafe and the "&" operator and could not acheive it. Can you tell me a good way of doing it? I am new to this and have no clue how to get the memory address.
3
1916
by: thomson | last post by:
Hi all, Can any one tell me what happens when an object is casted to an interface, somthing bit deeper, the case is i do have an interface Ifunction which has got one method display(), and i have implemented the interface in one class and i have implemented the display function in the class, So when i say ((Ifunction)object).display(), it calls the function
3
1889
by: mailtogops | last post by:
Hi, I often think over the const_cast operator provided by C++ and I feel usage of const_cast or the document availale for const_cast are cumbersome. C++ provides us, const qualifier which force a variable or an object remain constant throughout lifetime of the program. class Experiment
6
1774
by: kun1he2 | last post by:
Hmm... reusing VB.NET Form dialogs in other VB.NET projects seems very straight forward. However, I'm now trying to reuse these dialogs within an existing C-based app. The VB.NET project has already been configured for registration for COM interop, so next question is how do I get retrieve/invoke the dialog's interfaces/properties?
6
2696
by: better_cs_now | last post by:
Hello all, class Foo {/* Details don't matter */}; class Bar { public: Bar(): m_Foo(/* Construct a Foo however it wants to be constructed */); const Foo &GetFoo() const { return m_Foo; } private:
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.