VB.Net/Database ADO.net/OleDbDataReader — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:44, 26 мая 2010
Use OleDbDataReader to read data
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 * 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()
Catch OleDbExceptionErr As OleDbException
"Write the exception
Console.WriteLine(OleDbExceptionErr.Message)
Catch InvalidOperationExceptionErr As InvalidOperationException
"Write the exception
Console.WriteLine(InvalidOperationExceptionErr.Message)
End Try
"Declare an OleDbDataReader object
Dim objDataReader As OleDbDataReader
Try
"Execute the SQL text
objDataReader = objCommand.ExecuteReader()
"Check to see if we have data
If objDataReader.HasRows Then
"Process all rows
While objDataReader.Read()
"Get the data in each column
For intIndex As Integer = 0 To objDataReader.FieldCount - 1
Console.WriteLine( objDataReader.Item(intIndex).ToString & ", ")
Next
End While
End If
"Close the reader
objDataReader.Close()
Catch OleDbExceptionErr As OleDbException
Console.WriteLine(OleDbExceptionErr.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
<A href="http://www.vbex.ru/Code/VBDownload/Employee.zip">Employee.zip( 7 k)</a>
Use OleDbDataReader to read data from "select" command
Imports System
Imports System.Data
Imports System.Data.OleDb
public class MainClass
Shared Sub Main()
"Create Connection object
Dim thisConnection As New OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=Employee.mdb")
"Create Command object
Dim thisCommand As New OleDbCommand _
("SELECT ID, FirstName FROM Employee", _
thisConnection)
Try
" Open Connection
thisConnection.Open()
Console.WriteLine("Connection Opened")
" Execute Query
Dim thisReader As OleDbDataReader = thisCommand.ExecuteReader()
While (thisReader.Read())
Console.WriteLine("FirstName: {0} {1}", _
thisReader.GetValue(0), thisReader.GetValue(1))
End While
Catch ex As OleDbException
" Display error
Console.WriteLine("Error: " & ex.ToString())
Finally
" Close Connection
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try
End Sub
End Class
<A href="http://www.vbex.ru/Code/VBDownload/Employee.zip">Employee.zip( 7 k)</a>