2012-05-16 57 views
0

我正在tryng給exec C#的SP和得到這個錯誤EXEC存儲過程

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 

我Exec在

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; 

回答

8
代碼

在執行它之前,您正在創建一個新命令。這將處理剛剛添加到您創建的第一個命令中的所有參數。

將行cmd = new SqlCommand("usp_Registrations_Insert", con);移到頂部,緊跟在SqlConnection con = new SqlConnection(connStr);行之後,您應該沒問題。您可以刪除第一條cmd = new SqlCommand();行。

+4

「直到頂部」 - 但創建連接後。 –

+0

@KirkWoll感謝您的澄清。我更新了答案。 –

+1

他的方式,它保持了新的命令氣味。也許這比功能更重要。 =) – Yatrix