从存储过程中获取返回值时,总是给出0[英] Getting a returned value from stored procedure is always giving 0

问题描述

你好,

我正在尝试在北风中插入员工表并返回 scope_identity,但它总是给我 0,这是我的代码:

存储过程中:

ALTER PROCEDURE [dbo].[InsertmployeeAndGetScope]--这里添加存储过程的参数@LastName nvarchar(20)=null,@FirstName nvarchar(10),@EmplyeeId int= null 输出AS开始-- SET NOCOUNT ON 添加以防止额外的结果集-- 干扰 SELECT 语句.SET NOCOUNT ON;--在此处插入过程语句插入 插入 dbo.Employees(LastName,FirstName) (@LastName,@FirstName);set @EmplyeeId=SCOPE_IDENTITY();return @EmplyeeId;END


和c#代码:

public int insertemployeeandgetid(){string connStr = ConfigurationManager.ConnectionStrings["north"].ToString();SqlConnection conn = new SqlConnection(connStr);conn.Open();SqlCommand dCmd = new SqlCommand("InsertmployeeAndGetScope", 连接);dCmd.CommandType = CommandType.StoredProcedure;试试{dCmd.Parameters.AddWithValue("@LastName", "zarif");dCmd.Parameters.AddWithValue("@FirstName", "mhamad");dCmd.Parameters.AddWithValue("@EmplyeeId", null);return Convert.ToInt32(dCmd.ExecuteScalar());}赶上{抛出;}终于{dCmd.Dispose();conn.Close();conn.Dispose();}}

那么问题出在哪里?
解决方案
尝试像这样更改参数声明.

SqlParameter p = cmd.Parameters.Add("@MyParam", SqlDbType.Int);

p.Direction = ParameterDirection.Output;


谢谢
--RA

请在你的C#代码中设置EmployeeID的参数方向.

谢谢

本文地址:https://www.itbaoku.cn/post/1394760.html