简体   繁体   中英

Error with DateTime mapping C#

I'm working on a program that is pulling data from a SQLserver instance and manipulating it in a C# console application. I'm having trouble mapping dateTime value correcltly. I'm using Enterprise Library 5. This is the error I get

The Column DOB was not found on the IDataRecord being evaluated. This might indicate that the accessor was created with the wrong mappings

This is the param mapper interface

public class MyParameterMapper : IParameterMapper
{
    public void AssignParameters(DbCommand command, object[] parameterValues)
    {
        var parameter = command.CreateParameter();
        parameter.ParameterName = "@dateOfBirth";
        parameter.Value = parameterValues[0];
        parameter.DbType = DbType.DateTime;
        command.Parameters.Add(parameter);
    }
}

In the class that is modeling the type of object which will have the mappings has this declaration public DateTime? DateOfBirth {get; set;} public DateTime? DateOfBirth {get; set;} public DateTime? DateOfBirth {get; set;} . The class in which I actually implement the interface has pases the DoB like this: var matches = _objectAdapter.GetList(person.DateOfBirth.Value).ToList();

The actual method that passes the query is as follows:

public IEnumerable<Person> GetList(DateTime dateOfBirth)
{
    const string sql = (@"SELECT FirstName, LastName
            FROM   Table
            WHERE  DOB = @dateOfBirth"); 
    var accessor = new SqlStringAccessor<Person>(_db, sql, new MyParameterMapper(),_myRowMapper);
    var people = accessor.Execute(dateOfBirth);
    return people;
}

The actual mapping is as follows:

private IRowMapper<Person> _myRowMapper = MapBuilder<Person>.MapAllProperties()
        .Map(x => x.FirstName).ToColumn("FirstName")
        .Map(x => x.LastName).ToColumn("LastName")
        .Map(x => x.DateOfBirth).ToColumn("DOB")
        .Build();

The column name is correct, so I'm not sure if this error has to do with interface or if there is some explicit casting I have to pass on DateOfBirth?

Try add DOB to your select clause...

const string sql = (@"SELECT FirstName, LastName, DOB
                FROM   Table
                WHERE  DOB = @dateOfBirth"); 

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