vb如何连接Access数据库啊?高手回答一下!谢了!
2个回答
展开全部
Public DataPath As String '数据库路径
Public ConnectString As String '数据库路径
Public cn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public Function getDBConnection() As String
DataPath = App.Path + "\DataBase\GYDATA.mdb"
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DataPath + ";Persist Security Info=False" + ";Jet OLEDB:Database Password="
cn.ConnectionString = ConnectString
cn.Open ConnectString
End Function '/////////////////
'数据库选择:ADODB.Recordset
Public Function ExeSQL(ByVal SQL As String) As ADODB.Recordset
On Error GoTo ErrHandler:
Dim strArray() As String
strArray = Split(SQL)
getDBConnection
If StrComp(UCase$(strArray(0)), "select", vbTextCompare) = 0 Then
rs.Open Trim$(SQL), cn, adOpenKeyset, adLockOptimistic
Set ExeSQL = rs
Else
cn.Execute SQL
End If
ExeSQl_Exit:
Set rs = Nothing
Set cn = Nothing
Exit Function
ErrHandler:
' 显示错误信息
MsgBox "错误号:" & Err.Number & " 错误信息:" & Err.Description, vbExclamation
Resume ExeSQl_Exit
End Function
'关闭数据库连接
Public Sub CloseConn()
'打开错误处理陷阱
On Error GoTo ErrHandler:
'----------------------------------------------------
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
'-------------------------------------------
Exit Sub
ErrHandler:
' 显示错误信息
MsgBox "错误号:" & Err.Number & " 错误信息:" & Err.Description, vbExclamation
End Sub
Public ConnectString As String '数据库路径
Public cn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public Function getDBConnection() As String
DataPath = App.Path + "\DataBase\GYDATA.mdb"
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DataPath + ";Persist Security Info=False" + ";Jet OLEDB:Database Password="
cn.ConnectionString = ConnectString
cn.Open ConnectString
End Function '/////////////////
'数据库选择:ADODB.Recordset
Public Function ExeSQL(ByVal SQL As String) As ADODB.Recordset
On Error GoTo ErrHandler:
Dim strArray() As String
strArray = Split(SQL)
getDBConnection
If StrComp(UCase$(strArray(0)), "select", vbTextCompare) = 0 Then
rs.Open Trim$(SQL), cn, adOpenKeyset, adLockOptimistic
Set ExeSQL = rs
Else
cn.Execute SQL
End If
ExeSQl_Exit:
Set rs = Nothing
Set cn = Nothing
Exit Function
ErrHandler:
' 显示错误信息
MsgBox "错误号:" & Err.Number & " 错误信息:" & Err.Description, vbExclamation
Resume ExeSQl_Exit
End Function
'关闭数据库连接
Public Sub CloseConn()
'打开错误处理陷阱
On Error GoTo ErrHandler:
'----------------------------------------------------
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
'-------------------------------------------
Exit Sub
ErrHandler:
' 显示错误信息
MsgBox "错误号:" & Err.Number & " 错误信息:" & Err.Description, vbExclamation
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Module1.bas
'---------------------------------------------------------------------------------
Public strDSN As String
Public RS As ADODB.Recordset
Public rst As New ADODB.Recordset
Public Sub con(ssql As String)
strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\pcdb1.mdb;Persist Security Info=False;Jet OLEDB:Database Password=''" 'Access2003
On Error Resume Next
If rst.State = 1 Then rst.Close
rst.CursorLocation = adUseClient
Err.Clear
On Error GoTo err0
rst.Open ssql, strDSN, adOpenDynamic, adLockOptimistic
Exit Sub
err0:
MsgBox Err.Description
End Sub
'---------------------------------------------------------------------------------
调用
Dim SQLstr As String
SQLstr = "select id,pcname as 计算机名,macaddr as MAC地址,ip as IP地址 from pcmac where pcname like '" & str1 & "'"
Call con(SQLstr)
While Not rst.EOF
str1 = rst("id")
ips = rst("pcname")
rst.MoveNext
Wend
Set rst=Nothing
'---------------------------------------------------------------------------------
Public strDSN As String
Public RS As ADODB.Recordset
Public rst As New ADODB.Recordset
Public Sub con(ssql As String)
strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\pcdb1.mdb;Persist Security Info=False;Jet OLEDB:Database Password=''" 'Access2003
On Error Resume Next
If rst.State = 1 Then rst.Close
rst.CursorLocation = adUseClient
Err.Clear
On Error GoTo err0
rst.Open ssql, strDSN, adOpenDynamic, adLockOptimistic
Exit Sub
err0:
MsgBox Err.Description
End Sub
'---------------------------------------------------------------------------------
调用
Dim SQLstr As String
SQLstr = "select id,pcname as 计算机名,macaddr as MAC地址,ip as IP地址 from pcmac where pcname like '" & str1 & "'"
Call con(SQLstr)
While Not rst.EOF
str1 = rst("id")
ips = rst("pcname")
rst.MoveNext
Wend
Set rst=Nothing
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询