2011-07-11 41 views
0
一個MS Access數據庫視圖檢索行

我已經成功地得到了下面的代碼...使用Vb.Net

  con.ConnectionString = My.Settings.dbConnection 
     Dim sqlCmd As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand() 
     con.Open() 
     sqlCmd.Connection = con 

     Dim schemaTable As DataTable 
     schemaTable = con.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Views, Nothing) 

要檢索我的Access數據庫視圖的列表,但現在我想根據選定的視圖檢索結果。

這樣做是否有正確的方法,或者我從DataTable中爲每行返回SQL語句?

回答

-1

假設您在Access數據庫(Database1.accdb文件)中有Query1(View)。下面的代碼將輸出查詢的每一行控制檯(用於演示目的):

Dim con As OleDbConnection = New OleDbConnection() 

    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Database1.accdb;Persist Security Info=False;" 
    Dim sqlCmd As OleDbCommand = New System.Data.OleDb.OleDbCommand() 

    sqlCmd.CommandType = CommandType.StoredProcedure 
    sqlCmd.CommandText = "Query1" 
    sqlCmd.Connection = con 

    con.Open() 

    Dim reader As OleDbDataReader 

    reader = sqlCmd.ExecuteReader() 

    If reader.HasRows Then 
     While reader.Read() 
      Console.WriteLine(reader("Column1")) 'output specific column 
     End While 
    End If 

    Console.ReadLine() 

希望這有助於

+0

-1代表'Using' –