使用C#中的参数执行存储过程时出错[重复]

时间:2021-08-22 15:29:23

This question already has an answer here:

这个问题在这里已有答案:

I've got a stored procedure that runs just fine if I execute it on my server and if I build an execute statement with my parameters (e.g. string sql = "Exec Get_Data '" + St + " ' ...". However, as soon as I try:

我有一个存储过程运行得很好,如果我在我的服务器上执行它,如果我用我的参数构建一个执行语句(例​​如字符串sql =“Exec Get_Data'”+ St +“'...”。但是,我一试:

string strQS = "Exec Get_Data @param1,@param2,@param3,@param4..."

using (SqlConnection conSQL = new SqlConnection(connectionString))
{
    using (SqlCommand cmdSQL = new SqlCommand(strQS, conSQL))
    {
        conSQL.Open();

        cmdSQL.Parameters.AddWithValue("@param1", SqlDbType.VarChar).Value = St;
        cmdSQL.Parameters.AddWithValue("@param2", SqlDbType.VarChar).Value = Loc;
        ...

I get the following error:

我收到以下错误:

Column name or number of supplied values does not match table definition.

列名或提供的值数与表定义不匹配。

Obviously if I can run it before I use a parametrized query I don't have anything wrong with my column names, but something with how my values are being treated. All my variables are of type string and the SQL Server is expecting all parameters to by of type varchar...Any ideas?

显然,如果我在使用参数化查询之前运行它,我的列名称没有任何问题,但是我的值如何被处理。我的所有变量都是字符串类型,SQL Server期望所有参数都是类型varchar ...任何想法?

1 个解决方案

#1


1  

You need to change your command type to StoredProcedure.

您需要将命令类型更改为StoredProcedure。

And then drop the EXEC and all the parameters from your string.

然后删除EXEC和字符串中的所有参数。

cmdSQL.CommandType = CommandType.StoredProcedure;
string strQS = "Get_Data"

#1


1  

You need to change your command type to StoredProcedure.

您需要将命令类型更改为StoredProcedure。

And then drop the EXEC and all the parameters from your string.

然后删除EXEC和字符串中的所有参数。

cmdSQL.CommandType = CommandType.StoredProcedure;
string strQS = "Get_Data"