473,698 Members | 2,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic class which takes command string and returns datareader?

I'm trying to create a generic class which will take a command string and I
can issue statement like this:

Dim rdrCustomers As New Generic_Reader( "select * from customers")

However, I have not found anyway to do that. It seems that I may need to
create some functions instead of class and issue statement like this:

Dim rdrCustomers As SqlDataReader = GetDataReader(" select * from customers")
Aug 28 '07 #1
7 1816
On Aug 28, 12:30 am, Peter <Pe...@discussi ons.microsoft.c omwrote:
I'm trying to create a generic class which will take a command string and I
can issue statement like this:

Dim rdrCustomers As New Generic_Reader( "select * from customers")

However, I have not found anyway to do that. It seems that I may need to
create some functions instead of class and issue statement like this:

Dim rdrCustomers As SqlDataReader = GetDataReader(" select * from customers")
Huh?

You want to pass a connection string into a class constructor for a
datareader right?

How about:

Public Class MyDataReader
Inherits SqlDataReader

Public Sub New(queryString as String)
Dim conn As New SqlConnection(c onnString)
Dim com As SqlCommand = conn.CreateComm and()
com.CommandType = CommandType.Tex t
com.CommandText = queryString
Me = com.ExecuteRead er()
End Sub
End Class

I just typed that in the message, so it might not work properly, but
it should give you a start. Also I 100% recommend you have this class
implement IDisposable and manually dispose of the Connection and
Command objects from within the classes Dispose method.

Thanks,

Seth Rowe

Aug 28 '07 #2
On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups
<ro********@yah oo.comwrote:
>On Aug 28, 12:30 am, Peter <Pe...@discussi ons.microsoft.c omwrote:
>I'm trying to create a generic class which will take a command string and I
can issue statement like this:

Dim rdrCustomers As New Generic_Reader( "select * from customers")

However, I have not found anyway to do that. It seems that I may need to
create some functions instead of class and issue statement like this:

Dim rdrCustomers As SqlDataReader = GetDataReader(" select * from customers")

Huh?

You want to pass a connection string into a class constructor for a
datareader right?

How about:

Public Class MyDataReader
Inherits SqlDataReader

Public Sub New(queryString as String)
Dim conn As New SqlConnection(c onnString)
Dim com As SqlCommand = conn.CreateComm and()
com.CommandType = CommandType.Tex t
com.CommandText = queryString
Me = com.ExecuteRead er()
End Sub
End Class

I just typed that in the message, so it might not work properly, but
it should give you a start. Also I 100% recommend you have this class
implement IDisposable and manually dispose of the Connection and
Command objects from within the classes Dispose method.

Thanks,

Seth Rowe
You can't inherit from SqlDataReader. You could probably put the code
above in New() in a Shared function and return the DataReader.
Aug 28 '07 #3
On Aug 28, 10:04 am, Jack Jackson <jacknos...@peb bleridge.comwro te:
On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups

<rowe_em...@yah oo.comwrote:
On Aug 28, 12:30 am, Peter <Pe...@discussi ons.microsoft.c omwrote:
I'm trying to create a generic class which will take a command string and I
can issue statement like this:
Dim rdrCustomers As New Generic_Reader( "select * from customers")
However, I have not found anyway to do that. It seems that I may need to
create some functions instead of class and issue statement like this:
Dim rdrCustomers As SqlDataReader = GetDataReader(" select * from customers")
Huh?
You want to pass a connection string into a class constructor for a
datareader right?
How about:
Public Class MyDataReader
Inherits SqlDataReader
Public Sub New(queryString as String)
Dim conn As New SqlConnection(c onnString)
Dim com As SqlCommand = conn.CreateComm and()
com.CommandType = CommandType.Tex t
com.CommandText = queryString
Me = com.ExecuteRead er()
End Sub
End Class
I just typed that in the message, so it might not work properly, but
it should give you a start. Also I 100% recommend you have this class
implement IDisposable and manually dispose of the Connection and
Command objects from within the classes Dispose method.
Thanks,
Seth Rowe

You can't inherit from SqlDataReader. You could probably put the code
above in New() in a Shared function and return the DataReader.
Darn, I was hoping when I typed the message that SqlDataReader wasn't
a sealed class.

Thanks,

Seth Rowe

Aug 28 '07 #4
On Tue, 28 Aug 2007 07:16:04 -0700, rowe_newsgroups
<ro********@yah oo.comwrote:
>On Aug 28, 10:04 am, Jack Jackson <jacknos...@peb bleridge.comwro te:
>On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups

