Database Access Layer (VB) of the three-layer architecture

xiaoxiao2021-03-06  41

'######## Database Access Layer ##########

'Returns an Adocn Connection Object' and C # Unlike C #, you cannot close the connection and release of memory public function getcn (sdbpath as string) as adoDb.connection

Const sPro As String = "provider = microsoft.jet.oledb.4.0; data source =" Const sDBPWD As String = "; jet oledb: database password = QQ: 48403849" Dim sDBPath As String Dim m_cn As ADODB.Connection Set m_cn = New adodb.connection m_cn.cursorlocation = aduseclient 'client game if m_cn.state <> adstateclosed the m_cn.close on error goto conerr m_cn.open spro & sdbpath & sdbpwd set getcn = m_cn

EXIT FUNCONERR: SET getCN = Nothing msgbox "Database Connection Error", Vbcritical

END FUNCTION

'Execute a SQL statement correctly returns 1Public Function ExcuteSQL (sSQL As String) As Integer Dim m_cn As New ADODB.Connection On Error GoTo err Set m_cn = GetCN (sG_DBPath) m_cn.Execute sSQL m_cn.Close Set m_cn = Nothing ExcuteSQL = 1 EXIT functionerr: excutesql = 0 set m_cn = Nothing

End function 'Execute a set of SQL statements, correctly return 1PUBLIC FUNCTION EXCUTESQLEX (SSQL () AS String) AS Integer' call transaction DIM M_CN As New Adodb.Connection, I as integer if ubound (ssql) <0 THEN EXIT FUNCTION ON Error GoTo err Set m_cn = GetCN (sG_DBPath) m_cn.BeginTrans For i = 0 To UBound (sSQL) - 1 m_cn.Execute sSQL (i) Next i m_cn.CommitTrans m_cn.Close Set m_cn = Nothing ExcuteSQLEX = 1 Exit Functionerr: ExcuteSQLEX = 0 m_cn.rollbacktrans m_cn = Nothing end function

'Und finished, to be supplement

转载请注明原文地址:https://www.9cbs.com/read-55299.html

New Post(0)