简体   繁体   中英

Dapper Not able to parse column String

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.

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)'

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