IDatareader

  • 15 years ago

    hey coders, hope you guys can help me out again!


    i'm using entlib's daab to connect to my sql database which is working find but i'm faced with a little problem, so help me out with this.


    i would like to determine if a cetain entry is already contained within the database, for this i have two columns in the table that I reference "DataID" and "UserID", where DataID is a uniqueidentified value which is retrieved according to an ID that is passed to my method. the UserID is passed at run-time, look at the following piece of code


    Dim _Database as Database = DatabaseFactory.CreateDatabase   [Data Access Block]
    Dim _Reader as IDatareader = _Database.ExecuteReader(CommandType.Text, "SELECT * FROM Data WHERE DataID   = '" & _DataID & "' AND UserID = '" & _UserID & "'")


    as you can see this is a standard ADO.Net type query and should return the data, if any exists in the database back to the reader. my problem is that i cannot use the datareader's hasrows method because i'm referencing the idatareader directly.


    how can i determine if _reader contains any values or rows....


    i know that an alternative would be to use a dataset, but i'm looking for a readonly, once-off connection to the database.

  • 15 years ago

    IDataReader.Read returns False if there is no next row, so normally you would just use a loop:

    Code:
    While myIDataReader.Read()
       'The reader contains the next row here.
    End While

  • 15 years ago

    Hey J


    That is true, but a loop would not have been the right course of action for what I wanted to do. If the reader contains no row values means that an insert into the database had to be done, that is what I wanted to access the HasRows event.


    If _Reader.HasRows = False Then
       ' Do the insert
    End


    but because i'm referencing the interface directly could i not access the HasRows method so what I did was to assign the interface to the SQLDatareader, which in turn gave me access to the HasRows method:


    Dim _SQLReader As SQLDatareader = _Reader


    Thanks for your reply

  • 15 years ago

    If all you wanted was whether there is matching data or not then you should be calling ExecuteScalar rather than ExecuteReader, assuming that your framework gives you access to that method.  Even as you you still don't need to cast the object as any other type.  Just use an If statement with Read instead of a Loop.  You still get your boolean value that indicates whether the reader has rows or not.

  • 15 years ago

    that makes perfect sense as well, thanks

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan