简体   繁体   中英

SQL Server stored procedure after insert row return string value of column data truncated

I have been trying to return string value after insert however my data is always truncated. It returns only the first character of the string.

My stored procedure:

ALTER PROC [dbo].[usp_add_user]
    (@firstName nvarchar(50),
     @lastName nvarchar(50),
     @email nvarchar(100),
     @password nvarchar(100),
     @userID nvarchar(50) OUTPUT)
AS
BEGIN
    SET @userID = newid()

    INSERT INTO [User] ([UserID], [FirstName], [LastName], [Email], [Password])
    VALUES (@userID, @firstName, @lastName, @email, @password)

    SELECT @userID;
END

My C# how I am trying to catch the value

access.Parameter["@userID"].Value.ToString();

When I execute the stored procedure in SQL Server Management Studio, it shows everything correctly, my @userID is correct. However when it comes back to my Visual Studio I only get first character

Thank you

You need to specify the size to get the entire string

cmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.NVarChar, 50));
cmd.Parameters["@userID"].Direction = ParameterDirection.Output;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM