|
|
|
|
|
|
|
|
|
|
|
|
|
VB.NET – DB Communications
Public Class Foo
Public Shared Sub Main()
Dim ConnectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:\temp\foo.mdb"
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)
System.Environment.Exit(1)
End Try
Try
Connection.Close()
Console.WriteLine("Connection closed...")
Catch e As Exception
Console.WriteLine("Problems closing..." + e.Message)
System.Environment.Exit(1)
End Try
System.Environment.Exit(0)
End Sub 'end Main
End Class
· Notice how the above code only connects/disconnects from the database; no transactions occur.
· Notice that the disconnect only occurs after a successful Open (do not disconnect from a database to which you are not connected).
· Notice how the Open call sits inside a Try block.
· Notice how a failed Open or failed Close is caught in the Catch block and how the Message part of the exception is written to the console.
Note: in Windows, env. vars. come in two types, 'User' (user-specific) and 'System' (system-wide). In the COB labs, you may not be allowed to set the 'System' env. vars, but you are allowed to set the 'User' env. variables.
Public Class Foo
Public Shared Sub Main()
Dim ConnectString As String = "Provider=Microsoft.Jet.OLEDB.4.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)
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
End Sub
End Class
select name from foo
(Try this out in Access itself before you try to do it from VB. 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):
· In VB.Net we use an OleDBCommand object to store the statement. The object's ExecuteReader() method can be used to execute the statement. This method returns an OleDbDataReader object which holds the results of the query.
· Now fill up the foo table with multiple records (three or five will do fine). Now, the above query results in more than one name being returned to the OleDBDataReader object.
· If our program must display each of these name strings, it must grab them, one after the other, and display them to the screen. We do this grabbing using the Read() method on the OleDbDataReader object. Each time we call this method it will give us the next record returned from the database. When no more records are returned by the reader, we are done.
Public Class Foo
Public Shared Sub Main()
Dim ConnectString As String = _
"Provider=Microsoft.Jet.OLEDB.4.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
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)
System.Environment.Exit(1)
End Try
'set the SQL statement
SQLCommand = New System.Data.OleDb.OleDbCommand("select 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...")
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...")
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
End Sub
End Class
· Note that the DataReader object never gets created in this code (no New statement for it). This is because it gets created as a side effect by the ExecuteReader() method on the OleDbCommand object.
· It is good practice to Close() your DataReader once you’re done with it (in our next and final lab we'll see the benefit of this!).
· Also note that each of the database actions is enclosed in a try-catch block. Although there is no syntactical requirement to do so, it is very wise(!!!) to check each and every action run against a database. This gives your program opportunity to ‘bail out’ when things go wrong.
· Summary:
o Open the connection.
o Store the SQL statement on an OleDbCommand object.
o Call the ExecuteReader method on the OleDbCommand object to execute the query.
o Call the Read() method on the OleDbDataReader object to access the results.
o When done, close the DataReader and close the Connection.