<rowe_em...@ya hoo.comwrote:
>On Aug 28, 12:30 am, Peter <Pe...@discussi ons.microsoft.c omwrote:
I'm trying to create a generic class which will take a command string and I
can issue statement like this:
>Dim rdrCustomers As New Generic_Reader( "select * from customers")
>However, I have not found anyway to do that. It seems that I may need to
create some functions instead of class and issue statement like this:
>Dim rdrCustomers As SqlDataReader = GetDataReader(" select * from customers")
>Huh?
>You want to pass a connection string into a class constructor for a
datareader right?
>How about:
>Public Class MyDataReader
Inherits SqlDataReader
Public Sub New(queryString as String)
Dim conn As New SqlConnection(c onnString)
Dim com As SqlCommand = conn.CreateComm and()
com.CommandType = CommandType.Tex t
com.CommandText = queryString
Me = com.ExecuteRead er()
End Sub
End Class
>I just typed that in the message, so it might not work properly, but
it should give you a start. Also I 100% recommend you have this class
implement IDisposable and manually dispose of the Connection and
Command objects from within the classes Dispose method.
>Thanks,
>Seth Rowe

You can't inherit from SqlDataReader. You could probably put the code
above in New() in a Shared function and return the DataReader.

Darn, I was hoping when I typed the message that SqlDataReader wasn't
a sealed class.

Thanks,

Seth Rowe
It's not sealed, but it doesn't have a public constructor, so as far
as I know it can't be inherited.
Aug 28 '07 #5
Thanks Seth. I wish that SqlDataReader is not a sealed class too.

"rowe_newsgroup s" wrote:
On Aug 28, 10:04 am, Jack Jackson <jacknos...@peb bleridge.comwro te:
On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups

<rowe_em...@yah oo.comwrote:
>On Aug 28, 12:30 am, Peter <Pe...@discussi ons.microsoft.c omwrote:
>I'm trying to create a generic class which will take a command string and I
>can issue statement like this:
>Dim rdrCustomers As New Generic_Reader( "select * from customers")
>However, I have not found anyway to do that. It seems that I may need to
>create some functions instead of class and issue statement like this:
>Dim rdrCustomers As SqlDataReader = GetDataReader(" select * from customers")
>Huh?
>You want to pass a connection string into a class constructor for a
>datareader right?
>How about:
>Public Class MyDataReader
Inherits SqlDataReader
Public Sub New(queryString as String)
Dim conn As New SqlConnection(c onnString)
Dim com As SqlCommand = conn.CreateComm and()
com.CommandType = CommandType.Tex t
com.CommandText = queryString
Me = com.ExecuteRead er()
End Sub
>End Class
>I just typed that in the message, so it might not work properly, but
>it should give you a start. Also I 100% recommend you have this class
>implement IDisposable and manually dispose of the Connection and
>Command objects from within the classes Dispose method.
>Thanks,
>Seth Rowe
You can't inherit from SqlDataReader. You could probably put the code
above in New() in a Shared function and return the DataReader.

Darn, I was hoping when I typed the message that SqlDataReader wasn't
a sealed class.

Thanks,

Seth Rowe

Aug 28 '07 #6


"Jack Jackson" wrote:
On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups
<ro********@yah oo.comwrote:
On Aug 28, 12:30 am, Peter <Pe...@discussi ons.microsoft.c omwrote:
I'm trying to create a generic class which will take a command string and I
can issue statement like this:

Dim rdrCustomers As New Generic_Reader( "select * from customers")

However, I have not found anyway to do that. It seems that I may need to
create some functions instead of class and issue statement like this:

Dim rdrCustomers As SqlDataReader = GetDataReader(" select * from customers")
Huh?

You want to pass a connection string into a class constructor for a
datareader right?

How about:

Public Class MyDataReader
Inherits SqlDataReader

Public Sub New(queryString as String)
Dim conn As New SqlConnection(c onnString)
Dim com As SqlCommand = conn.CreateComm and()
com.CommandType = CommandType.Tex t
com.CommandText = queryString
Me = com.ExecuteRead er()
End Sub
End Class

I just typed that in the message, so it might not work properly, but
it should give you a start. Also I 100% recommend you have this class
implement IDisposable and manually dispose of the Connection and
Command objects from within the classes Dispose method.

Thanks,

Seth Rowe

You can't inherit from SqlDataReader. You could probably put the code
above in New() in a Shared function and return the DataReader.
Aug 28 '07 #7
Hi Jack,

