2010-04-19 160 views
2

我想從我的C#應用​​程序的訪問數據庫中加載一些數據。但是當它嘗試與數據庫建立連接時,出現此錯誤:無法識別的數據庫格式

Unrecognised database format

如何解決此問題?

這是我的代碼:

private void btnBrowseDB_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.Title = "Kies een databank."; 
    ofd.Filter = "Microsoft Access Databank (*.accdb)|*.accdb"; 
    ofd.RestoreDirectory = true; 

    if (ofd.ShowDialog() == DialogResult.OK) 
    { 
     txtDBLocation.Text = ofd.FileName; 
     loadCampagnes(ofd.FileName); 
    } 
} 

private void loadCampagnes(string pathDB) 
{ 
    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" 
         + "User ID=;" 
         + "Password=;" 
         + "Data Source =" + pathDB + ";"; 
    try 
    { 
     OleDbConnection oleDbConn = new OleDbConnection(connString); 

     string strSQL = "SELECT id, naam " 
         + "FROM Marketingcampagne "; 

     OleDbCommand oleDbCmd = new OleDbCommand(strSQL, oleDbConn); 
     oleDbConn.Open(); 
     OleDbDataReader oleDbReader = oleDbCmd.ExecuteReader(); 

     while (oleDbReader.Read()) 
     { 
      string strCampagneNaam = ""; 
      int intValue; 

      int intField = oleDbReader.GetOrdinal("naam"); 
      if (!oleDbReader.IsDBNull(intField)) 
       strCampagneNaam = oleDbReader.GetString(intField); 

      intField = oleDbReader.GetOrdinal("id"); 
      if (!oleDbReader.IsDBNull(intField)) 
       intValue = oleDbReader.GetInt32(intField); 

      lbCampagnes.Items.Add(strCampagneNaam); 
     } 

     oleDbConn.Close(); 
    } 

    catch (OleDbException ex) 
    { 
     MessageBox.Show("Problemen bij het ophalen van de data (" + ex.Message + ")"); 
    } 

    catch (Exception ex) 
    { 
     MessageBox.Show("Onbekende fout (" + ex.Message + ")"); 
    } 
} 

文森特

PS。我使用Windows 7 64位,但我已將目標CPU更改爲x86。

+0

可以利用你的訪問連接到數據庫?我想我一旦嘗試以64位模式訪問數據庫,就必須從備份中恢復數據庫,然後我能夠以32位模式訪問數據庫。至少我認爲... – 2010-04-19 14:50:06

+0

我在Acces中打開數據庫,發現有安全警告。但我不知道如何擺脫它。 – Vinzcent 2010-04-19 14:57:08

回答

-3
Server svr = new Server(); 

res.Database = "SMO"; 
res.Action = RestoreActionType.Database; 
res.Devices.AddDevice(@"C:\SMOTest.bak", DeviceType.File); 
res.PercentCompleteNotification = 10; 
res.ReplaceDatabase = true; 
res.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler); 
res.SqlRestore(srv); 
+0

這與問題中的任何內容有什麼關係? – Fionnuala 2013-01-15 15:34:00

相關問題