µ±Ç°Î»Ö㺿ª·¢ÕßÍøÂç >> ¼¼Êõ½Ì³Ì >> .NET½Ì³Ì >> Êý¾Ý¿âÓ¦Óà >> ÄÚÈÝ
¾«²ÊÍÆ¼ö
·ÖÀà×îн̳Ì
·ÖÀàÈȵã½Ì³Ì
  
¶ÔADO.NET¼¸¸ö¶ÔÏóµÄµÚÒ»´Î¼Ó¹¤
×÷ÕߣºÎ´Öª
ÈÕÆÚ£º2005-09-16
ÈËÆø£º
Ͷ¸å£º(תÌù)
À´Ô´£ºÎ´Öª
×ÖÌ壺´ó ÖРС
Êղأº¼ÓÈëä¯ÀÀÆ÷ÊÕ²Ø
ÒÔÏÂÕýÎÄ£º
 

Namespace Tonton.DAL
¡¡¡¡'//----------------------------------------
¡¡¡¡'//¡¶Êý¾ÝÁ´½Ó²ã¡· Ö® ¡¶Êý¾ÝÁ¬½ÓÓëÃüÁîÐС·
¡¡¡¡'//----------------------------------------
¡¡¡¡'//×÷ÕߣºÕÅÉÙÌÄ £¨Tonton£©
¡¡¡¡'//ʱ¼ä£º2005Äê8ÔÂ29ÈÕ
¡¡¡¡'//Óʱࣺtonton@yeah.net
¡¡¡¡'//Ö÷Ò³£ºhttp://www.tonton.cn
¡¡¡¡'//²©¿Í£ºhttp://blog.tonton.cn
¡¡¡¡'//----------------------------------------

¡¡¡¡'//----------------------------------------
¡¡¡¡'//Àý×Ó£º
¡¡¡¡'//----------------------------------------
¡¡¡¡'Dim Conn As New Tonton.DAL.Connection
¡¡¡¡'Dim Cmd As Tonton.DAL.Command
¡¡¡¡'
¡¡¡¡'¡¡¡¡Try
¡¡¡¡'¡¡¡¡¡¡¡¡'//´ò¿ªACCESSÊý¾Ý¿â£¬Ò²¿ÉÒÔÓÃÁ¬½Ó×Ö·û´®²¢²ÉÓÃOpen·½·¨£¬
¡¡¡¡'¡¡¡¡¡¡¡¡'//Conn.Open("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("db.mdb"))
¡¡¡¡'¡¡¡¡¡¡¡¡'//µ±È»£¬Èç¹ûÖªµÀÊÇÓÃACCESS£¬Äã»áÓÃÉÏÃæÕâ¸öÂé·³µÄ·½·¨Âð£¿
¡¡¡¡'¡¡¡¡¡¡¡¡'//Èç¹ûÊÇSQL SERVERµÄ»°£¬¿ÉÒÔÓà Conn.OpenSqlServer ·½·¨¡£
¡¡¡¡'¡¡¡¡¡¡¡¡'
¡¡¡¡'¡¡¡¡¡¡¡¡Conn.OpenAccess(Server.MapPath("db.mdb"))

¡¡¡¡'¡¡¡¡¡¡¡¡'//Ìí¼Ó¼Ç¼
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd = Conn.Execute("Insert Into [Item]([Value]) VALUES (?)")
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd.AddParam("Ìí¼ÓµÄÄÚÈÝ")
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd.Update()

¡¡¡¡'¡¡¡¡¡¡¡¡'//ɾ³ý¼Ç¼
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd = Conn.Execute("Delete From [Item] WHERE Id=?")
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd.AddParam(6)¡¡¡¡¡¡¡¡  '//Ҫɾ³ýµÄ¼Ç¼ºÅ
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd.Update()

¡¡¡¡'¡¡¡¡¡¡¡¡'//¸üмǼ
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd = Conn.Execute("Update [Item] Set [Value]=? WHERE Id=?")
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd.AddParam("еÄÄÚÈÝ")
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd.AddParam(5)
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd.Update()

