简体   繁体   English

sql中的“未指定错误”几秒钟后消失

[英]“Unspecified error” in sql that goes away after few seconds

I built a parser that takes data stored in an xml file and sends it into a Microsoft Access database using linq-to-sql. 我构建了一个解析器,该解析器使用xml文件中存储的数据并将其使用linq-to-sql发送到Microsoft Access数据库中。 I have the sql insert commands and they work... until they don't. 我有sql插入命令,它们可以工作...直到没有。

It's odd, I have each SQL command run (I keep them in a List and execute each command one at a time) and the first 40 or so run fine until they start hitting "unspecified error"s. 奇怪的是,我运行了每个SQL命令(我将它们保存在一个列表中并一次执行一个命令),并且前40个左右的命令运行良好,直到它们开始遇到“未指定的错误”为止。 The thing is, if I swallow the exception and instead have the exception catcher keep retrying, after a few seconds, they start working again. 问题是,如果我吞下了异常,而是让异常捕获器继续重试,则在几秒钟后,它们又开始工作。 This means it's not an error of the SQL query itself (or at least how it's written). 这意味着这不是SQL查询本身的错误(至少不是它的编写方式)。

This pattern repeats (there are thousands of inserts) many times. 此模式重复多次(有数千个插入)。 If I do normal exception handling, the program will just skip a few records while the error happens and keep inserting when whatever causes it temporarily goes away. 如果我执行正常的异常处理,则程序将在错误发生时跳过一些记录,并在任何原因暂时消失时继续插入。 if I let it run it's course, it inserts some records, skips some, inserts, skips, repeat and eventually inserts less than 2/3 of the records. 如果我让它正常运行,它将插入一些记录,跳过一些记录,插入,跳过,重复并最终插入少于2/3的记录。

Is there any reason why my computer would only run 40 or so Inserts and then refuse to run more for a random but short interval? 有什么理由为什么我的计算机将只运行40个左右的插入,然后拒绝以随机但短暂的间隔运行更多?

I'm at a loss on what could be causing this. 我对可能造成这种情况的原因不知所措。

The application is natively run; 该应用程序本机运行; it does not use any server/web communication and all I found when looking for "unspecified error" pointed me to occurrences in ADO.NET applications. 它不使用任何服务器/ Web通信,在查找“未指定的错误”时发现的所有内容都将我指向ADO.NET应用程序中。

Here's the code the error happens in: 这是发生错误的代码:

public static string insertQuery(string sql)
    {
        string connetionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Owner\Desktop\Arbeit\TrademarkParserproject1\TrademarkParserproject\bin\x86\Debug\Database.accdb";
        OleDbConnection connection;
        OleDbDataAdapter oledbAdapter = new OleDbDataAdapter();
        connection = new OleDbConnection(connetionString);

        string success = "false";

        try
        {
            connection.Open();
            oledbAdapter.InsertCommand = new OleDbCommand(sql, connection);
            oledbAdapter.InsertCommand.ExecuteNonQuery();

        }

        catch (Exception ex)
        {
            success = ex.ToString();
            return success;
        }

        success = "true";
        return success;
    }

Note, I have the application running in X86 mode to avoid errors with the ACE.OLEDB.12.0 adapter. 注意,我有在X86模式下运行的应用程序,以避免ACE.OLEDB.12.0适配器出错。

One thing that stands out, is you never close/dispose your SqlConnection. 突出的一件事是,您永远不要关闭/配置SqlConnection。 OleDbDataAdapter is also disposable and should be disposed. OleDbDataAdapter也是一次性的,应该丢弃。 A 'using' statement is a convenient construct here: “ using”语句在这里很方便:

public static string insertQuery(string sql)
{
    string connetionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Owner\Desktop\Arbeit\TrademarkParserproject1\TrademarkParserproject\bin\x86\Debug\Database.accdb";
    using(var oledbAdapter = new OleDbDataAdapter())
    using(var connection = new OleDbConnection(connetionString))
    {
        string success = "false";

        try
        {
            connection.Open();
            oledbAdapter.InsertCommand = new OleDbCommand(sql, connection);
            oledbAdapter.InsertCommand.ExecuteNonQuery();
        }

        catch (Exception ex)
        {
            success = ex.ToString();
            return success;
        }

        success = "true";
        return success;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM