我想從我的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。
可以利用你的訪問連接到數據庫?我想我一旦嘗試以64位模式訪問數據庫,就必須從備份中恢復數據庫,然後我能夠以32位模式訪問數據庫。至少我認爲... – 2010-04-19 14:50:06
我在Acces中打開數據庫,發現有安全警告。但我不知道如何擺脫它。 – Vinzcent 2010-04-19 14:57:08