¡¡¡¡'¡¡¡¡¡¡¡¡'//¶ÁÈ¡¼Ç¼
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd = Conn.Execute("Select * From [Item]")
¡¡¡¡'¡¡¡¡¡¡¡¡If Cmd.Read Then
¡¡¡¡'¡¡¡¡¡¡¡¡¡¡¡¡Response.Write(Cmd("Value"))
¡¡¡¡'¡¡¡¡¡¡¡¡Else
¡¡¡¡'¡¡¡¡¡¡¡¡¡¡¡¡Response.Write("OK")
¡¡¡¡'¡¡¡¡¡¡¡¡End If

¡¡¡¡'¡¡¡¡Catch ex As Exception
¡¡¡¡'¡¡¡¡¡¡¡¡'//³ö´í´¦Àí
¡¡¡¡'¡¡¡¡¡¡¡¡Response.Write(ex)
¡¡¡¡'¡¡¡¡Finally
¡¡¡¡'¡¡¡¡¡¡¡¡'¹Ø±ÕÁ¬½Ó
¡¡¡¡'¡¡¡¡¡¡¡¡Conn.Close()
¡¡¡¡'¡¡¡¡¡¡¡¡Cmd = Nothing
¡¡¡¡'¡¡¡¡End Try
¡¡¡¡'//----------------------------------------
¡¡¡¡'//Àý×Ó½áÊø
¡¡¡¡'//----------------------------------------


¡¡¡¡'//----------------------------------------
¡¡¡¡'//ÀඨÒ忪ʼ
¡¡¡¡'//----------------------------------------

¡¡¡¡'Êý¾ÝÁ¬½ÓÀàÐÍö¾Ù
¡¡¡¡Public Enum ConnectionType As Integer
¡¡¡¡¡¡¡¡OleDb = 1
¡¡¡¡¡¡¡¡SqlServer = 2
¡¡¡¡¡¡¡¡'Oracle = 3
¡¡¡¡¡¡¡¡'MySql = 4
¡¡¡¡End Enum

¡¡¡¡'Á¬½Ó×Ö·û´®¹¹ÔìÆ÷Àà
¡¡¡¡Public Class ConnectStringBuilder
¡¡¡¡¡¡¡¡Public Shared Function JetOleDb(ByVal DataBasePath As String, Optional ByVal PassWord As String = "") As String
¡¡¡¡¡¡¡¡¡¡¡¡If DataBasePath <> "" Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡JetOleDb = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataBasePath & ";"
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If PassWord <> "" Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡JetOleDb &= "User ID='admin';Password=;Jet OLEDB:Database Password=" & PassWord
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡End Function

¡¡¡¡¡¡¡¡Public Shared Function SqlOleDb(Optional ByVal HostName As String = "localhost", Optional ByVal Catalog As String = "", Optional ByVal UserName As String = "sa", Optional ByVal PassWord As String = "") As String
¡¡¡¡¡¡¡¡¡¡¡¡SqlOleDb = "Provider=SQLOLEDB.1;Persist Security Info=False;Data Source=" & HostName & ";Password=" & PassWord & ";User ID=" & UserName & ";"
¡¡¡¡¡¡¡¡¡¡¡¡If Catalog <> "" Then SqlOleDb &= "Initial Catalog=" & Catalog & ";"
¡¡¡¡¡¡¡¡End Function

¡¡¡¡¡¡¡¡Public Shared Function SqlClient(Optional ByVal HostName As String = "localhost", Optional ByVal Catalog As String = "", Optional ByVal UserName As String = "sa", Optional ByVal PassWord As String = "") As String
¡¡¡¡¡¡¡¡¡¡¡¡SqlClient = "Persist Security Info=False;Data Source=" & HostName & ";Password=" & PassWord & ";User ID=" & UserName & ";"
¡¡¡¡¡¡¡¡¡¡¡¡If Catalog <> "" Then SqlClient &= "Initial Catalog=" & Catalog & ";"
¡¡¡¡¡¡¡¡End Function

