Options

How do you execute commands on SQL server?

/usr/usr Member Posts: 1,768
I've literally looked everywhere, but can't find any relevant information.

I need to execute a query and return the number of results, but the only command I can really find is ExecuteNonQuery, which doesn't return the number of results.

I know my query is correct, I just can't get it to execute and return the correct number through VB.NET.

Any help would be much appreciated.

I know it's something simple. icon_wink.gif

Comments

  • Options
    JDMurrayJDMurray Admin Posts: 13,031 Admin
    Use the Microsoft Query Analyzer tool to write your SQL queries and then embed the SQL statements in your VB.NET app. Of course, the correct way to do it is only embed SQL statements in stored procedures and call those from VB.NET, but for a quick test I hard-code SQL right into the client app.

    I'm not fluent in SQL, so I rely on Query Analyzer to help me "hack" my SQL statements until they return the result set that I need. A truly indispensable tool for those of us that can't write SQL queries in our head.
  • Options
    /usr/usr Member Posts: 1,768
    I have my query so it will return the correct results in the analyzer, I just need to execute the query via code and have the number of records it returned.

    In Access it's fairly easy, using recordsets.

    But I haven't been able to figure it out in VB.NET. It doesn't help that I don't really have a good reference book.
  • Options
    JDMurrayJDMurray Admin Posts: 13,031 Admin
    VB.NET has access to ADO.NET using the System.Data.OleDb and System.Data.SqlClient. You can look those namespaces up in the online MSDN.
  • Options
    /usr/usr Member Posts: 1,768
    Yeah, I've got all that.

    I suppose what I'm used to is recordsets in Access. I suppose .NET substitutes datasets for recordsets. I'm just struggling with how to fill the dataset and return the results I want, etc.
  • Options
    2lazybutsmart2lazybutsmart Member Posts: 1,119
    If what you're looking for is the number of rows returned after executing an action query (UPDATE, INSERT, etc.), then just use the return value of ExecuteNonQuery. That method's return value is an integer which is the number of records affected by the query.

    If on the other hand, you're SELECTing data, then just read the record count of the result set.

    2lbs.
    Exquisite as a lily, illustrious as a full moon,
    Magnanimous as the ocean, persistent as time.
Sign In or Register to comment.