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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10675857/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:47:13  来源:igfitidea点击:

Failed To Convert Parameter Value From A String To A Int32

c#sqlsql-server-2008

提问by MasterP

I am currently trying to complete a transaction for a web based app, however;

但是,我目前正在尝试为基于 Web 的应用程序完成交易;

Failed To Convert Parameter Value From A String To A Int32

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

Here is copy of the function.

这是函数的副本。

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();
        }
    }
}

My SQL Server table contains the following tables and types

我的 SQL Server 表包含以下表和类型

storeCode INT
employee INT
Date DATETIME
itemListNoId INT

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by Marko Juvan?i?

I belive the problem is in your first paramter (storeCode). You're trying to send a string as an int paramter.

我相信问题出在您的第一个参数(storeCode)中。您正在尝试将字符串作为 int 参数发送。

That line should read like this:

那行应该是这样的:

command.Parameters.Add("@storeCode", SqlDbType.Int).Value = Convert.ToInt32(storeCode);

There's one more suspicious thing: the parameter's name is storeCode, which implies a varchar column. What's the value you're trying to pass as a storeCode? Are you sure it's an int?

还有一件更可疑的事情:参数的名称是 storeCode,这意味着一个 varchar 列。您尝试作为 storeCode 传递的值是多少?你确定是int?

回答by Tom Gullen

One of the inputs is a string, check the declarations for:

其中一个输入是一个字符串,请检查以下声明:

storeCode
employeeId
itemListNoId

I imagine storeCodeis a string. You can fix this by parsing it as an Int:

我想storeCode是一个字符串。您可以通过将其解析为以下内容来解决此问题Int

command.Parameters.Add("@storeCode", SqlDbType.Int).Value = int.Parse(storeCode);

However this will cause problems if storeCodeis ever no a parasable Int.

但是,如果storeCode没有 parasable ,这将导致问题Int

回答by Lakis

I would suggest you change the type of the parameters in the method.

我建议您更改方法中参数的类型。

to

public static void completeTransaction(int storeCode, int employeeId, DateTime Date, string itemListNoId)

and convert the strings before passing the values to the method.

并在将值传递给方法之前转换字符串。