c# - 无法将参数值从字符串转换为 Int32

标签 c# sql sql-server-2008

不过,我目前正在尝试为基于网络的应用程序完成交易;

Failed To Convert Parameter Value From A String To A 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 传递的值是多少?你确定它是一个整数吗?

关于c# - 无法将参数值从字符串转换为 Int32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10675857/

相关文章:

sql-server - 数据库设计: Is it a good idea to combine two tables with the same columns and too many data?

c# - ObservableCollection 与异步 Web 服务调用的普通列表

SQL to_char() 将 2 位数字打印为 0xx 并且不触及 3 位数字

sql - 从 ADF 中的 @item().name 中删除 .csv

sql-server-2008 - 将 SQL Server 2008 查询分解为多个批处理

sql-server - 触发处理多个插入

c# - 带有 AllowColumnReorder 的 ListView OwnerDraw 不能正常工作

c# - 模仿 .NET 中的 MS Access 或 SQL Server ODBC 服务器

c# - 使用 Simple Injector 将依赖项注入(inject) OWIN 中间件和每个 Web 请求

sql - Concat 函数不起作用 - 参数数量无效