2010-01-11 107 views
0

你好當我運行我的服務器應用程序,連接不打開 - >我的數據集仍然關閉ASP.NET數據庫連接

Dim strconnect As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + "rootPath" + "\" + "VSS_TESTDB.mdb" + "Persist Security Info=False" 


Dim objConnection As New OleDbConnection(strconnect) 

Dim sql As String = "SELECT VSS_Files.id, VSS_Files.filename,VSS_Files.dateOfCreation,VSSDirs.dir FROM VSS_Files , VSSDirs Where VSS_Files.dir_id = VSSDirs.id;" 


Dim cmd As New OleDbCommand(sql, objConnection) 

Dim myDataReader As OleDbDataReader 
     myDataReader = cmd.ExecuteReader() 

我能做些什麼?

問候tyzak

回答

1

您需要創建使用OleDbConnectionStringBuilder連接到數據庫的一個OleDbConnection。

例如:

Dim builder As New OleDbConnectionStringBuilder 
builder.Provider = "Microsoft.Jet.OLEDB.4.0" 
builder.DataSource = Path.Combine(rootPath, "VSS_TESTDB.mdb") 
builder.PersistSecurityInfo = False 

Using connection As New OleDbConnection(builder.ToString()) 
Using command As New OleDbCommand("SELECT VSS_Files.id, VSS_Files.filename,VSS_Files.dateOfCreation,VSSDirs.dir FROM VSS_Files, VSSDirs Where VSS_Files.dir_id = VSSDirs.id;", connection) 
    connection.Open() 
    Using reader As OleDbDataReader = command.ExecuteReader() 
     'Do something 
    End Using 
End Using 

編輯:你的問題很可能是你把引號rootPath。連接字符串的Data SourceDataSource=rootPath\VSS_TESTDB.mdb。我假設你實際上希望它具有rootPath變量的

另外,您需要打開連接。

最後,您應該使用Using聲明關閉連接和DataReader。

查看我更新的示例。

+0

嗨,對不起,我發佈了一些更多的代碼: Dim strconnect As String =「Provider = Microsoft.Jet.OLEDB.4.0;」 +「DataPath =」+「rootPath」+「\」+「VSS_TESTDB.mdb」+「Persist Security Info = False」 Dim objConnection As New OleDbConnection(strconnect) Dim sql As String =「SELECT VSS_Files.id ,VSS_Files.filename,VSS_Files.dateOfCreation,VSSDirs.dir FROM VSS_Files,VSSDirs其中VSS_Files.dir_id = VSSDirs.id;「 昏暗CMD作爲新的OleDbCommand(SQL,objConnection) 昏暗myDataReader作爲OleDbDataReader myDataReader = cmd.ExecuteReader() – Tyzak

0

這個問題是相當模糊,就很難正確地從一行代碼診斷。這裏有一些建議:

  1. 您需要將此連接字符串賦值給一個連接對象。

  2. 請參閱http://www.connectionstrings.com/瞭解連接字符串的完整形式。