VB数据访问通⽤模块(AccessSQLSERVER)
'⼯程-引⽤ Microsoft ActiveX Data Objects 2.8 Library
access是什么意思啊了'DataModule 模块
Function GetAccessConn(ByVal FilePath As String) As ADODB.Connection 'Access DB Connection Dim conn As New ADODB.Connection
conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ="& FilePath
Set GetAccessConn = conn
End Function
Function GetSQLServerConn(ByVal DBName, Server, UserID, Password As String) As ADODB.Connection 'S QL Server DB Connection
Dim conn As New ADODB.Connection
conn.Open "Driver={SQL Server};DataBase="& DBName &";Server="& Server &";UID="& UserID &";PW
D="& Password
Set GetSQLServerConn = conn
End Function
Function GetRecordSet(ByVal SQL As String, ByRef DBConn As ADODB.Connection) As ADODB.Recordset Dim rs As New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open SQL, DBConn, adOpenDynamic, adLockOptimistic
Set GetRecordSet = rs
End Function
Sub ExecuteSQL(ByVal SQL As String, ByRef DBConn As ADODB.Connection)
End Sub
'调⽤⽰例
Private Sub Command1_Click() 'Access
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = DataModule.GetAccessConn("C:aa.mdb")
Set rs = DataModule.GetRecordSet("select top 10 * from [user]", conn)
Set DataGrid1.DataSource = rs
End Sub
Private Sub Command2_Click()'SQL SERVER
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = DataModule.GetSQLServerConn("pubs", "(local)", "sa", "123")
Set rs = DataModule.GetRecordSet("select top 10 * from authors", conn)
Set DataGrid1.DataSource = rs
End Sub
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论