【问题标题】:Send notification on Time/Date from database从数据库发送时间/日期通知
【发布时间】:2017-05-12 21:13:28
【问题描述】:

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

它不需要做任何复杂的事情,比如弹出/声音通知,它只需要发送一条简单的消息。

到目前为止我的代码。

    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 Name=@Name", 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);
        }       
    }
}

}

【问题讨论】:

    标签: c# sql-server database winforms


    【解决方案1】:

    作为最快和最简单的解决方案,我建议使用

    1. 引发事件的计时器,例如,每分钟(或每秒钟,取决于您需要达到的粒度)并读取时间表 - 包含消息的日期\时间集合。
    2. 一个单独的线程(或计时器)每 X 秒读取(轮询)您的数据库并维护计划(添加\删除事件)。

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

    作为替代方案,您可以使用 3rd 方库,例如 FluentScheduler。如果您需要在应用程序的多个实例之间保持一致性,那么对于更重量级的解决方案,请考虑 Quartz.NET

    【讨论】:

    • 如果托管环境(功能有限)有一个名为 ATrigger 的免费 Web 服务。在这里您可以安排网络通话。完全公开,最多可能会有几秒钟的漂移
    猜你喜欢
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 2022-06-11
    • 2010-12-14
    • 2017-10-24
    相关资源
    最近更新 更多