![](/img/trans.png)
[英]Arithmetic overflow error converting varchar to data type numeric. Error in asp.net
[英]Arithmetic overflow error converting varchar to data type numeric. when trying to read from a database DOES NOT happen every time
我正在尝试从本地数据库中读取数据,并创建并返回一个对象。 我的代码在调用它的循环的前3次中运行良好,然后失败,给了我一个错误:将varchar转换为数值类型的算术溢出错误。 我已经进行了很多调试,并且错误被抛出给reader.Read()。 据我所知,我在那里没有进行任何转换,并且我的所有研究都为该错误找到了完全不同的来源。 任何帮助将不胜感激。
System.Data.SqlClient.SqlDataReader reader;
/*Initialize Variables*/
using (SqlCommand cmd = GetCommand("SELECT * FROM tblInventoryItem where InvoiceID = " + InvoiceNumber))
{
//Do Work...
try
{
cmd.Connection.Open();
reader = cmd.ExecuteReader();
InventoryItem i;
NItem ni;
InventoryDataRepository2 idr = new InventoryDataRepository2();
while (reader.Read())
{
//Create an instance of the object....
i = new InventoryItem();
ni = new NItem();
ni.ItemNumber = (string)reader["ItemNumber"];
InventoryItem invitem = (InventoryItem)idr.GetItem(ni.ItemNumber);
ni.Title = invitem.Item.Title;
ni.Model = invitem.Item.Model;
ni.RetailPrice = invitem.Item.RetailPrice;
ni.WholesalePrice = invitem.Item.WholesalePrice;
i.Item = ni;
i.Quantity = Convert.ToInt32(reader["Quantity"]);
allItems.Add(i);
}
编辑:stacktrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows)
at System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more)
at System.Data.SqlClient.SqlDataReader.Read()
at NeweggDistributor.InventoryItemDataRepository.GetAllItemsInInvoice(String InvoiceNumber) in c:\Users\AJ\Documents\Visual Studio 2012\Projects\NeweggDistributor\NeweggDistributor\InventoryItemDataRepository.cs:line 90
Arithmetic overflow error converting varchar to data type numeric.
我同意EricJ。我不认为Reader.Read()会引发此类错误。 我看到的很可能会引发此类错误,例如,如果DB中的“数量”字段是字符串并且无法转换为整数,或者在DB中为null,因为无法将null转换为任何整数。 其他可能性是RetailPrice和WholesalePrice字段。 也许您可以注释掉这些行,并确保注释掉后不会出现相同的错误。 那么您可以一次取消注释每个字段,一次测试每个字段一个新字段。
我的第一个猜测是i.Quantity = Convert.ToInt32(reader["Quantity"]);
正在尝试将varchar字段转换为int且varchar字符串不是数字。 在这种情况下,我几乎不会使用Convert.ToInt32或C#强制转换,因为它们会引发意外的令人讨厌的错误。 我创建了一个单独的方法来转换我想要的内容,然后将无法传递的默认值传递给它。 因此,我在转换方法中使用了“ defaultValue”参数。
您的查询是
SELECT *
FROM tblInventoryItem
WHERE InvoiceID = 1234
我的猜测是数据库中的InvoiceID
是varchar字段。 这意味着您要在where子句中比较整数和varchar。 隐式转换会将查询更改为
SELECT *
FROM tblInventoryItem
WHERE CONVERT(int, InvoiceID) = 1234
如果数据库中的任何InvoiceID包含不适合整数的内容,您都会收到错误消息。
算术溢出错误最可能导致发票> 2147483648
请注意,如果您的InvoiceNumber
也大于整数,则不会发生该错误。
SELECT *
FROM tblInventoryItem
WHERE InvoiceID = 5000000000
将起作用,因为它已更改为
SELECT *
FROM tblInventoryItem
WHERE CONVERT(bigint, InvoiceID) = 5000000000
解决方案当然是使用参数
command.CommandText = @"SELECT * " +
@"FROM tblInventoryItem " +
@"WHERE InvoiceID = @InvoiceNumber";
command.Parameters.Add("@InvoiceNumber", SqlDbType.VarChar).Value = InvoiceNumber;
(如果您确定InvoiceID仅包含数字,则也可以使用SqlDbtype.BigInt
类型的参数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.