我的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
值從存儲過程
你可能想要'Database.ExecuteReader',而不是'Database.ExecuteSqlCommand' – Tibrogargan