从存储过程中获取返回值始终为0 [英] Getting a returned value from stored procedure is always giving 0
本文介绍了从存储过程中获取返回值始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在罗斯文(Northwind)中插入employees表并返回scope_identity,但它始终为我提供0,这是我的代码:
在存储过程中:
Hi ,
I am trying to insert into employees table in northwind and return the scope_identity but it is always giving me 0 and this is my code :
in stored procedure:
ALTER PROCEDURE [dbo].[InsertmployeeAndGetScope]
-- Add the parameters for the stored procedure here
@LastName nvarchar(20)=null,
@FirstName nvarchar(10),
@EmplyeeId int= null output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
insert into dbo.Employees(LastName,FirstName) values(@LastName,@FirstName);
set @EmplyeeId=SCOPE_IDENTITY();
return @EmplyeeId;
END
和C#代码:
and the c# code:
public int insertemployeeandgetid()
{
string connStr = ConfigurationManager.ConnectionStrings["north"].ToString();
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand dCmd = new SqlCommand("InsertmployeeAndGetScope", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@LastName", "zarif");
dCmd.Parameters.AddWithValue("@FirstName", "mhamad");
dCmd.Parameters.AddWithValue("@EmplyeeId", null);
return Convert.ToInt32(dCmd.ExecuteScalar());
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
那么问题是什么呢?
So what is the problem ?
推荐答案
尝试更改这样的参数声明.
try to change parameter declaration like this.
SqlParameter p = cmd.Parameters.Add("@MyParam", SqlDbType.Int);
p.Direction = ParameterDirection.Output;
谢谢
--RA
Thanks
--RA
请在您的C#代码中为EmployeeID设置参数方向.
谢谢
Please set the parameter direction for EmployeeID in your C# code.
Thanks
这篇关于从存储过程中获取返回值始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文