2016-12-28 30 views
1

我想弄清楚如何让我的应用程序发送一个通知在它从我的数据库检索到的指定日期/时间。从数据库发送时间/日期通知

example

它并不需要做太多复杂的,它只是需要发送一个简单的信息弹出/声音通知。

我的代码到目前为止。

SqlConnection connectionString = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Goeli\Desktop\Feedr OIS\DateTimePlanner2\DateTimePlanner2\Database1.mdf;Integrated Security=True"); 

    public Form1() 
    { 
     InitializeComponent(); 
     dateTimePicker1.Format = DateTimePickerFormat.Custom; 
     timePicker2.Format = DateTimePickerFormat.Custom; 
     dateTimePicker1.CustomFormat = "MM/dd/yyyy"; 
     timePicker2.CustomFormat = "HH:mm"; 
     DisplayData(); 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

     try 
     { 
      if (textBox1.Text != "") 
      { 

       SqlCommand cmd = new SqlCommand("INSERT INTO dbo.Planner(Name, Date, Time) VALUES(@Name, @Date, @Time)", connectionString); 
       connectionString.Open(); 
       cmd.Parameters.AddWithValue("@Name", textBox1.Text); 
       cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Text); 
       cmd.Parameters.AddWithValue("@Time", timePicker2.Text); 
       cmd.ExecuteNonQuery(); 
       connectionString.Close(); 
       MessageBox.Show("Successfully Planned"); 
       DisplayData(); 
       //ClearData(); 
      } 
      else 
      { 
       MessageBox.Show("Please provide a name"); 
      } 
     } 
     catch (Exception) 
     { 

      MessageBox.Show("Cannot have duplicate name"); 
     } 
    } 

    private void DisplayData() 
    { 
     connectionString.Open(); 
     DataTable dt = new DataTable(); 
     SqlDataAdapter adapt = new SqlDataAdapter("select * from dbo.Planner", connectionString); 
     adapt.Fill(dt); 
     dataGridView1.DataSource = dt; 
     connectionString.Close(); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     SqlCommand cmd = new SqlCommand("DELETE FROM dbo.Planner WHERE [email protected]", connectionString); 
     try 
     { 
      if (textBox1.Text != "") 
      {     
       connectionString.Open(); 
       cmd.Parameters.AddWithValue("@Name", textBox1.Text); 
       cmd.ExecuteNonQuery(); 
       connectionString.Close(); 
       MessageBox.Show(" Successfully Deleted"); 
       DisplayData(); 
      } 

      else 
      { 
       MessageBox.Show("Please provide the name to delete"); 
      } 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     }  
    } 
} 

}

回答

1

由于最快和最简单的解决方案,我会建议使用

  1. 是引发事件,例如定时器,每分钟(或第二,取决于粒度你需要实现)并读取一个时间表 - 用消息收集日期\时间。
  2. 一个单独的线程(或计时器),每隔X秒读取(轮询)数据库并维护计划(添加\删除事件)。

需要涉及一些简单的同步。

作为替代方案,您可以使用第三方库,如FluentScheduler。对于更多重量级解决方案,如果您需要在许多应用程序实例中保持一致,请考虑Quartz.NET

+0

如果托管环境(具有有限功能)有一个名为ATrigger的免费Web服务。在这里您可以安排网络通话。完全披露,可能会有几秒钟的漂移 –