我的C#ExecuteSqlCommand
没有从存储过程返回输出。
我实际上需要最后插入的行Id
这是我的存储过程
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
我已经测试了这个存储过程,它在SQL Server Management Studio中运行良好
以下是Stored Procedure Execution Image,因为我使用SCOPE_IDENTITY()
并将其作为OUTPUT
通过@LastCreatedId
您可以在image数据中看到数据完全插入数据库但在C#中我可以正确地将数据发送到数据库但不能保留OUTPUT
@LastCreatedId
数据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;
}
}
}
这是我使用此存储过程的C#代码
{}
我从returnCode.Value
获得Specified cast is not valid.
值
如果我尝试将其转换为整数,它会给我异常@LastCreatedId
如何从存储过程中检索glFramebufferTexture2D( myfboID, GL_FRAMEBUFFER, GL_TEXTURE_2D, mytexID, 0);
的值作为OUTPUT值
答案 0 :(得分:1)
添加
SELECT @LastCreatedId = CAST(SCOPE_IDENTITY() as int)
存储过程中的..还要使用下面的强制转换操作检查它是否包含int值
if (returnCode.Value != DBNull.Value)
return Convert.ToInt32(returnCode.Value);
答案 1 :(得分:1)
由于您基本上是尝试通过SCOPE_IDENTITY()获取ID,为什么不尝试使用:。
执行查询,并返回查询返回的结果集中第一行的第一列。其他列或行将被忽略。
我从下面的帖子中得到了答案: ExecuteScalar()
干杯!
答案 2 :(得分:1)
我遇到了同样的问题,尝试在ExecuteSQLCommand调用中提到参数之后添加单词OUTPUT
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;
}
}
}