【问题标题】: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如何将查询结果映射到一个类,然后是一个列表,然后用于填充列表视图?无法将查询结果转换为类
【发布时间】:2020-08-01 09:11:57
【问题描述】:

我正在尝试填充列表视图,因此我首先将查询的结果映射到类模型,但它一直给我这个异常“System.Data.DataException 消息=解析列 0 时出错(TimeOfAppointment=15:30:00 - 对象) 来源=小巧玲珑 InvalidCastException:对象必须实现 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, 
)

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

【问题讨论】:

    标签: c# sql-server winforms visual-studio-2019 dapper


    【解决方案1】:

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

    //public DateTime TimeOfAppointment { get; set; }
    
    public TimeSpan TimeOfAppointment { get; set; }
    

    【讨论】:

      【解决方案2】:

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

      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 数据类型的映射。

      日期 - DateTime, Nullable&lt;DateTime&gt; 时间 - TimeSpan, Nullable&lt;TimeSpan&gt;

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

      【讨论】:

      • 什么是“货币代码”?我尝试了将时间数据类型更改为时间跨度,并且成功了。
      • @Mohammed 抱歉,这是错误的,我更正了。建议按照我定义的方式使用参数
      猜你喜欢
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多