|
-
Apr 12th, 2008, 09:10 AM
#1
Thread Starter
Hyperactive Member
[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
-
Apr 12th, 2008, 09:30 AM
#2
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
-
Apr 12th, 2008, 09:39 AM
#3
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|