|
我使用vb连接sqlserver,连接函数如下:
Public Function ExecuteSQL(ByVal sql As String) As ADODB.Recordset
Dim mycon As New ADODB.Connection
Dim rst As New ADODB.Recordset
Set mycon = New ADODB.Connection
'On Error GoTo err
mycon.ConnectionString = connstring
mycon.CursorLocation = adUseClient
mycon.CommandTimeout = 5
mycon.Open
' Exit Function
' err.Clear
Dim stokens() As String
'On Error GoTo exectuesql_error
stokens = Split(sql)
If InStr("INSER,DELETE,UPDATE", UCase(stokens(0))) Then
mycon.Execute sql
Else
Set rst = New ADODB.Recordset
rst.Open Trim(sql), mycon, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
End If
End Function
Public Function connstring(serverip) As String
connstring = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=e;Initial Catalog=tt;Data Source=(local)"
End Function
调用的时候使用
............
txtsql1 = "select * from person where username='ixcw'"
Set mrc = ExecuteSQL(txtsql1, serverip)
If mrc.EOF = False Then
sss = MsgBox("存在!")
Else
sss = MsgBox("不存在!")
End If
..............
当提供的sqlserver信息正确时,没有问题,但当sql权限有误时就会出现错误,提示sql不存在或连接错误,点确定后,程序就关闭了,想知道如何加上检查sql是否连接成功的代码,并正确中止查询事件,多谢!
|
|