为什么插入后输出参数从标识列返回空白值?
我尝试了各种测试,返回输出参数始终为空
这是 C# 代码:
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "TestSerPsp @ValColp, @RetKeyp";
cmd.Parameters.Add("@RetKeyp", SqlDbType.Int);
cmd.Parameters["@RetKeyp"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@ValColp", SqlDbType.NChar);
cmd.Parameters["@ValColp"].Value = "QQ";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
string x = cmd.Parameters["@RetKeyp"].Value.ToString();
这是 SQL 服务器存储过程:
ALTER PROCEDURE [dbo].[TestSerPsp]
@ValColp nvarchar(5),
@RetKeyp int OUTPUT
AS
BEGIN
SET NOCOUNT ON
*** Test 1- This uses the scope identity function
Insert into TestSP (ValCol) Values (@ValColp); SELECT @RetKeyp = SCOPE_IDENTITY()
*** Test 2- This uses the @@ identity function
Insert into TestSP (ValCol) Values (@ValColp)
SET @RetKeyp = @@Identity
这是表 DDL:
CREATE TABLE [dbo].[TestSP](
[KeyCol] [int] IDENTITY(1,1) NOT NULL,
[ValCol] [nchar](10) NULL,
CONSTRAINT [PK_TestSP] PRIMARY KEY CLUSTERED
(
[KeyCol] ASC
) WITH (PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END