三層架構之資料庫訪問層(VB)

來源:互聯網
上載者:User
訪問|架構|資料|資料庫 '########資料庫訪問層##########


'返回一個ADOCN連線物件

'與C#不同,這裡不能關閉串連及釋放記憶體
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 '用戶端資料指標

If m_cn.State <> adStateClosed Then m_cn.Close

On Error GoTo conerr

m_cn.Open sPro & sDBPath & sDBPWD

Set GetCN = m_cn

Exit Function
conerr:
Set GetCN = Nothing
MsgBox "資料庫連接錯誤", vbCritical

End Function

'執行一句SQL語句,正確返回 1
Public 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 Function
err:
ExcuteSQL = 0
Set m_cn = Nothing


End Function
'執行一組SQL語句,正確返回1
Public Function ExcuteSQLEX(sSQL() As String) As Integer
'調用交易處理
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 Function
err:
ExcuteSQLEX = 0
m_cn.RollbackTrans
m_cn = Nothing

End Function


'未完,待補充



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.