Read Table data by Table Column Name
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources
Public Class MainClass
Shared Sub Main()
'Declare variables and objects
Dim strConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=Employee.mdb;"
Dim objConnection As New OleDbConnection(strConnectionString)
Dim strSQL As String = _
"SELECT FirstName, LastName FROM Employee"
Dim objCommand As New OleDbCommand(strSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTable As New Data.DataTable("Employee")
Dim objDataRow As DataRow
Try
'Open the database connection
objConnection.Open()
'Fill the DataTable object
objDataAdapter.Fill(objDataTable)
'Load the list box on the form
For Each objDataRow In objDataTable.Rows
Console.WriteLine(objDataRow.Item("FirstName") & " " & _
objDataRow.Item("LastName"))
Next
Catch OleDbExceptionErr As OleDbException
'Write the exception
Console.WriteLine(OleDbExceptionErr.Message)
Catch InvalidOperationExceptionErr As InvalidOperationException
'Write the exception
Console.WriteLine(InvalidOperationExceptionErr.Message)
End Try
'Close the database connection
objConnection.Close()
'Clean up
objDataRow = Nothing
objDataTable.Dispose()
objDataTable = Nothing
objDataAdapter.Dispose()
objDataAdapter = Nothing
objCommand.Dispose()
objCommand = Nothing
objConnection.Dispose()
objConnection = Nothing
End Sub
End Class
Employee.zip( 7 k)Related examples in the same category