2016-08-26 54 views
0

我的C#ExecuteSqlCommand未從存儲過程返回輸出。C#ExecuteSqlCommand未從存儲過程返回輸出

我真的需要最後插入的行的Id

這裏是我的存儲Proceedure

ALTER PROCEDURE [dbo].[PatientInsert] 
@FirstName nvarchar(Max), 
@MiddleName nvarchar(Max), 
@LastName nvarchar(Max), 
@Gender nvarchar(Max), 
@CNICNumber nvarchar(Max), 
@DateOfBirth datetime, 
@Country nvarchar(max), 
@StateOrProvince nvarchar(max), 
@City nvarchar(max), 
@Address nvarchar(max), 
@CellNumber nvarchar(max), 
@TelephoneNumber nvarchar(max), 
@Email nvarchar(max), 
@NOKFirstName nvarchar(max), 
@NOKLastName nvarchar(max), 
@NOKRelation nvarchar(max), 
@NOKCNICNumber nvarchar(max), 
@NOKCellNumber nvarchar(max), 
@Title int, 
@PicturePath nvarchar(max), 
@LastCreatedId INT OUTPUT 
AS 
BEGIN 
    SET NOCOUNT ON 

INSERT INTO [dbo].[Patients] 
      ([FirstName] 
      ,[MiddleName] 
      ,[LastName] 
      ,[Gender] 
      ,[CNICNumber] 
      ,[DateOfBirth] 
      ,[Country] 
      ,[StateOrProvince] 
      ,[City] 
      ,[Address] 
      ,[CellNumber] 
      ,[TelephoneNumber] 
      ,[Email] 
      ,[NOKFirstName] 
      ,[NOKLastName] 
      ,[NOKRelation] 
      ,[NOKCNICNumber] 
      ,[NOKCellNumber] 
      ,[Title] 
      ,[PicturePath]) 

    VALUES 
      (
      @FirstName , 
      @MiddleName , 
      @LastName , 
      @Gender , 
      @CNICNumber , 
      @DateOfBirth, 
      @Country , 
      @StateOrProvince , 
      @City , 
      @Address , 
      @CellNumber , 
      @TelephoneNumber , 
      @Email , 
      @NOKFirstName , 
      @NOKLastName , 
      @NOKRelation , 
      @NOKCNICNumber , 
      @NOKCellNumber , 
      @Title, 
      @PicturePath 
     ) 

     SELECT @LastCreatedId = SCOPE_IDENTITY() 

END 

我已經測試了此存儲proceedure,它是在SQL Server Management Studio中

正常使用

這裏是Stored Procedure Execution Image因爲我正在使用SCOPE_IDENTITY()並將其返回爲OUTPUT@LastCreatedId

可以在image數據看到的是完全插入數據庫,但在C#中,我可以正確地發送數據到數據庫,但不能保持性@LastCreatedId

OUTPUT數據這裏是我的C#代碼使用此存儲過程

public int PatientInsert(Patient PatientInstance) 
     { 
      using (var context = new HMContext()) 
      { 
       try 
       { 

        //Int32 LastCreatedId = new Int32(); 

        var returnCode = new SqlParameter("@LastCreatedId", SqlDbType.Int); 
        returnCode.Direction = ParameterDirection.Output; 

        insertRow = 
        context.Database.ExecuteSqlCommand("PatientInsert @FirstName ,@MiddleName," 
        +"@LastName,@Gender,@CNICNumber,@DateOfBirth,@Country,@StateOrProvince,@City," 
        + "@Address,@CellNumber,@TelephoneNumber,@Email,@NOKFirstName,@NOKLastName,@NOKRelation," 
        + "@NOKCNICNumber,@NOKCellNumber,@Title,@PicturePath,@LastCreatedId", 
        new SqlParameter("@FirstName", PatientInstance.FirstName), 
        new SqlParameter("@MiddleName", PatientInstance.MiddleName), 
        new SqlParameter("@LastName", PatientInstance.LastName), 
        new SqlParameter("@Gender", PatientInstance.Gender), 
        new SqlParameter("@CNICNumber", PatientInstance.CNICNumber), 
        new SqlParameter("@DateOfBirth", PatientInstance.DateOfBirth.Value.ToShortDateString()), 
        new SqlParameter("@Country", PatientInstance.Country), 
        new SqlParameter("@StateOrProvince", PatientInstance.StateOrProvince), 
        new SqlParameter("@City", PatientInstance.City), 
        new SqlParameter("@Address", PatientInstance.Address), 
        new SqlParameter("@CellNumber", PatientInstance.CellNumber), 
        new SqlParameter("@TelephoneNumber", PatientInstance.TelephoneNumber), 
        new SqlParameter("@Email", PatientInstance.Email), 
        new SqlParameter("@NOKFirstName", PatientInstance.NOKFirstName), 
        new SqlParameter("@NOKLastName", PatientInstance.NOKLastName), 
        new SqlParameter("@NOKRelation", PatientInstance.NOKRelation), 
        new SqlParameter("@NOKCNICNumber", PatientInstance.NOKCNICNumber), 
        new SqlParameter("@NOKCellNumber", PatientInstance.NOKCellNumber), 
        new SqlParameter("@Title", PatientInstance.Title.ToString()), 
        new SqlParameter("@PicturePath", (PatientInstance.PicturePath==null?"":PatientInstance.PicturePath)), 
        returnCode 
        ); 
        return (int)returnCode.Value; 
       } 
       catch (Exception exp) 
       { 
        var me = exp.Message; 
        return -1; 
       } 
      } 
     } 