I think the ExecuteReader method in Data Access Application Block of
Enterprise Library is similar (actually much more) to what I'm trying to do.
Peter

"Jack Jackson" wrote:
On Tue, 28 Aug 2007 07:16:04 -0700, rowe_newsgroups
<ro********@yah oo.comwrote:
On Aug 28, 10:04 am, Jack Jackson <jacknos...@peb bleridge.comwro te:
On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups

<rowe_em...@yah oo.comwrote:
On Aug 28, 12:30 am, Peter <Pe...@discussi ons.microsoft.c omwrote:
I'm trying to create a generic class which will take a command string and I
can issue statement like this:

Dim rdrCustomers As New Generic_Reader( "select * from customers")

However, I have not found anyway to do that. It seems that I may need to
create some functions instead of class and issue statement like this:

Dim rdrCustomers As SqlDataReader = GetDataReader(" select * from customers")

Huh?

You want to pass a connection string into a class constructor for a
datareader right?

How about:

Public Class MyDataReader
Inherits SqlDataReader

Public Sub New(queryString as String)
Dim conn As New SqlConnection(c onnString)
Dim com As SqlCommand = conn.CreateComm and()
com.CommandType = CommandType.Tex t
com.CommandText = queryString
Me = com.ExecuteRead er()
End Sub
End Class

I just typed that in the message, so it might not work properly, but
it should give you a start. Also I 100% recommend you have this class
implement IDisposable and manually dispose of the Connection and
Command objects from within the classes Dispose method.

Thanks,

Seth Rowe

You can't inherit from SqlDataReader. You could probably put the code
above in New() in a Shared function and return the DataReader.
Darn, I was hoping when I typed the message that SqlDataReader wasn't
a sealed class.

Thanks,

Seth Rowe

It's not sealed, but it doesn't have a public constructor, so as far
as I know it can't be inherited.
Aug 28 '07 #8

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

Similar topics

3
2325
by: SimonH | last post by:
Hi all, I would like to make a generic set of methods that could be called regardless of the database behind the scenes. One of the methods I would like would take a string sql statement and an array of DataParameter objects. The problem i have is there doesnt seem to be a generic DataParameter class that I can instantiate. There only seems to be specific implementations like
0
1053
by: Christopher Benson-Manica | last post by:
(To the extent that this is a rehash of earlier questions, sorry.) I feel like I'm starting to get the hang of how to make classes work with << and >>, so now I have the (possibly dumb) idea to create generic interfaces that use these, making it easy to subclass our existing classes and make them behave a little better in the C++ world. I'm going to describe what I'm doing in some detail - feel free to interject criticism at any point,...
2
1148
by: gorden blom | last post by:
Hi, I'm a student who's working on a school project about ASP.NET / C#. I'm having some problems with my class. I want to create an ArrayList filled with results from a DataReader. It gives me the following error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
1
1100
by: Scott Collens | last post by:
Hello, I am having issues returning the results from the database using a data access class. The class is called dbconnect.vb. Imports System Imports System.Data Imports System.Data.SqlClient
2
2639
by: Bala Nagarajan | last post by:
Hello, I am working with a webservice developed by my team some time back and found a problem with it. All the web service methods returned a dataset instead of a more generic object . Clearly this is not the best way to develop a web service because it is so tightly coupled that only .NET clients can use it. The dataset returned by the web service has 1000's of records in it. My question is how can i map the records in dataset so it...
9
8326
by: craig.overton | last post by:
All, I am currently developing an FTP class in VB.NET. It's kid tested, mother approved when trying to access an FTP Server on a Windows box meaning I can connect, run commands, upload and download a file no problem. My issues come when I try to use the same class with the same commands to access an FTP server on a UNIX box. I can connect and login just fine, but after that all my commands come back "500 'PWD': command not understood."....
2
1756
by: Maxwell_Smart | last post by:
Is there a way for a function to refer to itself generically? I'd like to use such a thing (should it exist) for convenience and consistency, not functionality. For example: Function Common(Some_String as String) As String ... End Function
4
3141
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my question In short my question is why when I pass a generic type directly to the formatObject function it works fine, but when I pass it to the checkText function where it is itself a generic argument, and then it is passed to formatObject, it is seen...
10
6102
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the code below which when executed generates the following error message: 'There is already an open datareader with this command which must be closed first' Private Sub MainMenu_Load(ByVal sender As System.Object, ByVal e As
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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
9030
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
8899
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
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
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
5861
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
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
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.