请问各位大虾,在b中如何使用ado访问sqlserver,最好能把具体的代码给出来,希望各位能不吝赐教。
请问各位大虾,在b中如何使用ado访问sqlserver,最好能把具体的代码给出来,希望各位能不吝赐教。
我是做了一个公共模块,在模块中用程序连接数据库,具体代码如下:但就是连接不上,不知道哪里错了
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.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 ConnectionString
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
'getRecordCount
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 ConnectionString() As String
ConnectionString = "Provider=SQLOLEDB.1;DataSource=H:\西译中学\data\studentsystem.MDF"
End Function
在vb中用ado连接sqlserver数据库的问题已经解决:
Public Function ConnectionString() As String
ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=studentsystem;Data Source=(local)"
End Function
使用vb里面的ado控件生成一个连接串,然后把这个连接串放到程序里就可以了