2个回答
推荐于2017-12-16
展开全部
Public Function ConnectString() _
As String
ConnectString = "FileDSN=info.dsn;UID=sa;PWD=23" '连接字符器,改成你自己的
End Function
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset '这里是执行SQL语句,你只需要调用这个方法,把SQL查询语句参数传进来,最后返回一个Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & _
" 条记录 "
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
End Function
Public Function Testtxt(txt As String) As Boolean
If Trim(txt) = "" Then
Testtxt = False
Else
Testtxt = True
End If
End Function
As String
ConnectString = "FileDSN=info.dsn;UID=sa;PWD=23" '连接字符器,改成你自己的
End Function
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset '这里是执行SQL语句,你只需要调用这个方法,把SQL查询语句参数传进来,最后返回一个Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & _
" 条记录 "
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
End Function
Public Function Testtxt(txt As String) As Boolean
If Trim(txt) = "" Then
Testtxt = False
Else
Testtxt = True
End If
End Function
2013-10-20
展开全部
ODBC方法:工程-->引用-->microsoft activeX Data Object 2.6 Library
dim conn as new adodb.connection
dim rs as new adodb.recordset
conn.open "字符串1"
rs.open "字符串2",adopenstatic,adlockoptimistic
'字符串1:用ADO控件的属性中,生成字符串方法,选择数据库的类型,名称,用户名,密码.把生成的字符串COPY到字符串1的位置,然后把ADO控件删除.
'字符串2:是SQL语句.
'首先引用Microsoft Activex Data Objects 2.x Library
Dim objcon As New ADODB.Connection
Dim objrs As New ADODB.Recordset
objcon.Open "Provider=sqloledb.1;user id=sa;password=密码;Initial Catalog=数据库名;Data Source="
objrs.Open "select * from 表 ", objcon, 3, 1
'再部件中添加DataGrid控件
Set DataGrid1.DataSource = objrs
'access
Dim objcon As New ADODB.Connection
Dim objrs As New ADODB.Recordset
objcon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:database password=basic;Data Source=" & App.Path & "\infoTable.mdb"
objrs.Open "select * from infoTable ", objcon, 3, 1
Set DataGrid1.DataSource = objrs
dim conn as new adodb.connection
dim rs as new adodb.recordset
conn.open "字符串1"
rs.open "字符串2",adopenstatic,adlockoptimistic
'字符串1:用ADO控件的属性中,生成字符串方法,选择数据库的类型,名称,用户名,密码.把生成的字符串COPY到字符串1的位置,然后把ADO控件删除.
'字符串2:是SQL语句.
'首先引用Microsoft Activex Data Objects 2.x Library
Dim objcon As New ADODB.Connection
Dim objrs As New ADODB.Recordset
objcon.Open "Provider=sqloledb.1;user id=sa;password=密码;Initial Catalog=数据库名;Data Source="
objrs.Open "select * from 表 ", objcon, 3, 1
'再部件中添加DataGrid控件
Set DataGrid1.DataSource = objrs
'access
Dim objcon As New ADODB.Connection
Dim objrs As New ADODB.Recordset
objcon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:database password=basic;Data Source=" & App.Path & "\infoTable.mdb"
objrs.Open "select * from infoTable ", objcon, 3, 1
Set DataGrid1.DataSource = objrs
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询