存储过程的SQL错误

时间:2013-06-10 12:34:35

标签: c# sql stored-procedures

我通过我的c#代码在sql表中插入行,该代码调用了一个存储过程。

C#代码:

SqlCommand myCommand = thisConnection.CreateCommand();
                myCommand.CommandText = "FederationUpdateCTRAndImpressionCountsForAllYPIds";
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@bid", SqlDbType.UniqueIdentifier);
                myCommand.Parameters.Add("@uid", SqlDbType.UniqueIdentifier);
                myCommand.Parameters.Add("@imp", SqlDbType.VarChar);
                myCommand.Parameters.Add("@ctr", SqlDbType.VarChar);

                while (myfederationReader.Read())
                {                      
                    myCommand.Parameters["@bid"].Value = myfederationReader["BusinessId"];
                    myCommand.Parameters["@uid"].Value = myfederationReader["UId"];
                    myCommand.Parameters["@imp"].Value = myfederationReader["Impression"];
                    myCommand.Parameters["@ctr"].Value = myfederationReader["CTR"];                    
                    rowsAffected = myCommand.ExecuteNonQuery();                        
                }

存储过程:

CREATE PROCEDURE [dbo].[FederationUpdateCTRAndImpressionCountsForAllYPIds]
    @bid uniqueidentifier,
    @uid uniqueidentifier,
    @imp varchar(255),
    @ctr varchar(255)
AS  BEGIN   
    UPDATE  BasicBusinessInformation
    SET     BasicBusinessInformation.CTR = @ctr , BasicBusinessInformation.Impression = @imp
    WHERE   BasicBusinessInformation.BusinessId = @bid AND  BasicBusinessInformation.UId = @uid 
    END

执行它时,会报告以下错误:

  

过程没有参数和参数提供

3 个答案:

答案 0 :(得分:2)

尝试清除命令参数

[C#] 
public bool ExportAndClear() {
SqlParameter[] myParamArray = new SqlParameter[myCmd.Parameters.Count - 1];
myCmd.Parameters.CopyTo(myParamArray, 0);
myCmd.Parameters.Clear();
return true;
}

答案 1 :(得分:0)

尝试在触发此命令之前从阅读器获取数据 像

1)Fetch data from reader and store in list or datatable
2)For loop on list or datatable
3)in for loop fire this command

答案 2 :(得分:-3)

尝试移动以下

SqlCommand myCommand = thisConnection.CreateCommand();
                myCommand.CommandText = "FederationUpdateCTRAndImpressionCountsForAllYPIds";
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@bid", SqlDbType.UniqueIdentifier);
                myCommand.Parameters.Add("@uid", SqlDbType.UniqueIdentifier);
                myCommand.Parameters.Add("@imp", SqlDbType.VarChar);
                myCommand.Parameters.Add("@ctr", SqlDbType.VarChar);

在while循环中,看看你是否得到不同的结果。

相关问题