¡¡¡¡¡¡¡¡Public Shared Function Dsn(ByVal DsnName As String) As String
¡¡¡¡¡¡¡¡¡¡¡¡Return "DSN=" & DsnName
¡¡¡¡¡¡¡¡End Function
¡¡¡¡End Class

¡¡¡¡'Á¬½Ó¶ÔÏóÀà
¡¡¡¡Public Class Connection
¡¡¡¡¡¡¡¡Private _dbConn As IDbConnection
¡¡¡¡¡¡¡¡Private _ConnStr As String
¡¡¡¡¡¡¡¡Private _dbType As ConnectionType = ConnectionType.OleDb

¡¡¡¡¡¡¡¡Public Sub New(Optional ByVal ConnectType As ConnectionType = ConnectionType.OleDb)
¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnectType
¡¡¡¡¡¡¡¡End Sub

¡¡¡¡¡¡¡¡Public Sub New(ByRef Connect As IDbConnection)
¡¡¡¡¡¡¡¡¡¡¡¡If TypeOf Connect Is SqlClient.SqlConnection Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnectionType.SqlServer
¡¡¡¡¡¡¡¡¡¡¡¡ElseIf TypeOf Connect Is OleDb.OleDbConnection Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnectionType.OleDb
¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡End Sub

¡¡¡¡¡¡¡¡Public Sub New(ByVal ConnString As String, Optional ByVal ConnectType As ConnectionType = ConnectionType.OleDb)
¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnectType
¡¡¡¡¡¡¡¡¡¡¡¡Me.ConnectString = ConnString
¡¡¡¡¡¡¡¡End Sub

¡¡¡¡¡¡¡¡'ÉèÖÃ/·µ»ØÁ¬½Ó×Ö·û´®£¬ÉèÖõÄͬʱÉú³ÉеÄÁ¬½Ó¶ÔÏóʵÀý
¡¡¡¡¡¡¡¡Public Property ConnectString() As String
¡¡¡¡¡¡¡¡¡¡¡¡Get
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _ConnStr
¡¡¡¡¡¡¡¡¡¡¡¡End Get
¡¡¡¡¡¡¡¡¡¡¡¡Set(ByVal Value As String)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_ConnStr = Value
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn.Close()
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Catch ex As Exception
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Finally
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If Value <> "" Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Select Case _dbType
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Case ConnectionType.OleDb
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn = New OleDb.OleDbConnection(_ConnStr)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Case ConnectionType.SqlServer
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn = New SqlClient.SqlConnection(_ConnStr)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Case Else
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn = Nothing
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End Select
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn = Nothing
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End Try
¡¡¡¡¡¡¡¡¡¡¡¡End Set
¡¡¡¡¡¡¡¡End Property

¡¡¡¡¡¡¡¡'ÉèÖÃ/·µ»ØÁ¬½ÓÀàÐÍ
¡¡¡¡¡¡¡¡Public Property ConnectType() As ConnectionType
¡¡¡¡¡¡¡¡¡¡¡¡Get
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _dbType
¡¡¡¡¡¡¡¡¡¡¡¡End Get
¡¡¡¡¡¡¡¡¡¡¡¡Set(ByVal Value As ConnectionType)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbType = Value
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me.ConnectString = _ConnStr
¡¡¡¡¡¡¡¡¡¡¡¡End Set
¡¡¡¡¡¡¡¡End Property

¡¡¡¡¡¡¡¡Protected Overrides Sub Finalize()
¡¡¡¡¡¡¡¡¡¡¡¡Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn.Close()
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn.Dispose()
¡¡¡¡¡¡¡¡¡¡¡¡Catch ex As Exception
¡¡¡¡¡¡¡¡¡¡¡¡Finally
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MyBase.Finalize()
¡¡¡¡¡¡¡¡¡¡¡¡End Try
¡¡¡¡¡¡¡¡End Sub

¡¡¡¡¡¡¡¡'·µ»ØÁ¬½Ó¶ÔÏó
¡¡¡¡¡¡¡¡Public ReadOnly Property Connection() As IDbConnection
¡¡¡¡¡¡¡¡¡¡¡¡Get
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _dbConn
¡¡¡¡¡¡¡¡¡¡¡¡End Get
¡¡¡¡¡¡¡¡End Property

