Generic class which takes command string and returns datareader?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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")
 
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
 
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.
 
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
 
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.
 
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
 
Back
Top