无法识别的数据库格式

时间:2010-04-19 14:35:05

标签: c# database ms-access

我想在C#-application中从access-database加载一些数据。但是当它试图与数据库建立连接时,我收到了这个错误:

  

无法识别的数据库格式

我该如何解决这个问题?

这是我的代码:

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。

2 个答案:

答案 0 :(得分:5)

对于accdb,您需要:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;

- http://www.connectionstrings.com/access-2007

答案 1 :(得分:-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);
相关问题