C# 错误的执行存储过程

本文关键字:存储过程 执行 错误 | 更新日期: 2023-09-27 18:34:35

我正在尝试在 C# 上执行 SP 并得到该错误

    System.Data.SqlClient.SqlException: Procedure or function 'usp_Registrations_Insert' expects parameter '@RegistrationID', which was not supplied.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at C2DMwebService.C2DM.RegisterDevice(String DeviceID, String RegistrationId) in c:'users'eyal'documents'visual studio 2010'Projects'C2DMwebService'C2DMwebService'C2DM.asmx.cs:line 52

我执行的代码是

SqlCommand cmd = new SqlCommand();
                string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(connStr);
                //cmd.Parameters.Add("@RegistrationID", SqlDbType.NVarChar).Value = RegistrationId;
                //cmd.Parameters.Add("@DeviceID", SqlDbType.NVarChar).Value = DeviceID;
                //cmd.Parameters.Add(new SqlParameter("@RegistrationID", RegistrationId));
                //cmd.Parameters.Add(new SqlParameter("@DeviceID", DeviceID));
                SqlParameter myParm1 = cmd.Parameters.Add("@RegistrationID", SqlDbType.NVarChar, 200);
                myParm1.Value = RegistrationId;
                SqlParameter myParm2 = cmd.Parameters.Add("@DeviceID", SqlDbType.NVarChar, 200);
                myParm2.Value = DeviceID;
                cmd = new SqlCommand("usp_Registrations_Insert", con);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

我的 SP

ALTER PROCEDURE dbo.usp_Registrations_Insert
    (
    @RegistrationID NVarChar(200),
    @DeviceID NVarChar(200)
    )
AS
    /* SET NOCOUNT ON */
    INSERT INTO Registrations
                         (RegistrationID, DeviceID)
VALUES        (@RegistrationID,@DeviceID)
    RETURN @@rowcount;

C# 错误的执行存储过程

在执行新命令之前,您正在创建一个新命令。 这将释放您刚刚添加到您创建的第一个命令中的所有参数。

将行cmd = new SqlCommand("usp_Registrations_Insert", con);移动到顶部,紧随SqlConnection con = new SqlConnection(connStr);行之后,您应该没问题。 您可以删除第一行cmd = new SqlCommand();