【问题标题】:C# oledbexceptionC# oledb 异常
【发布时间】:2012-04-05 20:58:42
【问题描述】:

我正在开发一个 C# 项目,以将数据从 excel 文件读取到 Access 数据库中。我不断收到OleDbException 类型的异常。现在的问题不是我为什么会收到这个错误,而是如何处理它。我收到错误是因为我让用户决定他们要上传哪个文件,而某些文件可能没有正确的标题或格式。这是我正在使用的代码:

带有 ** 的行是引发异常的原因。我试过使用:

  1. catch (OleDbException)
  2. catch {}
  3. catch (Exception)

但似乎从未将异常抛出到我的 catch 子句中。

public UploadGrades(string filename, OleDbConnection con)
{
    this.filename = filename;
    this.con = con;
    //connect to the file and retrive all data.
    excelconn = new OleDbConnection(
     @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"");;
   
    try
    {
        excelconn.Open();
        OleDbCommand command = new OleDbCommand("SELECT temp, name, score, submitdate, test from [sheet1$]", excelconn);
        **reader = command.ExecuteReader();**
    }
    catch 
    {
        MessageBox.Show("The File " + filename + " cann't be read.  It is either in use by a different user \n or it doen't contain the " +
            "correct columns.  Please ensure that column A1 is temp B1 is Name C1 is Score D1 is Submitdate and E1 is Test.");
    }
 }

【问题讨论】:

  • 你为什么要传递一个连接到方法然后创建一个新的?另外,您确定您正在到达 reader.ExecuteReader() 行吗?您的连接可能会超时,因此您实际上永远不会到达 ExecuteReader 行,并且,如果连接超时足够长,您可能会在抛出异常之前关闭应用程序。或者它可能会在您创建连接时出现,在尝试捕获之外
  • 将尝试向上移动几行,然后执行 catch(Exception ex) { messagebox.show(ex.message) }
  • 实际上,尝试将连接实例更改为: new OleDbConnection(String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 ;HDR=YES;IMEX=1\"", 文件名));也。您可能还想测试该文件是否存在,并且是一个完整的 excel 文件。
  • @iterationx 可能是对的。否则,windows 正在做一些愚蠢的事情,这种想法不会是第一次。
  • 在 VS 菜单中:Debug + Exceptions,取消勾选。现在你的 catch 处理程序可以工作了 :)

标签: c# oledb


【解决方案1】:

这可能是您的连接字符串有问题,或者您没有安装 ACE.OLEDB 库,因此 OleDB 找不到合适的提供程序。查看此页面以获取alternative connection strings,或者您应该能够从here 下载提供程序。

您可能想尝试以下方法:

try
{

       using(OleDbConnection excelConnection = new OleDbConnection(String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"", filename)))
       {
        excelConnection .Open();     
          using(OleDbCommand command = new OleDbCommand("SELECT columbia_uni, name, score, submitdate, test from [sheet1$]", excelconn))
          {     
                 command.CommandType = CommandType.Text;
                 using(IDataReader reader = command.ExecuteReader())
                 {
                    while(reader.Read())
                    {
                      //Do something
                    }
                 }    
          }
       }


}
catch(Exception e)
{
    MessageBox.Show("The File " + filename + " cann't be read.  It is either in use by a different user \n or it doen't contain the correct columns.  Please ensure that column A1 is Columbia_UNI B1 is Name C1 is Score D1 is Submitdate and E1 is Test.\r\n The actual exception message is: " + e.Message);
}

using 等效于 try/finally,将确保适当地清理连接、命令和 IDataReader 对象。 catch 块应该(几乎)捕获此代码生成的任何异常。

【讨论】:

  • 哇,感谢您的快速响应,我没想到会在 5 分钟内完成。无论如何,我发现了这个问题。每当发生任何异常时,我都会设置中断。如果不是当你去调试 - >异常 - >你会看到我在说什么,这是否有意义。现在它完美无缺。您之前看到,当异常发生时,应用程序将中断并显示异常。
  • 哈——是的,那个。您实际上正在停止潜在的异常!但是,我仍然建议您使用 using 语句来正确管理和处置对象。我还建议不要在字段中保存连接对象,但这取决于您的设计。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
相关资源
最近更新 更多