C# 超出 OleDbException 系统资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/155959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:02:05  来源:igfitidea点击:

OleDbException System Resources Exceeded

提问by Pauly

The following code executes a simple insert command. If it is called 2,000 times consecutively (to insert 2,000 rows) an OleDbException with message = "System Resources Exceeded" is thrown. Is there something else I should be doing to free up resources?

以下代码执行一个简单的插入命令。如果它被连续调用 2,000 次(插入 2,000 行),则会抛出消息 =“超出系统资源”的 OleDbException。我还应该做些什么来释放资源?

using (OleDbConnection conn = new OleDbConnection(connectionString))
using (OleDbCommand cmd = new OleDbCommand(commandText, conn))
{
    conn.Open();
    cmd.ExecuteNonQuery();
}

采纳答案by FlySwat

The system resources exceeded error is not coming from the managed code, its coming from you killing your database (JET?)

系统资源超出错误不是来自托管代码,而是来自您杀死数据库(JET?)

You are opening way to many connections, way to fast...

你正在打开通往许多联系的道路,通往快速的道路......

Some tips:

一些技巧:

  • Avoid roundtrips by not opening a new connection for every single command, and perform the inserts using a single connection.
  • Ensure that database connection pooling is working (Not sure if that works with OLEDB connections).
  • Consider using a more optimized way to insert the data.
  • 通过不对每个命令都打开一个新连接来避免往返,并使用单个连接执行插入。
  • 确保数据库连接池正在工作(不确定这是否适用于 OLEDB 连接)。
  • 考虑使用更优化的方式插入数据。

Have you tried this?

你试过这个吗?

using (OleDBConnection conn = new OleDBConnection(connstr))
{
    while (IHaveData)
    {
        using (OldDBCommand cmd = new OldDBCommand())
        {
            cmd.Connection = conn;
            cmd.ExecuteScalar();
        }
    }
}

回答by Austin Salonen

I tested this code out with an Access 2007 database with no exceptions (I went as high as 13000 inserts).

我用 Access 2007 数据库测试了这段代码,没有任何例外(我高达 13000 次插入)。

However, what I noticed is that it is terribly slow as you are creating a connection every time. If you put the "using(connection)" outside the loop, it goes much faster.

但是,我注意到它非常慢,因为您每次都在创建连接。如果你把“使用(连接)”放在循环之外,它会更快。

回答by Fry

In addition to the above (connecting to the database only once), I would also like to make sure you're closing and disposing of your connections. As most objects in c# are managed wrt memory, connections and streams don't have this luxury always, so if objects like this aren't disposed of, they are not guaranteed to be cleaned up. This has the added effect of leaving that connection open for the life of your program.

除了上述(仅连接到数据库一次)之外,我还想确保您正在关闭和处理您的连接。由于 c# 中的大多数对象都是由内存管理的,连接和流并不总是有这种奢侈,所以如果不处理这样的对象,则不能保证它们会被清除。这具有在程序的生命周期内保持该连接打开的附加效果。

Also, if possible, I'd look into using Transactions. I can't tell what you're using this code for, but OleDbTransactions are useful when inserting and updating many rows in a database.

另外,如果可能的话,我会考虑使用交易。我不知道您使用此代码的目的是什么,但是在插入和更新数据库中的许多行时,OleDbTransactions 很有用。

回答by Krucible

I am not sure about the specifics but I have ran across a similar problem. We utilize an Access database with IIS to serve our clients. We do not have very many clients but there are alot of connections being opened and closed during a single session. After about a week of work, we recieve the same error and all connection attempts fail. To correct the problem, all we had to do was restart the worker processes.

我不确定具体细节,但我遇到了类似的问题。我们利用带有 IIS 的 Access 数据库为我们的客户提供服务。我们没有很多客户端,但是在单个会话期间打开和关闭了很多连接。经过大约一周的工作,我们收到了同样的错误,所有的连接尝试都失败了。为了解决这个问题,我们所要做的就是重新启动工作进程。

After some research, I found (of course) that Access does not perform well in this environment. Resources do not get released correctly and over time the executable will run out. To solve this problem, we are going to move to an Oracle database. If this does not fix the problem, I will keep you updated on my findings.

经过一番研究,我发现(当然)Access 在这种环境中表现不佳。资源没有被正确释放,随着时间的推移,可执行文件将耗尽。为了解决这个问题,我们将迁移到 Oracle 数据库。如果这不能解决问题,我会让你了解我的发现。

回答by jaymeht

This could be occurring because you are not disposing the Connection and Command object created. Always Dispose the object at the end.

这可能是因为您没有处理创建的 Connection 和 Command 对象。始终在最后处理对象。

OledbCommand.Dispose();