0

UPDATEAccess データベースでクエリを実行するコードがいくつかあります。うまくいく場合もあれば、次のエラーが発生する場合もあります。

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();

これの原因は何ですか?なぜ時々しか機能しないのですか?コードの実行中に実際の .accdb ファイルを Access で開いていません。

ありがとう!

編集: 上記のコードを実行する前に、このコードを問題なく実行します。

        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(); 
4

1 に答える 1