如何将查询结果映射到 class,然后映射到一个列表,然后用于填充列表视图?无法将查询结果转换为 class

How to map query result to a class and then a list to then use to populate listview? can't cast query result to class

我正在尝试填充列表视图,所以我首先将查询结果映射到 class 模型,但它一直给我这个异常“System.Data.DataException 消息=解析列 0 时出错(TimeOfAppointment=15:30:00 - 对象) 来源=Dapper InvalidCastException:对象必须实现 IConvertible。“

我的class是

public class AppointmentModel
    {
        public string PatientName { get; set; }
        public int AppointmentId { get; set; }
        public DateTime Date { get; set; }
        public DateTime TimeOfAppointment { get; set; }
        public int Duration { get; set; }
    }

我的查询是

CREATE PROCEDURE [dbo].[spGet_Appointments_byDay]
    @DateSelected date
AS
begin
select [dbo].[Appointments].[TimeOfAppointment], [dbo].[Appointments].[Duration], [dbo].[Patients].[Name], [dbo].[Appointments].[Date], dbo.Appointments.AppointmentId
from [dbo].[Appointments]
inner join [dbo].[Patients] on [dbo].[Appointments].PatientId = [dbo].[Patients].PatientID
where [dbo].[Appointments].[Date] = @DateSelected;
end

我就是这么叫它的

public List<AppointmentModel> CreateListViewList(DateTime date)
        {
            List<AppointmentModel> ListForListView;
            var l = new DynamicParameters();
            l.Add("@DateSelected", date);
            using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
            {
                ListForListView = connection.Query<AppointmentModel>("[Test DB].dbo.spGet_Appointments_byDay", l, commandType: CommandType.StoredProcedure).ToList();
            }

            return ListForListView;
        }

这是填充列表框

private void button1_Click_1(object sender, EventArgs e)
        {
            foreach (IDataConnection db in GlobalConfig.Connections)
            {
                ListForListView.AddRange(db.CreateListViewList(AppointmentDateBox.Value.Date));
            }
            
            foreach (AppointmentModel RowFromQuery in ListForListView)
            {
                ListViewItem viewItem = new ListViewItem(RowFromQuery.TimeOfAppointment.ToString());
                viewItem.SubItems.Add(RowFromQuery.Duration.ToString());
                viewItem.SubItems.Add(RowFromQuery.PatientName);
                viewItem.SubItems.Add(RowFromQuery.AppointmentId.ToString());

                listView1.Items.Add(viewItem);

            }
        }

我的桌子

CREATE TABLE [dbo].[Appointments]
(
    [AppointmentId] INT NOT NULL PRIMARY KEY IDENTITY, 
    [PatientId] INT NOT NULL, 
    [Date] DATE NOT NULL, 
    [Duration] INT NOT NULL, 
    [TimeOfAppointment] TIME NOT NULL, 
)
CREATE TABLE [dbo].[Patients]
(
    [PatientID] INT NOT NULL PRIMARY KEY IDENTITY, 
    [Name] VARCHAR(50) NOT NULL, 
)

我真的很陌生所以我做错了什么?

尝试在您的模型中使用 TimeSpan 作为时间数据类型。

//public DateTime TimeOfAppointment { get; set; }

public TimeSpan TimeOfAppointment { get; set; }

你用下面的代码来解决这个问题。

using (var connection = new SqlConnection(GlobalConfig.CnnString(db)))
{
    DynamicParameters parameter = new DynamicParameters();
    parameter.Add("@DateSelected", date, DbType.Time, ParameterDirection.Input);

    CommandDefinition cmd = new CommandDefinition("[Test DB].dbo.spGet_Appointments_byDay", parameter, null, null, CommandType.StoredProcedure);
    lstLookuptypes = connection.QueryFirstOrDefault<List<AppointmentModel>>(cmd).ToList();
}

您需要将 TimeOfAppointment 的数据类型更改为 TimeSpan 或您需要更改 SQL 服务器中的数据类型。 下面是 SQL 服务器和 CLR 数据类型映射。

日期 - DateTimeNullable<DateTime> 时间 - TimeSpan, Nullable<TimeSpan>

参考: