Oregon State University
Oregon State University Home Page

BA 371: Database Communications

VB.Net 2005 – DB Communications

Public Class Foo

Public Shared Sub Main()

'Access 2003
'Dim ConnectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + _
'"Data Source=c:\foo.mdb"

'Access 2007
Dim ConnectString As String = "provider=Microsoft.ACE.OLEDB.12.0;" + _
"Data Source=c:\foo.accdb"

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()

'Access 2003
'Dim ConnectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + _
'"Data Source=" + Environ("vbdb")

'Access 2007
Dim ConnectString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _
"Data Source=" + Environ("vbdb")

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 Access itself by using its built-in SQL interpreter:

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()

'Access 2003
'Dim ConnectString As String = _
'"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Environ("vbdb")

'Access 2007
Dim ConnectSTring as String = _
"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Environ("vbdb")

Dim Connection As System.Data.OleDb.OleDbConnection

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