VB通用数据库操作方法

 1.VB通用数据操作方法。

 2.通用数据库查询方法。

 3.通用数据库操作方法。

'ERP查询数据库
Public Function YZQuery(sqls As String, msgstring As String) As ADODB.Recordset
 On Error GoTo ExecuteSQL_Error
    Set cnn = New ADODB.Connection
    cnn.ConnectionString = ConnectString
    cnn.Open
    Set rst = New ADODB.Recordset
    rst.Open Trim$(sqls), cnn, adOpenKeyset, adLockOptimistic
    Set YZQuery = rst
    msgstring = "查询到 " & rst.RecordCount & " 条记录 "
ExecuteSQL_Exit:
   Set cnn = Nothing
   Exit Function
ExecuteSQL_Error:
   msgstring = "查询错误: " & err.Description
   Resume ExecuteSQL_Exit
End Function
'ERP更新数据库
Public Function YZUpdate(sqls() As String, msgstring As String) As Boolean
    Dim i As Long
 On Error GoTo ExecuteSQL_Error
    Set cnn = New ADODB.Connection
    cnn.ConnectionString = ConnectString
    cnn.Open
    cnn.BeginTrans
    For i = LBound(sqls) To UBound(sqls)
        cnn.Execute sqls(i)
    Next
    cnn.CommitTrans
    YZUpdate = True
ExecuteSQL_Exit:
   Set rst = Nothing
   Set cnn = Nothing
   Exit Function
ExecuteSQL_Error:
   msgstring = "更新错误: " & err.Description
   YZUpdate = False
   cnn.RollbackTrans
   Resume ExecuteSQL_Exit
End Function

上面查询方法与操作方法都到了ConnectString定义如下:

Public Const ConnectString = "driver={sql server};server=192.168.1.1; uid=sa; pwd=123; database=erp;"


作者:王春天 2013.11.14   地址:http://www.cnblogs.com/spring_wang/p/3423167.html

 

posted @ 2013-11-14 13:37  王春天  阅读(3122)  评论(0编辑  收藏  举报
云推荐