Re: Reading Single Value from a SQL DB [FIXED]
- From: Jim in Arizona <tiltowait@xxxxxxxxxxx>
- Date: Wed, 25 Jan 2006 15:29:17 -0700
sloan wrote:
You want to look at the ExecuteScalar method:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/ frlrfsystemdatasqlclientsqlcommandclassexecutescalartopic.asp
Its a cheaper operation than ExecuteReader, when you know you only want 1 value.
Thanks. Your link didn't work but I went to http://msdn.microsoft.com/library/default.asp and did a search for ExecuteScalar and found what I needed (probably where you intended me to go).
My resulting code:
Dim strConnection As String = "server=localhost;uid=sa;pwd=password;database=tickets"
Dim objConnection As New SqlConnection(strConnection)
Dim strResult As String
Dim strSQL As String = "SELECT field2 FROM TTickets WHERE PK = " & txtID.Text
Dim objCommand As New SqlCommand(strSQL, objConnection)
objCommand.Connection.Open()
strResult = objCommand.ExecuteScalar().ToString
objConnection.Close()
Response.Write(strResult)
Thanks everyone for the quick responses.
Jim .
- References:
- Reading Single Value from a SQL DB
- From: Jim in Arizona
- Re: Reading Single Value from a SQL DB
- From: sloan
- Reading Single Value from a SQL DB
- Prev by Date: Re: VS 2005 Crash
- Next by Date: Re: New to ASP.NET
- Previous by thread: Re: Reading Single Value from a SQL DB
- Next by thread: Re:Casting a DBNull
- Index(es):
Relevant Pages
|