无法将参数值从字符串转换为 int32
本文关键字:字符串 转换 int32 参数 | 更新日期: 2023-09-27 18:34:40
但是,我目前正在尝试完成基于Web的应用程序的交易;
无法将参数值从字符串转换为 int32
这是函数的副本。
public static void completeTransaction(string storeCode, string employeeId, DateTime Date, string itemListNoId)
{
using (SqlConnection conn = new SqlConnection("Data Source = ; Initial Catalog =Business ; Integrated Security = true;"))
{
using (SqlCommand command = new SqlCommand("dbo.completeTransaction", conn))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@storeCode", SqlDbType.Int).Value = storeCode;
command.Parameters.Add("@employeeId", SqlDbType.Int).Value = employeeId;
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = Date;
command.Parameters.Add("@itemListNoId", SqlDbType.Int).Value = itemListNoId;
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
}
}
我的 SQL Server 表包含以下表和类型
storeCode INT
employee INT
Date DATETIME
itemListNoId INT
任何帮助将不胜感激。
我相信
问题出在你的第一个参数(storeCode(上。您正在尝试将字符串作为 int 参数发送。
该行应如下所示:
command.Parameters.Add("@storeCode", SqlDbType.Int).Value = Convert.ToInt32(storeCode);
还有一件可疑的事情:参数的名称是 storeCode,这意味着一个 varchar 列。您尝试作为商店代码传递的值是什么?你确定它是一个整数吗?
其中一个输入是字符串,请检查以下声明:
storeCode
employeeId
itemListNoId
我想storeCode
是一根绳子。 您可以通过将其解析为Int
来解决此问题:
command.Parameters.Add("@storeCode", SqlDbType.Int).Value = int.Parse(storeCode);
但是,如果storeCode
永远不是可寄生的Int
,这将引起问题。
我建议您更改方法中参数的类型。
自
public static void completeTransaction(int storeCode, int employeeId, DateTime Date, string itemListNoId)
并在将值传递给方法之前转换字符串。