vb链接sqlsever数据库代码怎么写?

2024-12-22 22:58:31
推荐回答(3个)
回答1:

先引用 "Microsoft ActiveX Data Objects 2.0 Library"

Public conn As ADODB.Connection
Public rst As ADODB.Recordset

Set conn = New ADODB.Connection
Set rst = New ADODB.Recordset
conn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=数据库名;Data Source=数据库名"
conn.CursorLocation = adUseClient
conn.Open
rst.Open "select * from 数据库名.dbo.表名", cnn, adOpenStatic, adLockPessimistic

----------------------------------
VB6.0远程连接SQL SERVER2000
Public conn As ADODB.Connection '数据连接

Set conn = New ADODB.Connection
conn.Open "{SQL SERVER};SERVER=192.168.1.11;DATABASE=数据库名;USER ID=打开数据库用户名;PASSWORD=打开数据库密码"

回答2:

在模块中建立SJK过程:
Sub SJK(db)
'连接数据库
db.ConnectionTimeout = 10
db.CursorLocation = adUseServer
db.ConnectionString = "driver={SQL Server};server=服务器名称;uid=sa;pwd=密码;database=数据库名"
db.Open
End Sub

在启动页:
Private Sub Form_Load()
On Error GoTo ABC
Call SJK(db)
db.Close
Exit Sub
ABC:
MsgBox "数据库链接失败!系统不能继续,请检查数据库链接!", , "严重错误!"
End Sub

回答3:

Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;" _
+ "User ID=sa;password=123456;Initial Catalog=student;Data Source=127.0.0.1"
conn.Open
这些就能连接到数据库了还需要其他什么吗??
按照你的代码看来只是连接到数据库其他的什么都没做。。。。