如何用vb 连接sql server 2000

2024-11-21 17:25:05
推荐回答(1个)
回答1:

'引用ADO(Microsoft ActiveX Data Objects 2.X Library)
Private Sub Command1_Click()
On Error GoTo err
Dim cn As New ADODB.Connection, rs As New ADODB.RecordSet
'混合验证方式的连接:
'cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=登陆用户名;Password=登录密码;Initial Catalog=数据库名;Data Source=服务器别名"
'集成验证的连接:
cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=数据库名;Data Source=服务器别名"
cn.Open
rs.CursorLocation=adUseClient'设置客户端游标
rs.Open "select * from 表名称", cn, 3, 2
Set DataGrid1.DataSource=rs '把记录显示到DataGrid1里
Exit Sub
err:
MsgBox err.Description
End Sub