Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从数据库发送时间/日期通知_C#_Sql Server_Database_Winforms - Fatal编程技术网

C# 从数据库发送时间/日期通知

C# 从数据库发送时间/日期通知,c#,sql-server,database,winforms,C#,Sql Server,Database,Winforms,我试图弄清楚如何让我的应用程序在从数据库检索的指定日期/时间发送通知 它不需要做任何复杂的事情,比如弹出/声音通知,它只需要发送一条简单的消息 我的代码到目前为止 SqlConnection connectionString = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Goeli\Desktop\Feedr OIS\DateTimePlanner2\DateTime

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

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

我的代码到目前为止

    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);
        }       
    }
}

}

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

  • 计时器,例如每分钟(或每秒,取决于您需要达到的粒度)引发事件,并读取带有消息的日期\时间的计划集合
  • 一个单独的线程(或计时器),它每X秒读取(轮询)数据库并维护计划(添加\删除事件)
  • 需要进行一些简单的同步

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

    如果托管环境(具有有限的功能性),则有一个称为ATrigger的免费Web服务。您可以在这里安排web呼叫。完全公开,可能会有几秒钟的漂移