Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 打开.accdb数据库时偶尔出错_C#_Ms Access - Fatal编程技术网

C# 打开.accdb数据库时偶尔出错

C# 打开.accdb数据库时偶尔出错,c#,ms-access,C#,Ms Access,我有一些代码在Access数据库中执行更新查询。有时它有效,有时我会出现以下错误: 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 t

我有一些代码在Access数据库中执行
更新
查询。有时它有效,有时我会出现以下错误:

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

您能捕获异常和内部异常吗?它应该对这个问题有所启发:

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

它在
com.ExecuteNonQuery()处失败
这给了我一个错误
myApp.exe中发生了“System.NullReferenceException”类型的未处理异常附加信息:对象引用未设置为对象的实例。
@JeffBrady如何处理e.Message而不是e.InnerException.Message?@JeffBrady,Access数据库上的文件大小是多少?它只会给出相同的错误:
无法打开数据库“”。您的应用程序可能无法识别该数据库,或者该文件可能已损坏。
@JeffBrady这正是我所担心的,JET以前有2GB的限制。如果您的插入超出了该阈值,则可能是罪魁祸首。如果可行的话,您可以考虑将迁移迁移到SQL数据库(您可以将数据存储在那里,并在访问SQL中的链接中有参考表——从用户的角度来看没有变化)。
    try
    {
        // Your Code Here
    }
    catch (OleDbException e)
    {
        Messagebox.Show(e.InnerException.Message);
    }