Re: Generic class which takes command string and returns datareade

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
<rowe_email@xxxxxxxxx> wrote:

On Aug 28, 10:04 am, Jack Jackson <jacknos...@xxxxxxxxxxxxxxx> wrote:
On Tue, 28 Aug 2007 03:30:47 -0700, rowe_newsgroups



<rowe_em...@xxxxxxxxx> wrote:
On Aug 28, 12:30 am, Peter <Pe...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
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.

.



Relevant Pages

  • Re: Generic class which takes command string and returns datareader?
    ... Inherits SqlDataReader ... Dim conn As New SqlConnection ... You can't inherit from SqlDataReader. ... above in Newin a Shared function and return the DataReader. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: sqldatareader and RecordsAffected
    ... > and the executeReader method. ... > I need to know the number of rows in the sqldatareader and ... RecordsAffected property ... While using select query the value ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: problem of datareader
    ... first of all thanks for reply i know that sqlcommand's executereader method give the instance of sqldatareader but why we can not make it directly and i know that you can not make a instance of the class ... 2.it have private constrcutor. ... what can be the possibilty try to find out n share with us ...
    (microsoft.public.dotnet.languages.csharp)