从c#调用存储过程将返回“ ?????”在varchar响应中

时间:2019-02-12 11:39:43

标签: c# stored-procedures ado.net sql-server-2016

当我使用ADO.NET从C#在SQL Server上调用存储过程时,我的输出参数收到以下响应:
enter image description here

从字面上??????VarChar

我的存储过程如下:

ALTER Procedure [sp_getEncryptedPassword]
 (@PublicKey nvarchar(128), @UserId uniqueidentifier,  @PasswordClearText varchar(255) OUTPUT, @resOUT int OUTPUT, @rowsOUT int OUTPUT)

AS
    SET NOCOUNT On

    DECLARE @LinkIsValid bit = 0;
    set @rowsOUT=0

    while (@LinkIsValid=0)
    begin

        SELECT  CONVERT(nvarchar, DecryptByPassphrase(@PublicKey, UserpasswordEncrypted, 1, CONVERT(varbinary, @UserId)))  AS PassWordClearText
        Into #TEMP1
        from UserLogin  
        WHERE Userid = @UserId

        set @rowsOUT=@@ROWCOUNT

        SET @LinkIsValid=1

    end

    if @rowsOUT=0
        SET @resOUT=1 

    select 'RStestPwd' as 'PasswordClearText', @resOUT as 'resOUT', @rowsOUT as 'rowsOUT'

我删除了一些行,但我希望你明白这一点。
作为调试的一部分,我已经对响应进行了硬编码-只是看是否能得到任何东西。我做:
enter image description here

我的C#代码:

SqlConnection con = new SqlConnection(connectionstring);

SqlCommand cmd = new SqlCommand(spName, con);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter pubKey = cmd.Parameters.Add("@PublicKey", SqlDbType.UniqueIdentifier);
pubKey.Direction = ParameterDirection.Input;
pubKey.Value = publicKey;

SqlParameter IdIn = cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier);
IdIn.Direction = ParameterDirection.Input;
IdIn.Value = userid;

SqlParameter pwdOUT = cmd.Parameters.Add("@PasswordClearText", SqlDbType.VarChar, 255);
pwdOUT.Direction = ParameterDirection.Output;

SqlParameter resOUT = cmd.Parameters.Add("@resOUT", SqlDbType.Int);
resOUT.Direction = ParameterDirection.Output;

SqlParameter rowsOUT = cmd.Parameters.Add("@rowsOUT", SqlDbType.Int);
rowsOUT.Direction = ParameterDirection.Output;

con.Open();
cmd.ExecuteNonQuery();
con.Close();

result.Password = pwdOUT.Value.ToString();

这是SQL Server和我的应用程序之间格式的一部分吗? 是我的SQL服务器,ADO.NET Command实例还是连接字符串中的设置?

编辑: 感谢您的意见。我已经检查了我的代码,并使用(N)VarChar等修改了地方。
在进行这些修改之前或之后,我都不会遇到任何例外-只是问号。因此SP可以正常工作-但可以在数据库和应用程序之间删除/修改数据...

0 个答案:

没有答案