爲什麼輸入參數在插入後從標識列返回空白值?sqlserver輸出參數爲空
我已經嘗試了各種試驗和返回輸出參數始終是空
下面是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 Server過程:
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