Monday, January 19, 2009

Read a single value from a query with VB.Net

Sometimes it's necessary to read single value from a table (or a query). Here's the code:

Imports System.Data.Oledb


Dim ConnString As String = "connection string"
' open connection
Dim Conn As New Data.OleDb.OleDbConnection(ConnString)
Cn.Open()

' request execution
Dim SQL As String = "SELECT Data FROM Table WHERE ID = 'aaaa';"
Dim command As New Data.OleDb.OleDbCommand(Sql, Conn)
Dim ID As Integer = command.ExecuteScalar()

'close connection
Cn.Close()

No comments: