473,383 Members | 1,834 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,383 software developers and data experts.

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 1810
On Aug 28, 12:30 am, Peter <Pe...@discussions.microsoft.comwrote:
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(connString)
Dim com As SqlCommand = conn.CreateCommand()
com.CommandType = CommandType.Text
com.CommandText = queryString
Me = com.ExecuteReader()
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********@yahoo.comwrote:
>On Aug 28, 12:30 am, Peter <Pe...@discussions.microsoft.comwrote:
>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(connString)
Dim com As SqlCommand = conn.CreateCommand()
com.CommandType = CommandType.Text
com.CommandText = queryString
Me = com.ExecuteReader()
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...@pebbleridge.comwrote:
On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups

<rowe_em...@yahoo.comwrote:
On Aug 28, 12:30 am, Peter <Pe...@discussions.microsoft.comwrote:
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(connString)
Dim com As SqlCommand = conn.CreateCommand()
com.CommandType = CommandType.Text
com.CommandText = queryString
Me = com.ExecuteReader()
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********@yahoo.comwrote:
>On Aug 28, 10:04 am, Jack Jackson <jacknos...@pebbleridge.comwrote:
>On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups

<rowe_em...@yahoo.comwrote:
>On Aug 28, 12:30 am, Peter <Pe...@discussions.microsoft.comwrote:
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(connString)
Dim com As SqlCommand = conn.CreateCommand()
com.CommandType = CommandType.Text
com.CommandText = queryString
Me = com.ExecuteReader()
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_newsgroups" wrote:
On Aug 28, 10:04 am, Jack Jackson <jacknos...@pebbleridge.comwrote:
On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups

<rowe_em...@yahoo.comwrote:
>On Aug 28, 12:30 am, Peter <Pe...@discussions.microsoft.comwrote:
>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(connString)
Dim com As SqlCommand = conn.CreateCommand()
com.CommandType = CommandType.Text
com.CommandText = queryString
Me = com.ExecuteReader()
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********@yahoo.comwrote:
On Aug 28, 12:30 am, Peter <Pe...@discussions.microsoft.comwrote:
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(connString)
Dim com As SqlCommand = conn.CreateCommand()
com.CommandType = CommandType.Text
com.CommandText = queryString
Me = com.ExecuteReader()
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********@yahoo.comwrote:
On Aug 28, 10:04 am, Jack Jackson <jacknos...@pebbleridge.comwrote:
On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups

<rowe_em...@yahoo.comwrote:
On Aug 28, 12:30 am, Peter <Pe...@discussions.microsoft.comwrote:
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(connString)
Dim com As SqlCommand = conn.CreateCommand()
com.CommandType = CommandType.Text
com.CommandText = queryString
Me = com.ExecuteReader()
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
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...
0
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...
2
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...
1
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
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...
9
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...
2
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...
4
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...
10
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.