簡體   English   中英

偶爾錯誤打開.accdb數據庫

[英]Occasional error opening .accdb database

我有一些代碼在Access數據庫中執行UPDATE查詢。 有時它有效,有時我得到這個錯誤:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Cannot open database ''.  It may not be a database that your application recognizes, or the file may be corrupt.

這是代碼:

string[] lines = File.ReadAllLines(file, Encoding.GetEncoding(1252));   //read as ANSI encoding

OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=c:\\myDatabase.accdb");
con.Open();

for (int i = 2; i < lines.Length - 1; i++)  //ignore the first 2 lines
{
    string line = lines[i];         //get the current line

    string[] values = line.Split('\t');     //split line at the tabs

    using (OleDbCommand com = new OleDbCommand("INSERT INTO [myTable](" +
        "[Field1], [Field1], [Field1], [Field1], [Field1], [Field1], [Field1], [Field1], [Field1], [Field1], " +
   "[Field1], [Field1], [Field1], [Field1], [Field1], [Field1], [Field1], [Field1], [Field1], [Field1]" +
   "[Field1], [Field1], [Field1]" +
   ") VALUES (" +
   "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?" +
   ")", con))
    {
        for (int y = 0; y < 23; y++)
   {
       if (values[y] != "")
       {
           com.Parameters.Add("@p" + y + "", OleDbType.Char, 255).Value = (object)values[y] ?? DBNull.Value;
       }
       else
       {
           com.Parameters.Add("@p" + y + "", OleDbType.Char, 255).Value = DBNull.Value;
       }
        }     
        com.ExecuteNonQuery();
    }
}
con.Close();

知道是什么導致了這個嗎? 為什么它只在某些時候有效? 我在運行代碼時沒有在Access中打開實際的.accdb文件。

謝謝!

編輯:在運行上面的代碼之前,它運行此代碼就好了:

        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=c:\\myDatabase.accdb");
        con.Open();

        string strCreate = "CREATE TABLE [myTable](" +
                            "[Field1] Text(255), " +            
                            "[Field1] Text(255), " +          
                            "[Field1] Text(255), " +             
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255), " +
                            "[Field1] Text(255))";              

        OleDbCommand cCom = new OleDbCommand(strCreate, con);
        cCom.ExecuteNonQuery();

        cCom.Connection.Close(); 

你能抓住異常和內部異常嗎? 它應該對這個問題有所了解:

    try
    {
        // Your Code Here
    }
    catch (OleDbException e)
    {
        Messagebox.Show(e.InnerException.Message);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM