主题:[转帖]共享金老师(金旭亮)写的一个有关访问数据库的类
值得学习。。。。。。。。。。。
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
'使用OLEDB访问数据库的对象
Public Class OLEDBAccessObj
Private _Connection As OleDb.OleDbConnection
Private _Command As OleDb.OleDbCommand
Private _DataAdapter As OleDb.OleDbDataAdapter
Private _DataReader As OleDb.OleDbDataReader
Private _DataSet As Data.DataSet
'实现OLEDB连接字串
Private _ConnectionStr As String
Public Property ConnectionStr() As String
Get
Return _ConnectionStr
End Get
Set(ByVal Value As String)
_ConnectionStr = Value
End Set
End Property
'设置Command对象的CommandText属性,以允许更新脱机保存的数据
Public Property strSQLCommand() As String
Get
If _Command Is Nothing Then
Return ""
Else
Return _Command.CommandText
End If
End Get
Set(ByVal Value As String)
If _Command Is Nothing Then
Throw New Exception("接收SQL命令的Command对象为Nothing")
End If
_Command.CommandType = CommandType.Text
_Command.CommandText = Value
End Set
End Property
'在此过程中创建对象,并创建这些对象之间的联系,由构造函数调用
Private Sub CreateObject()
_Connection = New OleDb.OleDbConnection
_Command = New OleDb.OleDbCommand
_Command.Connection = _Connection
_DataAdapter = New OleDb.OleDbDataAdapter(_Command)
End Sub
'构造函数
Public Sub New()
_ConnectionStr = ""
CreateObject()
End Sub
Public Sub New(ByVal strConnection As String)
_ConnectionStr = strConnection
CreateObject()
End Sub
'连接数据库,成功返回true
'如有错误则抛出异常
Public Function ConnectDB() As Boolean
If _ConnectionStr.Trim() = System.String.Empty Then
Throw New Exception("请指定连接字串")
End If
If _Connection.State = ConnectionState.Open Then
Throw New Exception("数据库连接己打开")
End If
_Connection.ConnectionString = _ConnectionStr
'尝试连接并捕获错误
Try
_Connection.Open()
Catch ErrObj As System.InvalidOperationException
Throw New Exception("数据库连接已打开了,不能再一次打开,在OLEDBAccessObj.ConnectDB中")
Catch ErrObj As System.Data.OleDb.OleDbException
Dim i As Integer
For i = 0 To ErrObj.Errors.Count - 1
Throw New Exception("Index #" + i.ToString() + ControlChars.Cr _
+ "Message: " + ErrObj.Errors(i).Message + ControlChars.Cr _
+ "Native: " + ErrObj.Errors(i).NativeError.ToString() + ControlChars.Cr _
+ "Source: " + ErrObj.Errors(i).Source + ControlChars.Cr _
+ "SQL: " + ErrObj.Errors(i).SQLState + ControlChars.Cr)
Next i
Return False
End Try
Return True
End Function
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
'使用OLEDB访问数据库的对象
Public Class OLEDBAccessObj
Private _Connection As OleDb.OleDbConnection
Private _Command As OleDb.OleDbCommand
Private _DataAdapter As OleDb.OleDbDataAdapter
Private _DataReader As OleDb.OleDbDataReader
Private _DataSet As Data.DataSet
'实现OLEDB连接字串
Private _ConnectionStr As String
Public Property ConnectionStr() As String
Get
Return _ConnectionStr
End Get
Set(ByVal Value As String)
_ConnectionStr = Value
End Set
End Property
'设置Command对象的CommandText属性,以允许更新脱机保存的数据
Public Property strSQLCommand() As String
Get
If _Command Is Nothing Then
Return ""
Else
Return _Command.CommandText
End If
End Get
Set(ByVal Value As String)
If _Command Is Nothing Then
Throw New Exception("接收SQL命令的Command对象为Nothing")
End If
_Command.CommandType = CommandType.Text
_Command.CommandText = Value
End Set
End Property
'在此过程中创建对象,并创建这些对象之间的联系,由构造函数调用
Private Sub CreateObject()
_Connection = New OleDb.OleDbConnection
_Command = New OleDb.OleDbCommand
_Command.Connection = _Connection
_DataAdapter = New OleDb.OleDbDataAdapter(_Command)
End Sub
'构造函数
Public Sub New()
_ConnectionStr = ""
CreateObject()
End Sub
Public Sub New(ByVal strConnection As String)
_ConnectionStr = strConnection
CreateObject()
End Sub
'连接数据库,成功返回true
'如有错误则抛出异常
Public Function ConnectDB() As Boolean
If _ConnectionStr.Trim() = System.String.Empty Then
Throw New Exception("请指定连接字串")
End If
If _Connection.State = ConnectionState.Open Then
Throw New Exception("数据库连接己打开")
End If
_Connection.ConnectionString = _ConnectionStr
'尝试连接并捕获错误
Try
_Connection.Open()
Catch ErrObj As System.InvalidOperationException
Throw New Exception("数据库连接已打开了,不能再一次打开,在OLEDBAccessObj.ConnectDB中")
Catch ErrObj As System.Data.OleDb.OleDbException
Dim i As Integer
For i = 0 To ErrObj.Errors.Count - 1
Throw New Exception("Index #" + i.ToString() + ControlChars.Cr _
+ "Message: " + ErrObj.Errors(i).Message + ControlChars.Cr _
+ "Native: " + ErrObj.Errors(i).NativeError.ToString() + ControlChars.Cr _
+ "Source: " + ErrObj.Errors(i).Source + ControlChars.Cr _
+ "SQL: " + ErrObj.Errors(i).SQLState + ControlChars.Cr)
Next i
Return False
End Try
Return True
End Function