Oregon State University
Oregon State University Home Page

BA 371: Database Communications

VB.Net 2008 – DB Communications

Public Class Foo

Public Shared Sub Main()

'Microsoft SQL Server 2008
'Replace "your database" with the name of your database in the following statement
Dim ConnectString As String = "Provider=SQLOLEDB.1;" + _
"Data Source=cob-tfs08; Initial Catalog = your database;" + _
"Integrated Security = SSPI;"

Dim Connection As New System.Data.OleDb.OleDbConnection(ConnectString)

Try
Console.WriteLine(ConnectString)
Connection.Open()
Console.WriteLine("Connection open...")
Catch e As Exception
Console.WriteLine("Problems opening..." + e.Message)
Console.WriteLine()
Console.WriteLine("Press Enter to Exit.")
System.Console.ReadLine()
System.Environment.Exit(1)
End Try

Try
Connection.Close()
Console.WriteLine("Connection closed...")
Catch e As Exception
Console.WriteLine("Problems closing..." + e.Message)
Console.WriteLine()
Console.WriteLine("Press Enter to Exit.")
System.Console.ReadLine()
System.Environment.Exit(1)
End Try

Console.WriteLine()
Console.WriteLine("Press Enter to Exit.")
System.Console.ReadLine()
System.Environment.Exit(0)

End Sub 'end Main
End Class

Public Class Foo

Public Shared Sub Main()

'Microsoft SQL 2008
Dim ConnectString As String = "Provider=SQLOLEDB.1;" + _
"Data Source=" + Environ("dbserver") + ";" + " Initial Catalog = " + Environ("vbdb") + ";" + _
"Integrated Security = SSPI;"

Dim Connection As New System.Data.OleDb.OleDbConnection(ConnectString)

Try
Console.WriteLine(ConnectString)
Connection.Open()
Console.WriteLine("Connection open...")
Catch e As Exception
Console.WriteLine("Problems opening..." + _
e.Message)
Console.WriteLine()
Console.WriteLine("Press Enter to Exit.")
System.Console.ReadLine()
System.Environment.Exit(1)
End Try

Try
Connection.Close()
Console.WriteLine("Connection closed...")
Catch e As Exception
Console.WriteLine("Problems closing..." + _
e.Message)
End Try

Console.WriteLine()
Console.WriteLine("Press Enter to Exit.")
System.Console.ReadLine()

End Sub
End Class
 select my_name from foo;

Try this out in MS SQL 2008 itself by executing the query from MS SQL Server Management Studio :

Try this before running your query through VB.Net. A little trick-of-the-trade: before including SQL calls into your program code, always!! try them out using the SQL interpreter of the database software itself. That way you can exclude SQL errors from messing up your program.


Public Class Foo

Public Shared Sub Main()

'Microsoft SQL 2008
Dim ConnectString As String = "Provider=SQLOLEDB.1;" + _
"Data Source=" + Environ("dbserver") + ";" + " Initial Catalog = " + Environ("vbdb") + ";" + _
"Integrated Security = SSPI;"

Dim Connection As New System.Data.OleDb.OleDbConnection(ConnectString)

Dim SQLCommand As System.Data.OleDb.OleDbCommand

Dim SQLDataReader As System.Data.OleDb.OleDbDataReader = nothing

Dim result_string As String

'open the connection
Connection = New System.Data.OleDb.OleDbConnection(ConnectString)
Try
Console.WriteLine(ConnectString)
Connection.Open()
Console.WriteLine("Connection open...")
Catch e As Exception
Console.WriteLine("Problems opening..." + e.Message)
Console.WriteLine()
Console.WriteLine("Press Enter to Exit.")
System.Console.ReadLine()
System.Environment.Exit(1)
End Try

'set the SQL statement
SQLCommand = New System.Data.OleDb.OleDbCommand("select my_name from foo", Connection)

'execute the statement
Try
SQLDataReader = SQLCommand.ExecuteReader()

'in case the execution fails, close the connection and exit
Catch e As Exception
Console.WriteLine("Problems executing..." + e.Message)
Connection.Close()
Console.WriteLine("Connection closed...")
Console.WriteLine()
Console.WriteLine("Press Enter to Exit.")
System.Console.ReadLine()
System.Environment.Exit(1)
End Try

'at this point we know that execution succeeded so we can start reading the
'results. Since we do not know how many records we get returned, we'll stick
'the read in a while() loop that keeps executing until we no longer get
'results from the reader.

Try
Do While SQLDataReader.Read()
result_string = SQLDataReader.GetString(0)
Console.WriteLine(result_string)
Loop
SQLDataReader.Close()
Catch e As Exception
Console.WriteLine("Problems reading data..." + e.Message)
Connection.Close()
Console.WriteLine("Connection closed...")
Console.WriteLine()

Console.WriteLine("Press Enter to Exit.")
System.Console.ReadLine()
System.Environment.Exit(1)
End Try

'we’re done so we can close the connection
Try
Connection.Close()
Console.WriteLine("Connection closed...")
Catch e As Exception
Console.WriteLine("Problems closing..." + e.Message)
End Try

Console.WriteLine()
Console.WriteLine("Press Enter to Exit.")
System.Console.ReadLine()
End Sub
End Class