问题循环通过SQL表C#

时间:2016-05-11 13:21:37

标签: c# sql .net loops

我的应用程序正在读取SQL查询表,并在列表视图中的某些字段中匹配和显示值。我的查询SQL表看起来像这样

+--------------+---------+-------+----------+--------+
| process name | subtask | total | employee |  date  |
+--------------+---------+-------+----------+--------+
| process 1    | sub1    |     1 |     1111 | 01-May |
| process 2    |         |     1 |     2222 | 05-May |
| process 3    |         |     1 |     3333 | 10-May |
| process 4    |         |     1 |     4444 | 07-May |
+--------------+---------+-------+----------+--------+

我遇到了一个问题。我注意到通过循环我的循环(使用messegebox),进程1多次显示在我的消息框中(在MessageBox.Show(dr[0].ToString());中),当它只应显示一次时,当循环进入进程4时,消息框显示进程4 (在(MessageBox.Show(dr[0].ToString());)中)然后循环停止,因此不会获取总计,员工和日期值。我怎样才能解决这个问题。

         DateTime now = DateTime.Now;
        var startDate = new DateTime(now.Year, now.Month, 1);
        var endDate = startDate.AddMonths(1).AddDays(-1);


        string[,] report = new string[,] { {"process 1", "sub1", "3", "0",  "", "" },
       {"process 2", "", "3", "0", "", "" },
       {"process 3", "", "3", "0", "", "" }
        *** there are multiple entries in this array *****
       }  

    string totalsquery = "select Process_Name, Sub_Process1_Name, count(id) as total, Completed_By_Employee_Number, max(Refresh_Date)  from testDB.dbo.Quality_Data_Master  where Refresh_Date between '" + startDate + "' and '" + endDate + "' group by Process_Name, Sub_Process1_Name,  Completed_By_Employee_Number, Refresh_Date";


  SqlConnection con = new SqlConnection();
        SqlDataAdapter ada = new SqlDataAdapter(totalsquery, con);
        DataTable dt = new DataTable();
        ada.Fill(dt);

        listView1.View = View.Details;
        listView1.Columns.Add("Process Name", 250);
        listView1.Columns.Add(" Sub Task", 200);
        listView1.Columns.Add("Target", 45, HorizontalAlignment.Center);
        listView1.Columns.Add("Total", 40, HorizontalAlignment.Center);
        listView1.Columns.Add("Employee", 100, HorizontalAlignment.Center);
        listView1.Columns.Add("Date", 100);


        for (int i = 0; i < dt.Rows.Count; i++)
        {


            DataRow dr = dt.Rows[i];

            //MessageBox.Show(dr.ToString());
            for (int j = 0; j < report.GetLength(0); j++)
            {

                if (report[j, 0].Equals(dr[0].ToString()))
                {
                    MessageBox.Show(dr[0].ToString());
                    if (report[j, 1].Equals(dr[1].ToString()))
                    {
                        MessageBox.Show(dr[1].ToString());

                        report[j, 3] = (Int32.Parse(report[j, 3]) + (int)dr[2]).ToString();

                        MessageBox.Show(dr[2].ToString());

                        report[j, 4] = report[j, 4] + dr[3].ToString();
                        MessageBox.Show(dr[3].ToString());

                        report[j, 5] = report[j, 5] + dr[4].ToString();



                        MessageBox.Show(dr[4].ToString());
                    }
                }
            }
        }





        String[] temp = new String[7];
        for (int i = 0; i < report.GetLength(0); i++)
        {
            temp[0] = report[i, 0].ToString();
            temp[1] = report[i, 1].ToString();
            temp[2] = report[i, 2].ToString();
            temp[3] = report[i, 3].ToString();
            temp[4] = report[i, 4].ToString();
            temp[5] = report[i, 5].ToString();
            //temp[6] = report[i, 6].ToString();

            ListViewItem listItem = new ListViewItem(temp);
            listView1.Items.Add(listItem);

        }

            con.Close();

DateTime now = DateTime.Now; var startDate = new DateTime(now.Year, now.Month, 1); var endDate = startDate.AddMonths(1).AddDays(-1); string[,] report = new string[,] { {"process 1", "sub1", "3", "0", "", "" }, {"process 2", "", "3", "0", "", "" }, {"process 3", "", "3", "0", "", "" } *** there are multiple entries in this array ***** } string totalsquery = "select Process_Name, Sub_Process1_Name, count(id) as total, Completed_By_Employee_Number, max(Refresh_Date) from testDB.dbo.Quality_Data_Master where Refresh_Date between '" + startDate + "' and '" + endDate + "' group by Process_Name, Sub_Process1_Name, Completed_By_Employee_Number, Refresh_Date"; SqlConnection con = new SqlConnection(); SqlDataAdapter ada = new SqlDataAdapter(totalsquery, con); DataTable dt = new DataTable(); ada.Fill(dt); listView1.View = View.Details; listView1.Columns.Add("Process Name", 250); listView1.Columns.Add(" Sub Task", 200); listView1.Columns.Add("Target", 45, HorizontalAlignment.Center); listView1.Columns.Add("Total", 40, HorizontalAlignment.Center); listView1.Columns.Add("Employee", 100, HorizontalAlignment.Center); listView1.Columns.Add("Date", 100); for (int i = 0; i < dt.Rows.Count; i++) { DataRow dr = dt.Rows[i]; //MessageBox.Show(dr.ToString()); for (int j = 0; j < report.GetLength(0); j++) { if (report[j, 0].Equals(dr[0].ToString())) { MessageBox.Show(dr[0].ToString()); if (report[j, 1].Equals(dr[1].ToString())) { MessageBox.Show(dr[1].ToString()); report[j, 3] = (Int32.Parse(report[j, 3]) + (int)dr[2]).ToString(); MessageBox.Show(dr[2].ToString()); report[j, 4] = report[j, 4] + dr[3].ToString(); MessageBox.Show(dr[3].ToString()); report[j, 5] = report[j, 5] + dr[4].ToString(); MessageBox.Show(dr[4].ToString()); } } } } String[] temp = new String[7]; for (int i = 0; i < report.GetLength(0); i++) { temp[0] = report[i, 0].ToString(); temp[1] = report[i, 1].ToString(); temp[2] = report[i, 2].ToString(); temp[3] = report[i, 3].ToString(); temp[4] = report[i, 4].ToString(); temp[5] = report[i, 5].ToString(); //temp[6] = report[i, 6].ToString(); ListViewItem listItem = new ListViewItem(temp); listView1.Items.Add(listItem); } con.Close();

1 个答案:

答案 0 :(得分:0)

我也建议重构你的代码。

不要使用ListView,而是查看DataGridView。

使用DataGridView,您可以轻松完成:

// dataGridView defined at design
// more info on [msdn][1] pages
DataTable dt = GetDataTable(...);
dataGridView.DataSource = dt;