在制作此预约日历时,我想使用访问数据库来保存和检索我的约会。但是,我有多个属性类型(字符串,Ints,DateTime)和多种类型的框(ComboBox,ListBox,DateTimePicker)显示在窗体中。 我设法写我的数据库代码用下面的代码(的一部分):卡在从数据库中加载数据
foreach(var appointment in listOfAppointments)
{
OleDbCommand DbCommand = new OleDbCommand(
"INSERT INTO NewAppointmentDatabase " +
"([Start], [Length], [DisplayableDescription], [OccursOnDate], [Location], [IsRecurring], [Frequency], [Occurence]) " +
"VALUES(@Start, @Length, @DisplayableDescription, @OccursOnDate, @Location, @IsRecurring, @Frequency, @Occurence)",
SaveAppntAccess);
DbCommand.Connection = SaveAppntAccess;
DbCommand.Parameters.AddWithValue("@Start", appointment.Start); //is a short time in DateTime
DbCommand.Parameters.AddWithValue("@Length", appointment.Length); //is an int
DbCommand.Parameters.AddWithValue("@DisplayableDescription", appointment.DisplayableDescription); //is a long string
DbCommand.Parameters.AddWithValue("@OccursOnDate", appointment.OccursOnDate(date)); //is a boolean with DateTime as argument
DbCommand.Parameters.AddWithValue("@Location", appointment.Location); //is a string
DbCommand.Parameters.AddWithValue("@IsRecurring", appointment.IsRecurring); //is a boolean with yes/no tickbox
DbCommand.Parameters.AddWithValue("@Frequency", appointment.Frequency); //is a string
DbCommand.Parameters.AddWithValue("@Occurence", appointment.Occurence); //is an int
我必须指出,在appointment.OccursOnDate(date)
字日期在Visual Studio这是一种奇怪的,因为变红布尔参数被继承。
然后出现棘手的部分:我想加载我的数据!但是我想从数据库中提取我的值,并首先将它们分配给每个属性,然后将它们显示在ComboBoxes和TextBoxes和DateTimePickers中。
的代码是这样(的一部分):
if(LoadAppntAccess.State == ConnectionState.Open)
{
OleDbCommand DbCommand = new OleDbCommand(
"SELECT * FROM NewAppointmentDatabase", LoadAppntAccess);
OleDbDataReader reader = null;
DbCommand.Connection = LoadAppntAccess;
reader = DbCommand.ExecuteReader();
foreach (var appointment in listofAppointments)
{
while (reader.Read())
{
//code to complete
}
}
}
我将如何分配每个字段中的值到每个属性?我在想这样的事情:
appointment.Start.Add(reader["Start"].ToString());
appointment.Length.Add((reader["Length"].ToString());
appointment.DisplayableDescription(reader["DisplayableDescritpion"].ToString());
但我得到所有这些错误 - 什么是正确的语法?
编辑:我忘了提,“开始”,虽然它指定为DateTime
,我作为短时值,因为我想随着时间的推移和30个分钟的间隔组合框。所以这不完全是一个日期。对于OccursOnDate
它被写为:
public bool OccursOnDate(DateTime date)
{
return date.Date == date;
}
,并取回我用DateTimePicker
日期。
更多信息第二编辑
我的类看起来是这样的:
public class Appointment : IAppointment
{
public DateTime Start { get; set; }
public int Length { get; set; }
public string DisplayableDescription { get; set; }
public bool OccursOnDate(DateTime date)
{
return date.Date == date;
}
//custom members
public int ID { get; }
public string Location { get; set; }
public bool IsRecurring { get; set; }
public string Frequency { get; set; }
public int Occurence { get; set; }
public Appointment()
{
}
但不幸的是它继承IAppointment具有这种代码的参数。
int ID { get; }
DateTime Start { get; }
int Length { get; }
string DisplayableDescription { get; }
bool OccursOnDate(DateTime date);
//custom members
string Location { get; set; }
bool IsRecurring { get; set; }
string Frequency { get; set; }
int Occurence { get; set; }
自定义成员是我的补充,因为我不得不根据规格加些额外的东西。
但是,我设法根据你的答案找到一个语法如下。
appointment.Start.((DateTime)reader["Start"]);
appointment.Length.((int)reader["Length"]);
appointment.DisplayableDescription.((string)reader["DisplayableDescritpion"]);
appointment.OccursOnDate((DateTime)reader["OccursOnDate"]);
appointment.Location.((string)reader["Location"]);
appointment.IsRecurring.((bool)reader["IsRecurring"]);
appointment.Frequency.((string)reader["Frequency"]);
appointment.Occurence.((int)reader["Occurence"]);
任何线索?
你看到什么错误?你可以发布他们吗? –
'appointment.Start =((DateTime)reader [“Start”]);'注意'Start'和'('' – Nino