¡¡¡¡¡¡¡¡'´ò¿ªÒ»¸öÊý¾Ý¿âÁ¬½Ó£¬ÒªÖ¸¶¨Á¬½ÓÀàÐÍ£¨Ä¬ÈÏΪOLEDB£©
¡¡¡¡¡¡¡¡Public Function Open(Optional ByVal ConnString As String = "", Optional ByVal ConnType As ConnectionType = ConnectionType.OleDb) As Boolean
¡¡¡¡¡¡¡¡¡¡¡¡Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If ConnString <> "" Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_ConnStr = ConnString
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnType
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn.Open()
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return True
¡¡¡¡¡¡¡¡¡¡¡¡Catch ex As Exception
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throw ex
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return False
¡¡¡¡¡¡¡¡¡¡¡¡End Try
¡¡¡¡¡¡¡¡End Function

¡¡¡¡¡¡¡¡'´ò¿ªÒ»¸öACCESSÁ¬½Ó
¡¡¡¡¡¡¡¡Public Function OpenAccess(ByVal DbPath As String, Optional ByVal PassWord As String = "") As Boolean
¡¡¡¡¡¡¡¡¡¡¡¡If DbPath <> "" Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DbPath & ";"
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If PassWord <> "" Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_ConnStr &= "User ID='admin';Password=;Jet OLEDB:Database Password=" & PassWord
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnectionType.OleDb
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn = New OleDb.OleDbConnection(_ConnStr)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return True
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Catch ex As Exception
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throw ex
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return False
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End Try
¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡End Function

¡¡¡¡¡¡¡¡'´ò¿ªÒ»¸öSQL SERVERÁ¬½Ó
¡¡¡¡¡¡¡¡Public Function OpenSqlServer(Optional ByVal HostName As String = "localhost", Optional ByVal Catalog As String = "", Optional ByVal UserName As String = "sa", Optional ByVal PassWord As String = "") As Boolean
¡¡¡¡¡¡¡¡¡¡¡¡Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_ConnStr = "Persist Security Info=False;Data Source=" & HostName & ";Password=" & PassWord & ";User ID=" & UserName & ";"
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If Catalog <> "" Then _ConnStr &= "Initial Catalog=" & Catalog & ";"

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnectionType.SqlServer
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn = New SqlClient.SqlConnection(_ConnStr)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return True
¡¡¡¡¡¡¡¡¡¡¡¡Catch ex As Exception
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throw ex
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return False
¡¡¡¡¡¡¡¡¡¡¡¡End Try
¡¡¡¡¡¡¡¡End Function

¡¡¡¡¡¡¡¡'¹Ø±ÕÊý¾ÝÁ¬½Ó
¡¡¡¡¡¡¡¡Public Sub Close()
¡¡¡¡¡¡¡¡¡¡¡¡Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn.Close()
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbConn = Nothing
¡¡¡¡¡¡¡¡¡¡¡¡Catch ex As Exception
¡¡¡¡¡¡¡¡¡¡¡¡End Try
¡¡¡¡¡¡¡¡End Sub

¡¡¡¡¡¡¡¡'Ö´ÐÐÒ»¸öSQLÓï¾ä£¬Éú³É»ò²»Éú³ÉÒ»¸öCommand¶ÔÏó
¡¡¡¡¡¡¡¡Public Function Execute(ByVal Sql As String, Optional ByVal NonQuery As Boolean = False) As Command
¡¡¡¡¡¡¡¡¡¡¡¡Execute = New Command(_dbConn, Sql)
¡¡¡¡¡¡¡¡¡¡¡¡If NonQuery Then Execute.Update()
¡¡¡¡¡¡¡¡End Function

