rst.Open Trim$(sql), cnn, adOpenKeyset, adLockOptimistic为什么执行到这块老出错?
整段代码如下:PublicFunctionExecuteSQL(ByValsqlAsString,ByValmsgstringAsString)AsADODB.Recor...
整段代码如下:
Public Function ExecuteSQL(ByVal sql As String, ByVal msgstring As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
Dim conn As String
Dim txtSQL As String
Dim msgtext As String
On Error GoTo Executesql_error
sTokens = Split(sql)
Set cnn = New ADODB.Connection
conn = "DRIVER=SQL Server;SERVER=YEYU-PC\SQLEXPRESS;UID=sa;PWD=;APP=Microsoft? Windows? Operating System;WSID=YEYU-PC;DATABASE=master"
cnn.Open conn
If Trim(sql) = "" Then
MsgBox "传入的sql语句为空,不能执行!"
End If
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute sql
msgstring = sTokens(0) & "查询成功"
Exit Function
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(sql), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
msgstring = "查询到" & rst.RecordCount & "条记录"
Exit Function
End If
Executesql_exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
Executesql_error:
msgstring = "查询错误:" & Err.Description
Resume Executesql_exit
End Function
执行到这块rst.Open Trim$(sql), cnn, adOpenKeyset, adLockOptimistic
直接跳到 msgstring = "查询错误:" & Err.Description
不知道是与数据库连接有问题,还是?
sql="select uid,pwd form master.dbo.Table_1 where (username='" & txtusername.Text & "')" 展开
Public Function ExecuteSQL(ByVal sql As String, ByVal msgstring As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
Dim conn As String
Dim txtSQL As String
Dim msgtext As String
On Error GoTo Executesql_error
sTokens = Split(sql)
Set cnn = New ADODB.Connection
conn = "DRIVER=SQL Server;SERVER=YEYU-PC\SQLEXPRESS;UID=sa;PWD=;APP=Microsoft? Windows? Operating System;WSID=YEYU-PC;DATABASE=master"
cnn.Open conn
If Trim(sql) = "" Then
MsgBox "传入的sql语句为空,不能执行!"
End If
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute sql
msgstring = sTokens(0) & "查询成功"
Exit Function
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(sql), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
msgstring = "查询到" & rst.RecordCount & "条记录"
Exit Function
End If
Executesql_exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
Executesql_error:
msgstring = "查询错误:" & Err.Description
Resume Executesql_exit
End Function
执行到这块rst.Open Trim$(sql), cnn, adOpenKeyset, adLockOptimistic
直接跳到 msgstring = "查询错误:" & Err.Description
不知道是与数据库连接有问题,还是?
sql="select uid,pwd form master.dbo.Table_1 where (username='" & txtusername.Text & "')" 展开
展开全部
conn = "DRIVER=SQL Server;SERVER=YEYU-PC\SQLEXPRESS;UID=sa;PWD=;APP=Microsoft? Windows? Operating System;WSID=YEYU-PC;DATABASE=master"
几个???怎么说?
也不推荐你这样的做法去打开recordset
conn应该做个全局变量打开一次就行了,
平时按不同的句子需要修正 adOpenKeyset, adLockOptimistic 等参数
提高代码可读性和易维护性。
几个???怎么说?
也不推荐你这样的做法去打开recordset
conn应该做个全局变量打开一次就行了,
平时按不同的句子需要修正 adOpenKeyset, adLockOptimistic 等参数
提高代码可读性和易维护性。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
出什么错呢?
sql的值是什么
先去掉On Error GoTo Executesql_error语句
出错时看看sql 的值,是不是把表名字或者字段写错了
怎么表的名字是master.dbo.Table_1啊?中间有这么多的点,如果没错,试试这样
sql="select uid,pwd form [master.dbo.Table_1] where (username='" & txtusername.Text & "')"
sql的值是什么
先去掉On Error GoTo Executesql_error语句
出错时看看sql 的值,是不是把表名字或者字段写错了
怎么表的名字是master.dbo.Table_1啊?中间有这么多的点,如果没错,试试这样
sql="select uid,pwd form [master.dbo.Table_1] where (username='" & txtusername.Text & "')"
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询