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

xwmevbvl  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(150)

我尝试填充列表视图,所以我从将查询结果Map到类模型开始,但是它一直给我这个异常
System. Data. DataException消息=分析列0时出错(约会时间= 15:30:00-对象)源= Dapper
无效强制转换异常:对象必须实现IConvertible。
我的班级是

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

我真的是新来的,所以我做错了什么?

nbnkbykc

nbnkbykc1#

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

//public DateTime TimeOfAppointment { get; set; }

public TimeSpan TimeOfAppointment { get; set; }
dzjeubhm

dzjeubhm2#

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

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 Server中更改该数据类型。下面是SQL Server和CLR数据类型Map。
日期-第一个月第二个月第一次,第一个月第三个月第一次时间-第一个月第四个月第一次,第一个月第五个月第一次

参考:https://stackoverflow.com/a/655070/6527049

相关问题