获取错误:“将数据类型nvarchar转换为数字时出错”但在我的PC中工作正常 [英] Getting The Error: “Error Converting Data Type nvarchar to Numeric” But Works Fine In My Pc
问题描述
我正在制作市场计划。当一个项目被选中时,程序会自动减少该项目从存储中的数量。我正在使用SQL Server,为了减少特定项目的数量,我减少了条形码与当前
项目条形码相同的数字。数据库中的条形码类型是nvarchar,在程序中我使用字符串形式的条形码。据我所知,字符串类型可以与SQL中的nvarchar进行比较。有趣的是,程序在我的电脑中没有问题! pcs与减少数字的SQL代码之间的差异
如下所示。关于RAM还是什么问题?
I'm making a market program. When an item selled, program automatically decreases the number of that item from the storage. I'm using SQL Server and in order to decrease the spesific item's number, I decrease the number whose barcode is the same with current item's barcode. The barcode type in the database is nvarchar and in the program I use the barcode in string form. As far as I know string type is okay to compare with the nvarchar in SQL. Interestingly, program is working without a problem in my pc! Difference between pcs and the SQL code which decrease the number is given below. Can there be any problem about RAM or something?
差异:
-
客户端的RAM远低于我的(不确切多少) -
客户端是Windows 7,我的是Windows 10。 -
我只有SQL Server 2012在我的电脑,但客户端也有SQL Server 2008(只要我完全信任我的程序,因为这个补丁适用于他们之前使用的系统。) -
客户端的pc具有更低的磁盘空间。
代码:
SqlCommand cmdMinuesPiece = new SqlCommand("Update Storage set piece='" + newPiece.ToString() + "' where barcode = " + barcode + "", connection);
cmdMinuesPiece.ExecuteNonQuery(); //Barcode is in the string form and newPiece is in the integer form.
注意:问题正好在这部分,我尝试了调试。
Note: The problem is exactly in this part, I tried with Debugging.
推荐答案
普通计算机可以运行此代码但建议使用如下所示的参数。"未知"部分取自
here 。
using System.Data.SqlClient;
using BaseLibrary;
namespace WindowsFormsApp3
{
public class DataOperations : BaseSqlServerConnections
{
public void UpdateWithParams(int piece, string barcode)
{
var updateStatement = "UPDATE Storage SET piece=@piece WHERE barcode = @barcode";
using (var cn = new SqlConnection {ConnectionString = ConnectionString})
{
using (var cmd = new SqlCommand() {Connection = cn, CommandText = updateStatement })
{
cmd.Parameters.AddWithValue("@piece", piece);
cmd.Parameters.AddWithValue("@barcode", barcode);
cn.Open();
cmd.ExecuteNonQuery();
}
}
}
}
}
如果SQL-Server与您的计算机不同确保表的模式与您的模式匹配,如果基于服务器的数据库,请忽略此。
If the SQL-Server is different from your computer make sure the schema for the table matches up to yours, ignore this if server based database.
这篇关于获取错误:“将数据类型nvarchar转换为数字时出错”但在我的PC中工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!