我想从我的c-应用程序中的access数据库加载一些数据。但当它试图与数据库建立连接时,我会得到以下错误:
无法识别的数据库格式
我该怎么解决?
这是我的代码:
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 + ")");
}
}
文森特
我使用Windows764位,但是我已经把目标CPU改成了x86。
最佳答案
对于accdb,您需要:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
--http://www.connectionstrings.com/access-2007