0

我有一个 C# Windows 应用程序 (.net 4),它看起来像这样:它运行一个查询并通过运行我的视图来填充 gridview。

如何从查询结果中检查某个值?如果它存在然后播放声音?

private void Form1_Load(object sender, EventArgs e)
{
    //CREATE TIMER
    Timer timer = new Timer();
    //SET UP THE TIMER
    timer.Tick += new EventHandler(timer_Tick);
    timer.Interval = (1000 * 30 * 1);
    timer.Enabled = true;
    timer.Start();   
}

    void timer_Tick(object sender, EventArgs e)
    {
        MessageBox.Show("Run the Query every 30 SECONDS");

        //run the query now
        string ConnectionString = "Data Source=C;Initial Catalog=tickets;Integrated Security=True";
        SqlConnection Conn = new SqlConnection(ConnectionString);
        Conn.Open();

        SqlDataAdapter DA = new SqlDataAdapter("SELECT *  FROM [tickets].[dbo].[TicketView]", Conn);
        DataTable dt = new DataTable();
        DA.Fill(dt);

        if (Conn.State == ConnectionState.Open)
        {
            Conn.Close();
        }

            BindingSource BS = new BindingSource();
            BS.DataSource = dt;
            dataGridView1.DataSource = BS;
            bindingNavigator1.BindingSource = BS;
        }

我想过编写以下循环:

如果在 gridview 中没有找到任何内容,则 Ticket exists 应该返回一个值或零,如果找到,则返回值或 1。我想检查公司代码。( [COMPANYCODE]) << (我卡在这部分)

int ticketExists;
    while (ticketExists > 0)
    {
        Console.Write("Play a sound as ticket exists!");
        // Write the index to the screen.
        Console.WriteLine(ticketExists);
        // Increment the variable.
        ticketExists++;
    }

如果有人想知道,我想出了如何播放声音:

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("The sound will play after this message!!");

        using (var soundPlayer = new SoundPlayer(@"c:\Windows\Media\chimes.wav")) {
            soundPlayer.Play(); // can also use soundPlayer.PlaySync()
        }

    }
4

0 回答 0