Sql select语句不能正常工作WPF

时间:2015-09-21 07:32:44

标签: c# sql wpf

我正在使用WPF,我有一个sql语句来连接数据库。该语句是搜索它所做的First_Quater但是当我更改为Second_Quater时(它还没有在数据库中)它会给我First_quater figuers。我无法在列中添加First_Quater。

我已经尝试了很多并搜索了互联网,但我仍然感到茫然。 谢谢你的帮助。

SqlConnection con = new SqlConnection("Data Source=; Initial Catalog=; Integrated Security=True; Trusted_Connection=yes");
        con.Open();

        String comboquery = (@"SELECT * FROM [taxi_comm] WHERE First_Quarter = '" + checkedListBox1.SelectedItem + "'");
        SqlCommand cmd = new SqlCommand(comboquery, con);
        SqlDataReader dr = cmd.ExecuteReader();




            double sum = 0;
            for (int i = 0; i < gvDisplay.Rows.Count; ++i)
            {


                switch (checkedListBox1.SelectedItem.ToString().Trim())
                {
                    case "First Quarter":

                        foreach (string s in checkedListBox1.CheckedItems)
                        {

                            sum += Convert.ToInt32(gvDisplay.Rows[i].Cells[10].Value);
                            txtTotalGST.Text = sum.ToString(); 
                        }
                        MessageBox.Show("Its feb");
                        break;
                    case "Second Quarter":

                        foreach (string st in checkedListBox1.CheckedItems)
                        {
                            sum += Convert.ToInt32(gvDisplay.Rows[i].Cells[10].Value);
                            txtTotalGST.Text = sum.ToString();
                            MessageBox.Show("You have reached the second quarter");
                        }

                        break;

1 个答案:

答案 0 :(得分:0)

您需要从查询中填充您的gvdisplay

    SqlCommand cmd = new SqlCommand(comboquery, con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable gvdisplay = new DataTable();
    sda.Fill(gvdisplay);

        double sum = 0;
        for (int i = 0; i < gvdisplay.Rows.Count; ++i)
        {


            switch (checkedListBox1.SelectedItem.ToString().Trim())
            {
                case "First Quarter":

                    foreach (string s in checkedListBox1.CheckedItems)
                    {

                        sum += Convert.ToInt32(gvdisplay.Rows[i].Cells[10].Value);
                        txtTotalGST.Text = sum.ToString(); 
                    }
                    MessageBox.Show("Its feb");
                    break;
                case "Second Quarter":
//.......//