SQL Server Dapper Not able to parse column String

sczxawaw  于 2023-03-07  发布在  其他
关注(0)|答案(2)|浏览(198)

I have the following table

create table tblWorkers
(
    Id int identity,
    WorkerId nvarchar(8),

    Username NVARCHAR(50) not null,
    Password NVARCHAR(12) not null,
    Token NVARCHAR(max) null,
    CONSTRAINT PK_WorkerId PRIMARY KEY (WorkerId)
)

When I try the following Dapper code, by passing Username:W01, Password=check123 I get an error, Error parsing column 0 (UserId=W01 - String)'.FormatException: Input string was not in a correct format.

``

DynamicParameters parameters = new DynamicParameters();
        parameters.Add("@userName", loginModel.Username, DbType.String);
        parameters.Add("@password", loginModel.Password, DbType.String);
        using (IDbConnection con = _connectionManager.GetConnection())
        {
            con.Open();
            var result = con.Query<User>(StoredProcedures.uspAuthenticate, param: parameters, commandType: CommandType.StoredProcedure);
            return result.FirstOrDefault();

`` StoredProcedure is as below.

CREATE PROCEDURE [dbo].[uspAuthenticate] 
    @userName nvarchar(8),
    @password nvarchar(12)
AS
BEGIN

    SET NOCOUNT ON;

    select WorkerId
    from tblWorkers
    where Username = @username and Password = @password
END

Please advise.

vdzxcuhz

vdzxcuhz1#

Dapper expects SQL and .NET to be the same. The error mentions UserId, code examples show parameter as username (not id).

you say 'when passing: Username:W01' The error shows 'Error parsing column 0 (UserId=W01 - String)'

v1uwarro

v1uwarro2#

Also check the type. I do not think the case needs to match but the type does. In my case the class field was int but SQL was returning varchar2.

相关问题