¡¡¡¡¡¡¡¡'Éú³ÉÒ»¸öDataSet£¬²»ÔÙÐèÒªÊÖ¹¤Éú³ÉDataAdapter¶ÔÏóÁË¡£
¡¡¡¡¡¡¡¡Public ReadOnly Property DataSource(ByVal Sql As String, Optional ByVal Name As String = "", Optional ByVal Schema As Boolean = False) As DataSet
¡¡¡¡¡¡¡¡¡¡¡¡Get
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim Ds As IDataAdapter
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Select Case _dbType
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Case ConnectionType.OleDb
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Ds = New OleDb.OleDbDataAdapter(Sql, _dbConn)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Case ConnectionType.SqlServer
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Ds = New SqlClient.SqlDataAdapter(Sql, _dbConn)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End Select

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If Name = "" Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡DataSource = New DataSet
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡DataSource = New DataSet(Name)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End If

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If Schema Then Ds.FillSchema(DataSource, SchemaType.Source)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Ds.Fill(DataSource)
¡¡¡¡¡¡¡¡¡¡¡¡End Get
¡¡¡¡¡¡¡¡End Property
¡¡¡¡End Class

¡¡¡¡'ÃüÁîÐÐÀà
¡¡¡¡Public Class Command
¡¡¡¡¡¡¡¡Private _Cmd As IDbCommand
¡¡¡¡¡¡¡¡Private _Sql As String
¡¡¡¡¡¡¡¡Private _Conn As IDbConnection
¡¡¡¡¡¡¡¡Private _rdr As IDataReader
¡¡¡¡¡¡¡¡Private _dbType As ConnectionType

¡¡¡¡¡¡¡¡Public Sub New(ByRef Connect As IDbConnection)
¡¡¡¡¡¡¡¡¡¡¡¡_Conn = Connect
¡¡¡¡¡¡¡¡¡¡¡¡If TypeOf Connect Is OleDb.OleDbConnection Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnectionType.OleDb
¡¡¡¡¡¡¡¡¡¡¡¡ElseIf TypeOf Connect Is SqlClient.SqlConnection Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnectionType.SqlServer
¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡End Sub

¡¡¡¡¡¡¡¡Public Sub New(ByRef Connect As IDbConnection, ByVal Sql As String)
¡¡¡¡¡¡¡¡¡¡¡¡_Conn = Connect
¡¡¡¡¡¡¡¡¡¡¡¡If TypeOf Connect Is OleDb.OleDbConnection Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnectionType.OleDb
¡¡¡¡¡¡¡¡¡¡¡¡ElseIf TypeOf Connect Is SqlClient.SqlConnection Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dbType = ConnectionType.SqlServer
¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡¡¡¡¡Me.Sql = Sql
¡¡¡¡¡¡¡¡End Sub

¡¡¡¡¡¡¡¡Protected Overrides Sub Finalize()
¡¡¡¡¡¡¡¡¡¡¡¡Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_Cmd.Dispose()
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_rdr.Dispose()
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_Conn.Dispose()
¡¡¡¡¡¡¡¡¡¡¡¡Catch ex As Exception
¡¡¡¡¡¡¡¡¡¡¡¡Finally
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MyBase.Finalize()
¡¡¡¡¡¡¡¡¡¡¡¡End Try
¡¡¡¡¡¡¡¡End Sub

¡¡¡¡¡¡¡¡'ÉèÖûò·µ»ØÁ¬½Ó¶ÔÏó
¡¡¡¡¡¡¡¡Public Property Connect() As IDbConnection
¡¡¡¡¡¡¡¡¡¡¡¡Get
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _Conn
¡¡¡¡¡¡¡¡¡¡¡¡End Get
¡¡¡¡¡¡¡¡¡¡¡¡Set(ByVal Value As IDbConnection)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_Conn = Value
¡¡¡¡¡¡¡¡¡¡¡¡End Set
¡¡¡¡¡¡¡¡End Property

¡¡¡¡¡¡¡¡'¶ÁÈ¡»òÉèÖÃSQLÓï¾ä
¡¡¡¡¡¡¡¡Public Property Sql() As String
¡¡¡¡¡¡¡¡¡¡¡¡Get
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _Sql
¡¡¡¡¡¡¡¡¡¡¡¡End Get
¡¡¡¡¡¡¡¡¡¡¡¡Set(ByVal Value As String)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_Sql = Value
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If _dbType = ConnectionType.SqlServer Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_Cmd = New SqlClient.SqlCommand(Sql, _Conn)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Else
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_Cmd = New OleDb.OleDbCommand(_Sql, _Conn)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡¡¡¡¡End Set
¡¡¡¡¡¡¡¡End Property

