VB.NET get single row from Access

databases United States
  • 13 years ago

    It has been years since my VB courses (for VB 4), and I am sure it has changed anyway.  I know that I did this once before, but I can't remember how and can't find any examples on the web.


    I have a form where the user will key in a value in a text box that will be a primary key in a table in an MS Access database.  I do it all the time using C on Unix accessing Oracle, but I need to do it using VB on Windows accessing MS Access.


    How can I get the results of a PK select query -- where I am sure that there will be either no row returned or only one row returned -- into a VB variable.  Once it is there, I know what to do.


    Also -- I am probably going to sound like a rank amatuer by this statement, but, again, I do it all the time with C (and COBOL) accessing Oracle, DB2, and Informix -- does VB not support ESQL to access MS Access database?


    Any help would be appreciated greatly.

  • 13 years ago

    Do you mean a single row, or a single row with a single column?


    If it's a single row with a single column, you can use the ExecuteScalar() method of the SqlCommand/OleDbCommand object (we're talking ADO.NET here) which "Executes the query, and returns the first column of the first row in the result set returned by the query".


    If you know there is only going to be one row, when you call the ExecuteReader function you can pass a CommandBehavior.SingleRow as the CommandBehavoir parameter. However, you'll still have to use a reader to get the row first.


    Not sure about ESQL at all.... generally you'd use either a ole db or odbc driver for accessing the access database....

  • 13 years ago

    Thanks, I will try it.

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.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann