我有一个VB.NET函数,我想连接它以用于连接到SQL Server数据库并返回记录集。我是VB和SQL的新手,所以我希望它很简单,但我确信我的连接字符串和SQL查询是正确的,因为它们是由队友检查的。我在代码
后面有一个Exception消息的副本' Retrieve Record Set
Private Function GetRecords() As ADODB.Recordset
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
With conn
.ConnectionString = "Server=12.345.678.9;Database=db01;User Id=userid01;Password=passwordyword"
.Open() ' Exception Thrown Here
rs.Open("SELECT * FROM [Table] ;", "sql")
rs.Close()
.Close()
End With
Return rs
End Function
异常消息:
An exception of type 'System.Runtime.InteropServices.COMException' occurred in ThisProject.dll but was not handled in user code
Additional information: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
任何和所有帮助将不胜感激!谢谢!
答案 0 :(得分:1)
在这里尝试。
m_sConnStr = "Provider='SQLOLEDB';Data Source='MySqlServer';" & _
"Initial Catalog='Northwind';Integrated Security='SSPI';"
答案 1 :(得分:1)
这应该有效。请注意,连接也需要在rs.Open中传递
Private Function GetRecords() As ADODB.Recordset
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
With conn
.ConnectionString = "Driver={SQL Server};Server=12.345.678.9;Database=db01;Uid=userid01;Pwd=passwordyword;"
.Open() ' Exception Thrown Here
rs.Open("SELECT * FROM [Table] ;", conn)
rs.Close()
.Close()
End With
Return rs
End Function