Imports System
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
'Imports System.Data.SqlClient
'Imports System.Data.Odbc
'Imports Microsoft.Data.Odbc
Imports System.Configuration
Imports System.Reflection
Public Class COpDB
'连接字符串属性
Private _ConnectString As String
Public Property MyConnectString() As String
Get
Return _ConnectString
End Get
Set(ByVal Value As String)
_ConnectString = Value
End Set
End Property
'构造函数
Public Sub New(ByVal connString As String)
Me._ConnectString = connString
End Sub
'创建Connection
Private Function GetConnection() As IDbConnection
'Return New System.Data.SqlClient.SqlConnection(_ConnectString)
Return New System.Data.OleDb.OleDbConnection(_ConnectString)
End Function
'创建Command
Private Function GetCommand() As IDbCommand
'Return New System.Data.SqlClient.SqlCommand
Return New System.Data.OleDb.OleDbCommand
End Function
'
Private Function GetCommand(ByVal Sql As String, ByVal iConn As IDbConnection) As IDbCommand
'Return New System.Data.SqlClient.SqlCommand(Sql, iConn)
Return New System.Data.OleDb.OleDbCommand(Sql, iConn)
End Function
'创建Adapater
Private Function GetAdapater() As IDataAdapter
'Return New System.Data.SqlClient.SqlDataAdapter
Return New System.Data.OleDb.OleDbDataAdapter
End Function
Private Function GetAdapater(ByVal Sql As String, ByVal iConn As IDbConnection) As IDataAdapter
'Return New System.Data.SqlClient.SqlDataAdapter(Sql, iConn)
Return New System.Data.OleDb.OleDbDataAdapter(Sql, iConn)
End Function
Private Function GetAdapater(ByVal iCmd As IDbCommand) As IDataAdapter
'Return New System.Data.SqlClient.SqlDataAdapter(iCmd)
Return New System.Data.OleDb.OleDbDataAdapter(iCmd)
End Function
'多方式
'Private Function GetDataAdapter(ByVal connType As String, ByVal sql As String) As IDbDataAdapter
' Select Case connType
' Case "OLE"
' idbAdapter = New OleDbDataAdapter(sql, Me._ConnectString)
' Case "SQL"
' idbAdapter = New SqlDataAdapter(sql, Me._ConnectString)
' Case "ODBC"
' idbAdapter = New OdbcDataAdapter(sql, Me._ConnectString)
' Case Else
' '
' End Select
' Return idbAdapter
'End Function
'返回执行有效行数
Public Function ExecuteQuery(ByVal sqlString As String) As DataSet
Dim iConn As System.Data.IDbConnection = Me.GetConnection()
Dim ds As DataSet = New DataSet
Try
Dim iAdapter As System.Data.IDbDataAdapter = Me.GetAdapater(sqlString, iConn)
'If iConn.State <> ConnectionState.Open Then
' iConn.Open()
'End If
iAdapter.Fill(ds)
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
If iConn.State <> ConnectionState.Closed Then
iConn.Close()
End If
End Try
Return ds
End Function
Public Function ExecuteQuery(ByVal sqlString As String, ByVal iTableId As Integer) As DataTable
Dim iConn As System.Data.IDbConnection = Me.GetConnection()
Dim ds As DataSet = New DataSet
Try
Dim iAdapter As System.Data.IDbDataAdapter = Me.GetAdapater(sqlString, iConn)
'If iConn.State <> ConnectionState.Open Then
' iConn.Open()
'End If
iAd