2014-03-18 105 views
1

我有一個VB.NET函數,我想連接它用於連接到SQL Server數據庫並返回記錄集。我是VB和SQL的新手,所以我希望這很簡單,但我確信我的連接字符串和SQL查詢是正確的,因爲他們是由隊友檢查的。我有異常信息的副本VBA中的ADODB連接問題

' 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

在您的連接字符串中,您必須提供您使用哪個驅動程序?例如oledb,oracleoledb等.. –

回答

1

在這裏你試試。

m_sConnStr = "Provider='SQLOLEDB';Data Source='MySqlServer';" & _ 
"Initial Catalog='Northwind';Integrated Security='SSPI';" 

Connection String for VBA

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