在制作此约会日历时,我想使用访问数据库来保存和检索我的约会。但是,我有多个属性类型(字符串,Ints,DateTime)和多个类型的框(ComboBox,ListBox,DateTimePicker)显示在Windows窗体中。 我已经设法使用以下代码(部分代码)编写我的数据库代码:
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
,但我用作ShortTime值,因为我想要一个时间间隔为30分钟的ComboBox。所以这不是一个日期。对于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"]);
任何线索?
答案 0 :(得分:2)
根据您提供的信息,我猜是这样的:
appointment.Start = (DateTime)reader["Start"];
appointment.Length = (int)reader["Length"];
appointment.DisplayableDescription = (string)reader["DisplayableDescritpion"];
这只是一个简单的例子,我们需要更多信息才能给出更好的答案。如果任何列都可以具有空值,则还需要处理该值等。
答案 1 :(得分:1)
使用列索引访问读者的列,使用列索引(列索引0是SELECT
子句的第一列,1是第二列......等等)。如您所见,请使用读者正确的方法来获取适当类型的数据。
appointment.Start = reader.GetDateTime(0);
appointment.Length = reader.GetInt32(1);
appointment.DisplayableDescription= reader.GetString(2);
您还可以通过指定列名来获取数据。
appointment.Start = reader.GetDateTime(reader.GetOrdinal("Start"));
appointment.Length = reader.GetInt32(reader.GetOrdinal("Length"));
appointment.DisplayableDescription = reader.GetString(reader.GetOrdinal("DisplayableDescritpion"));
答案 2 :(得分:0)