VB连接SQL数据库出错
OptionExplicitDimconnAsADODB.ConnectionDimrsAsADODB.RecordsetPrivateSubForm_Load()Set...
Option Explicit
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub Form_Load()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "Provider=SQLOLEDB.1;Data Source=J:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\stc.mdf;Persist Security Info=true;User ID=sa;Initial Catalog=stc"
conn.Open
rs.Open "student", conn, , adCmdTable
'rs = Execute("select * from student")
Set my.DataSource = rs
rs.Close
conn.Close
End Sub 展开
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub Form_Load()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "Provider=SQLOLEDB.1;Data Source=J:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\stc.mdf;Persist Security Info=true;User ID=sa;Initial Catalog=stc"
conn.Open
rs.Open "student", conn, , adCmdTable
'rs = Execute("select * from student")
Set my.DataSource = rs
rs.Close
conn.Close
End Sub 展开
1个回答
展开全部
连接字符串写错了,datasource不是写物理文件地址,而是你安装sqlserver的那台服务器地址或者机器名等。
如data source = localhost 调用本机的库,老兄对sqlserver的基本概念理解有错误,建议去看看,嫌麻烦参照别人的连接字符串写比较好。
Provider=SQLOLEDB.1;Password=[密码];User ID=[用户名];Initial Catalog=[初始库];Data Source=[数据库服务器地址]
如data source = localhost 调用本机的库,老兄对sqlserver的基本概念理解有错误,建议去看看,嫌麻烦参照别人的连接字符串写比较好。
Provider=SQLOLEDB.1;Password=[密码];User ID=[用户名];Initial Catalog=[初始库];Data Source=[数据库服务器地址]
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询