Extracting a single record from a SQL database
-
Hey guys, I'm having a little trouble with what I'm trying to do. I think there is just one or two lines of code that need to be entered (to what I already have), but I'm not exatly sure what it is. What I'm trying to do extract one record from a SQL database, because right now I have the SQL statement in a combobox with visable disabled, and I know that's not correct. Here is what I was thinking, and if this is totally wrong just let me know.
Sub Temp(ByVal myConnString As String) Dim mySelectQuery As String = "SELECT MAX(TicketID) AS Expr1 FROM tblTicket" Dim myConnection As New Data.SqlClient.SqlConnection(myConnString) Dim myCommand As New Data.SqlClient.SqlCommand(mySelectQuery, myConnection) myConnection.Open() Dim myReader As Data.SqlClient.SqlDataReader myReader = myCommand.ExecuteReader() myReader.Close() myConnection.Close() End Sub
The exact bit of data I want to extract is what would be in "Expr1". I've tried several different things, as in... Dim Number as Double ... Number = myReader... Hopefully there is just a few lines of code that can be entered to extract that data, if anyone can help me I'd really appreciate it. Thanks, aqzman
-
Hey guys, I'm having a little trouble with what I'm trying to do. I think there is just one or two lines of code that need to be entered (to what I already have), but I'm not exatly sure what it is. What I'm trying to do extract one record from a SQL database, because right now I have the SQL statement in a combobox with visable disabled, and I know that's not correct. Here is what I was thinking, and if this is totally wrong just let me know.
Sub Temp(ByVal myConnString As String) Dim mySelectQuery As String = "SELECT MAX(TicketID) AS Expr1 FROM tblTicket" Dim myConnection As New Data.SqlClient.SqlConnection(myConnString) Dim myCommand As New Data.SqlClient.SqlCommand(mySelectQuery, myConnection) myConnection.Open() Dim myReader As Data.SqlClient.SqlDataReader myReader = myCommand.ExecuteReader() myReader.Close() myConnection.Close() End Sub
The exact bit of data I want to extract is what would be in "Expr1". I've tried several different things, as in... Dim Number as Double ... Number = myReader... Hopefully there is just a few lines of code that can be entered to extract that data, if anyone can help me I'd really appreciate it. Thanks, aqzman
If you are only ever going to retrieve the first column of the first row (or that the data only has one row, and the row only has one column) then you can use
ExecuteScalar()
- That will save you faffing around with DataReaders. Here is an updated snippet:myConnection.Open() Dim someValue As Object = myCommand.ExecuteScalar(); myConnection.Close()
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website
-
If you are only ever going to retrieve the first column of the first row (or that the data only has one row, and the row only has one column) then you can use
ExecuteScalar()
- That will save you faffing around with DataReaders. Here is an updated snippet:myConnection.Open() Dim someValue As Object = myCommand.ExecuteScalar(); myConnection.Close()
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website