2016-05-09 33 views
0

在制作此预约日历时,我想使用访问数据库来保存和检索我的约会。但是,我有多个属性类型(字符串,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"]); 

我仍然得到这个消息: Identifier expected

任何线索?

+0

你看到什么错误?你可以发布他们吗? –

+0

'appointment.Start =((DateTime)reader [“Start”]);'注意'Start'和'('' – Nino

回答

2

从你给我猜是这样的信息:

appointment.Start = (DateTime)reader["Start"]; 
appointment.Length = (int)reader["Length"]; 
appointment.DisplayableDescription = (string)reader["DisplayableDescritpion"]; 

这只是一个简单的例子,我们需要更多的信息,以提供更好的答案。如果任何列可以有一个空值你需要处理,以及等。

+0

之间的等号** = **我的参数是只读的,所以我不能得到这个因为这是以前的程序员的工作,所以我想尽可能少地进行修改,请使用另一种语法,如上面的Nino的注释,并告诉我你的想法......这是令人困惑的。 –

+0

我想说的是,约会.Start是只读的,然后我可以看到为什么会出现问题。如果是这种情况,您可以向我们展示约会类的代码吗?只是无需了解类的任何内容,如果参数是只读的,则无法设置值,然后有可能是一个很好的理由,为什么这被阻止,或者可能有一个特定的方式,他们需要设置,我们需要更多的信息有关该类的知道肯定。 – MrApnea

+0

我做了第二次编辑与有关cla的更多信息事实和继承。此外,我改变了语法,即使它看起来好,我得到的标识符预期错误 –

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")); 
+0

不幸的是两个工作。但我发现另一个语法是这样的: appointment.Start。((DateTime)reader [“Start”]); appointment.Length 。((int)reader [“Length”]); 即使它看起来正确,在第一个括号之前,我得到一个错误“预计的标识符”...为什么我得到这个? –

+0

使用像这样'appointment.Start =((DateTime)reader [“开始”])'。顺便说一句,在我的第一个答案中,我犯了一个错误,但后来我编辑了代码。也许你尝试过这个错误的版本。但是,@FSDaniel写的这个解决方案也应该可以工作。 – Nino

+0

它也没有工作...我发现了一个语法,虽然我在第二次编辑中显示。请看一看。 –

0

我发表了评论,但由于它没有明确表示没有图片我现在发布我以前的尝试,没有工作和解释。

这既是由@Nino和提到的代码@FsDaniel

The error

如果你看到(在我最初的帖子)接口的参数,他们只有获取属性,这使得开始,长度和DisplaybleDescritption只读。因此错误。其余的都是好的,因为他们是我的自定义成员,我给他们获取和设置属性。我不希望在界面上做任何改动,这就是为什么我要问是否有其他解决方案。