简体   繁体   中英

Failure to convert SQL Server varchar to binary in C#

I need to accept a user name and return the password and user ID to determine whether or not they have access to a particular Form in a project.

I have searched for a solution and viewed similar questions on this site, but to no avail. Please, help if you can.

The parameter values returned when run in SQL Server debug are:

Joseph 
0x6D616368696E653100000000000000000000000000000000000000000000000000000000000000000000
34DFCAA9-1A5F-4AC4-AC43-4A025DD84063

My stored procedure is:

CREATE PROCEDURE [dbo].[RMVerifyUser] 
    @UserName AS VARCHAR(40),
    @Password AS BINARY(42) OUTPUT,
    @ClerkID AS UNIQUEIDENTIFIER OUTPUT
AS
    SELECT   
        @ClerkID = IDClerk,
        @Password = (CONVERT(binary(8), ClkPassCode, 0))    // ClkPassCode is varchar(50) in the table.
    FROM 
        tblClerkInfo
    WHERE
        ClkName = @UserName
        AND ISNULL(Canceled, 0) = 0

    RETURN

And my C# code is:

private string retrievePassword(object userName) 
{
    string strPassword = "";
    //uidClerkID = new Guid();

    connect.Open();

    try
    {
        SqlCommand command = new SqlCommand("RMVerifyUser");
        command.Connection = connect;
        command.CommandType = CommandType.StoredProcedure

        SqlParameter retUserName = new SqlParameter("@UserName", userName); // userName is passed in from another method. 
        retUserName.ParameterName = "@UserName";
        retUserName.Value = userName;
        command.Parameters.Add(retUserName); 

        SqlParameter retPassword = new SqlParameter("@Password", strPassword);   // I suspect the error is occurring somewhere in the declaration/definition of this parameter.
        retPassword.ParameterName = "@Password";
        retPassword.Direction = ParameterDirection.Output;
        retPassword.DbType.Equals(DbType.String);   
        retPassword.Size = 50;
        retPassword.Value = strPassword; 
        command.Parameters.Add(retPassword);  // ClkPassCode is nvarchar(50) in the data table.

        SqlParameter retUserID = new SqlParameter("@ClerkId", uidClerkID);  // uidClerkID is a property in a separate class, but I also tried - uidClerkID = new Guid() - in this method. Same error thrown.  
        retUserID.ParameterName = "@ClerkId";
        retUserID.Direction = ParameterDirection.Output;
        retUserID.Value = uidClerkID;
        command.Parameters.Add(retUserID);

        command.ExecuteNonQuery();

        uidClerkID = Guid.Parse(retUserID.Value.ToString());

        if (strPassword == DBNull.Value.ToString())
        {
            ReferenceEquals(strPassword, null);             
        }
        else
        {
            strPassword = retPassword.Value.ToString();
            return strPassword;
        }
    }
    catch (Exception ex)
    {
        ....
    }
}

Your stored procedure has binary(42) as the parameter type but in your code you "try" to set it to DbType.String . Update your code to use binary. You will also need to use byte[] for the local variable. Also note that you should SET the value of DbType and in this case I used SqlDbType because it is more specific than just DbType . You are doing an equality comparison which has no influence.

byte[] password = new byte[42];

try
{
    // begin changed code

    SqlParameter retPassword = new SqlParameter("@Password", password);
    retPassword.Direction = ParameterDirection.Output;
    retPassword.SqlDbType = SqlDbType.Binary;
    retPassword.Size = 42;
    command.Parameters.Add(retPassword);

    // end changed code

    command.ExecuteNonQuery();


    // read the value back from the parameter
    password = retPassword.Value != DBNull.Value ? (byte[]) retPassword.Value : null; 

I think your password field should be nvarchar instead of binary. Is there an implicit way to convert varchar into binary? I would guess not and I am thinking that you would have to create a special function to get this behavior.

declare @binarydata binary
declare @password varchar(50)
set @binarydata = @password

Notice the error we get when we try to do this.

Msg 257, Level 16, State 3, Line 4 Implicit conversion from data type varchar to binary is not allowed. Use the CONVERT function to run this query.

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