【发布时间】: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