C# Sql错误“算术运算导致溢出。”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1136086/
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
Sql error "Arithmetic operation resulted in an overflow."
提问by David Bo?jak
I am running a service that does some computation and communicates with an ms sql server every minute or so (24/7, uptime is very important) and writes to error log if anything funny happens (like a timeout or lost connection).
我正在运行一个服务,该服务进行一些计算并每分钟左右与 ms sql 服务器通信(24/7,正常运行时间非常重要),如果发生任何有趣的事情(如超时或丢失连接),则写入错误日志。
This works great, however once in a while I will get this error:
这很好用,但是偶尔我会收到此错误:
Arithmetic operation resulted in an overflow.
算术运算导致溢出。
Since this is run on client's side, and the exception has only occurred 3 times since the project has been launched (a couple of months now) this would be extremely hard to catch and debug.
由于这是在客户端运行的,并且自项目启动(现在几个月)以来,异常只发生了 3 次,因此很难捕获和调试。
I am using OleDbDataAdapter to communicate with the server. The data being received from the server was not special in any way, that I am aware of at least! Data should never exceed field sizes etc, so I can't really think of a reason for this error to occur. Again this is extremly hard to verify since I all I get is the error message.
我正在使用 OleDbDataAdapter 与服务器进行通信。从服务器接收的数据在任何方面都不是特别的,至少我知道!数据不应超过字段大小等,因此我真的想不出发生此错误的原因。这再次很难验证,因为我得到的只是错误消息。
My question is: Why does this error usually accrue? I have been unable to find any real information about it on the web, so if someone could supply me with some information, that would be very much appreciated.
我的问题是:为什么通常会出现此错误?我一直无法在网上找到有关它的任何真实信息,因此如果有人可以向我提供一些信息,将不胜感激。
Thank you!
谢谢!
EDIT: A careful read through the error report showed me that this error has actually occurred during the Fill of DataTable object. Code looks something like this:
编辑:仔细阅读错误报告告诉我这个错误实际上是在填充 DataTable 对象期间发生的。代码如下所示:
DataTable.Clear();
try
{
oledbdataAdapter.Fill(DataTable, sqlString);
}
catch (Exception e)
{
//error has occured, report
}
Can anyone make sense of this?
任何人都可以理解这一点吗?
EDIT2: I have just thought of this ... Is it possible this exception would be thrown because the system doesn't have enough system resources to complete the Fill? This is the only reason I can think of that would explain the exception occurring. It would also explain why it only occurs on some servers and never occurs on dev server ...
EDIT2:我刚刚想到了这个......是否有可能因为系统没有足够的系统资源来完成填充而抛出这个异常?这是我能想到的唯一可以解释异常发生的原因。它还可以解释为什么它只发生在某些服务器上而从未发生在开发服务器上......
EDIT3: Here is the whole exception in case it gives anyone any more insight:
EDIT3:这是整个例外,以防它给任何人更多的洞察力:
System.OverflowException: Arithmetic operation resulted in an overflow.
at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32 pos, Boolean fireEvent)
at System.Data.DataTable.LoadDataRow(Object[] values, Boolean fAcceptChanges)
at System.Data.ProviderBase.SchemaMapping.LoadDataRow()
at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataSet dataSet, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
at INDAZajemService.INDAZajem.FillDataSet()
回答by Lima
If you are doing calculations on aggregations on the data then it would appear to be resulting in bigger results then the table structure is expecting.
如果您正在对数据的聚合进行计算,那么它似乎会产生比表结构预期更大的结果。
回答by Dave Markle
It usually happens when you have a number that is too big or too small to fit into a certain data type. So if you had, say, the number 120398018209571037 and tried to put it into an Int32, you would get this error. You'll need to put some more information in your code about where this is happening in order to pin it down. Good luck!
当您有一个太大或太小而无法放入特定数据类型的数字时,通常会发生这种情况。因此,如果您有数字 120398018209571037 并尝试将其放入 Int32 中,则会出现此错误。您需要在代码中添加更多有关发生这种情况的信息,以便将其确定下来。祝你好运!
回答by Sebastián
I was doing a select and filling a DataAdapter
with the results. This code is inside a loop and before the loop ends, I clear my DataTable: varDataTable.Clear()
.
我正在做一个选择并DataAdapter
用结果填充一个。这段代码在一个循环内,在循环结束之前,我清除了我的 DataTable: varDataTable.Clear()
。
ex:
前任:
varStrSQL = "select * from my_table where field_whatever = 2 and id_fk = 4"
varDataAdapter = New SqlDataAdapter(varStrSQL, MyConexion)
varDataAdapter.Fill(varDataTable)
varDataAdapter.Dispose()
But after a 65xxx records, I received the error Arithmetic operation resulted in an overflow
.
但是在 65xxx 记录之后,我收到了错误Arithmetic operation resulted in an overflow
。
I'm not sure, but I think the datatable was "dimensioned" with the initial results. Obviously, my first records were integers.
我不确定,但我认为数据表是用初始结果“标注”的。显然,我的第一个记录是整数。
To solve this I don't clear the DataTableTasas, I release it as follows:
为了解决这个问题,我没有清除 DataTableTasas,我按如下方式释放它:
varDataTableTasas = Nothing
varDataTableTasas = New Data.DataTable
Hope this helps.
希望这可以帮助。
回答by Kamran
I solved the problem when I was using VS.Net 2013 with SQL Server as backend. Just set the project build settings to "x86" instead of "Any cpu". and there you go. But of-course you need to check your requirements first.
我在使用 VS.Net 2013 和 SQL Server 作为后端时解决了这个问题。只需将项目构建设置设置为“x86”而不是“Any cpu”。你去吧。但是,当然,您需要先检查您的要求。
回答by JosephStyons
I had this issue, but only when connecting from a web application running under IIS. The solution in my case was:
我遇到了这个问题,但仅当从 IIS 下运行的 Web 应用程序连接时。在我的情况下的解决方案是:
- Open IIS
- Browse to the application pool used by my application
- Click "Advanced Settings"
- Change the option "Enable 32-Bit Applications" to "True"
- Click "OK"
- 打开 IIS
- 浏览到我的应用程序使用的应用程序池
- 点击“高级设置”
- 将选项“启用 32 位应用程序”更改为“真”
- 点击“确定”
I didn't even have to restart the application pool, the change fixed the problem immediately.
我什至不必重新启动应用程序池,更改立即解决了问题。