VB如何连接数据库
我想问问大侠,如何用VB连接Access数据库呢?
[解决办法]
Public gsOdbcName As String
Public ADOsdb As new ADODB.Connection
gsOdbcName = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= " _
& app.path & "\db.mdb " & "; "
With ADOsdb
If .State <> adStateOpen Then
.CursorLocation = adUseClient
.ConnectionString = gsOdbcName
.Open
End If
End With
[解决办法]
在窗体代码的最前面写
Dim db As ADODB.Connection
Dim rec As ADODB.Recordset
然后在具体要用到db和rec时再写
set db=new adodb.connection
set rec=new adodb.recordset及相关代码
[解决办法]
设置ADODB数据库连接池。在控制面板管理工具里,设置SQL数据库,然后VB里如下编写:
Dim conn As New adodb.Connection
Dim cmd As New adodb.Command
Dim rs As New adodb.Recordset
Dim sql As String
conn.ConnectionString = "driver={sql server};server=计算机名;uid=sa;pwd=;database=数据库的名字
conn.ConnectionTimeout = 30
conn.Open
sql = "....... "
cmd.CommandText = sql
rs.Open cmd, , adOpenKeyset