¡¡¡¡¡¡¡¡'¶ÁÈ¡ÏÂÒ»¼Ç¼ÐУ¬Èç¹û¼Ç¼¼¯Ã»Óдò¿ª£¬Ôò×Ô¶¯´ò¿ª¡£
¡¡¡¡¡¡¡¡Public Function Read(Optional ByVal Behavior As System.Data.CommandBehavior = CommandBehavior.Default) As Boolean
¡¡¡¡¡¡¡¡¡¡¡¡If _rdr Is Nothing Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_rdr = _Cmd.ExecuteReader(Behavior)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _rdr.Read
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Catch ex As Exception
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return False
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return False
¡¡¡¡¡¡¡¡¡¡¡¡Else
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _rdr.Read
¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡End Function

¡¡¡¡¡¡¡¡'//·µ»Ø×Ö¶ÎÊý
¡¡¡¡¡¡¡¡Public Function FieldCount() As Integer
¡¡¡¡¡¡¡¡¡¡¡¡Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _rdr.FieldCount
¡¡¡¡¡¡¡¡¡¡¡¡Catch ex As Exception
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return 0
¡¡¡¡¡¡¡¡¡¡¡¡End Try
¡¡¡¡¡¡¡¡End Function

¡¡¡¡¡¡¡¡'//Ö´ÐÐÃüÁî
¡¡¡¡¡¡¡¡Public Function Update() As Boolean
¡¡¡¡¡¡¡¡¡¡¡¡Try
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If _Conn.State <> ConnectionState.Open Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_Conn.Open()
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_Cmd.ExecuteNonQuery()
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return True
¡¡¡¡¡¡¡¡¡¡¡¡Catch ex As Exception
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throw ex
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return False
¡¡¡¡¡¡¡¡¡¡¡¡End Try
¡¡¡¡¡¡¡¡End Function

¡¡¡¡¡¡¡¡'//¶ÁÈ¡×ֶλòÌí¼ÓSQLµÄÃüÃû²ÎÊý
¡¡¡¡¡¡¡¡Default Public Property Item(ByVal Name As String)
¡¡¡¡¡¡¡¡¡¡¡¡Get
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _rdr(Name)
¡¡¡¡¡¡¡¡¡¡¡¡End Get
¡¡¡¡¡¡¡¡¡¡¡¡Set(ByVal Value)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡AddParam(Value, Name)
¡¡¡¡¡¡¡¡¡¡¡¡End Set
¡¡¡¡¡¡¡¡End Property

¡¡¡¡¡¡¡¡'//¶ÁÈ¡×Ö¶Î
¡¡¡¡¡¡¡¡Default Public ReadOnly Property Item(ByVal Index As Integer)
¡¡¡¡¡¡¡¡¡¡¡¡Get
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _rdr(Index)
¡¡¡¡¡¡¡¡¡¡¡¡End Get
¡¡¡¡¡¡¡¡End Property

¡¡¡¡¡¡¡¡'//Ìí¼Ó²ÎÊý
¡¡¡¡¡¡¡¡Public Function AddParam(ByRef Value As Object, Optional ByVal Name As String = "")
¡¡¡¡¡¡¡¡¡¡¡¡If _dbType = ConnectionType.OleDb Then
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return CType(_Cmd, OleDb.OleDbCommand).Parameters.Add(Name, Value)
¡¡¡¡¡¡¡¡¡¡¡¡Else
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return _Cmd.Parameters.Add(New SqlClient.SqlParameter("@" & Name, Value))
¡¡¡¡¡¡¡¡¡¡¡¡End If
¡¡¡¡¡¡¡¡End Function
¡¡¡¡End Class
End Namespace

Ïà¹ØÎÄÕ£º