我從returnCode.Value 得到{}值,如果我嘗試將它轉換爲整數這讓我異常Specified cast is not valid.

什麼SH烏爾德我到可能檢索@LastCreatedId值從存儲過程

+1

你可能想要'Database.ExecuteReader',而不是'Database.ExecuteSqlCommand' – Tibrogargan

回答

1

產值就在存儲過程中添加

SELECT @LastCreatedId = CAST(SCOPE_IDENTITY() as int) 

..還做檢查,包括使用下面的轉換操作

int值
if (returnCode.Value != DBNull.Value) 
return Convert.ToInt32(returnCode.Value); 
1

既然你基本上試圖通過SCOPE_IDENTITY()獲取ID,爲什麼不嘗試使用:ExecuteScalar()

執行查詢,並返回查詢返回的結果集中第一行的第一列。其他列或行將被忽略。

我把答案從以下後: Return value from SQL Server Insert command using c#

乾杯!

+1

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – Squazz

1

我有同樣的問題,嘗試添加文字輸出的參數在ExecuteSQLCommand通話中提到這樣

context.Database.ExecuteSqlCommand("...,@LastCreatedId OUTPUT",... 

後,您完整的代碼會是這樣:

public int PatientInsert(Patient PatientInstance) 
    { 
     using (var context = new HMContext()) 
     { 
      try 
      { 

       //Int32 LastCreatedId = new Int32(); 

       var returnCode = new SqlParameter("@LastCreatedId", SqlDbType.Int); 
       returnCode.Direction = ParameterDirection.Output; 

       insertRow = 
       context.Database.ExecuteSqlCommand("PatientInsert @FirstName ,@MiddleName," 
       +"@LastName,@Gender,@CNICNumber,@DateOfBirth,@Country,@StateOrProvince,@City," 
       + "@Address,@CellNumber,@TelephoneNumber,@Email,@NOKFirstName,@NOKLastName,@NOKRelation," 
       + "@NOKCNICNumber,@NOKCellNumber,@Title,@PicturePath,@LastCreatedId OUTPUT", 
       new SqlParameter("@FirstName", PatientInstance.FirstName), 
       new SqlParameter("@MiddleName", PatientInstance.MiddleName), 
       new SqlParameter("@LastName", PatientInstance.LastName), 
       new SqlParameter("@Gender", PatientInstance.Gender), 
       new SqlParameter("@CNICNumber", PatientInstance.CNICNumber), 
       new SqlParameter("@DateOfBirth", PatientInstance.DateOfBirth.Value.ToShortDateString()), 
       new SqlParameter("@Country", PatientInstance.Country), 
       new SqlParameter("@StateOrProvince", PatientInstance.StateOrProvince), 
       new SqlParameter("@City", PatientInstance.City), 
       new SqlParameter("@Address", PatientInstance.Address), 
       new SqlParameter("@CellNumber", PatientInstance.CellNumber), 
       new SqlParameter("@TelephoneNumber", PatientInstance.TelephoneNumber), 
       new SqlParameter("@Email", PatientInstance.Email), 
       new SqlParameter("@NOKFirstName", PatientInstance.NOKFirstName), 
       new SqlParameter("@NOKLastName", PatientInstance.NOKLastName), 
       new SqlParameter("@NOKRelation", PatientInstance.NOKRelation), 
       new SqlParameter("@NOKCNICNumber", PatientInstance.NOKCNICNumber), 
       new SqlParameter("@NOKCellNumber", PatientInstance.NOKCellNumber), 
       new SqlParameter("@Title", PatientInstance.Title.ToString()), 
       new SqlParameter("@PicturePath", (PatientInstance.PicturePath==null?"":PatientInstance.PicturePath)), 
       returnCode 
       ); 
       return (int)returnCode.Value; 
      } 
      catch (Exception exp) 
      { 
       var me = exp.Message; 
       return -1; 
      } 
     } 
    }