Results 1 to 3 of 3

Thread: [RESOLVED] [2008] populate variables taken from database

  1. #1

    Thread Starter
    Hyperactive Member Ms.Longstocking's Avatar
    Join Date
    Oct 2006
    Posts
    399

    Resolved [RESOLVED] [2008] populate variables taken from database

    I have a case where I'm given a variable and asked to check the existence of this variable in a db. If it exists, I'm asked to return the relevant row to my application (in this case my asp.net app)

    What is the most efficient way of doing this? I've read the books, the forums, tested my own stuff.... there are so many ways of doing what I want to do.

    My goal is to use as little processing power as possible. From what I've read, I'm guessing that using a dataReader is the way to go?

    In my first dry run, I did not use a dataReader.
    Code:
    Dim sqlQuery As String = "SELECT * FROM lostpass WHERE username = @username"
                    Dim connect As SqlConnection = New SqlConnection(connStr)
                    Dim cmd As SqlCommand = New SqlCommand(sqlQuery,connect)
                    cmd.Parameters.AddWithValue("@username", username)
                    Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
    
                    Dim ds As DataSet = New DataSet()
    
                    If Not IsNothing(da) Then
                        da.Fill(ds)
                        da.Dispose()
                    End If
    End If
    I have 2 issues with my code:
    1) I'm not sure if it's the most efficient way to go
    2) I can't for the life of me get to my record in the dataset

    I really could use a pointer or three.

    Thank you,
    MizPippz

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2008] populate variables taken from database

    If all you are looking for is a single value (ie, one field of a row), then ExecuteScalar will give you the best performance. If you need to return one or more rows for read only, then the datareader is best suited.

    Can you use Stored Procedures? If so, and all you need is a few fields from a single row, then using output parameters and .ExecuteNonQuery would be a good choice.

    As for getting to the row...
    ds.tables(0).Rows(0) gets you the first row.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Hyperactive Member Ms.Longstocking's Avatar
    Join Date
    Oct 2006
    Posts
    399

    Re: [2008] populate variables taken from database

    This is exactly what I was looking for.
    Perfect.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width