-1

这是我的代码

private void button1_Click(object sender, EventArgs e)
    {
        double prate = 0;
        double srate = 0;
        double rate = 0;
        double qty = 0;
        double val = 0; bool b = false;
        List<string> item = new List<string>();
        List<string> brand = new List<string>();
        List<string> station = new List<string>();
        List<string> dealer = new List<string>();
        int m1=0, m2=0;
        for (int k = 0; k < treeView1.Nodes.Count; k++)
        {
            for (int l = 0; l < treeView1.Nodes[k].Nodes.Count; l++)
            {

                if (treeView1.Nodes[k].Nodes[l].Checked == true)
                {
                    brand.Add(treeView1.Nodes[k].Tag.ToString());
                    item.Add(treeView1.Nodes[k].Nodes[l].Tag.ToString());
                    m1++;
                }
            }
        }



        for (int i = 0; i < treeView2.Nodes.Count; i++)
        {

            for (int j = 0; j < treeView2.Nodes[i].Nodes.Count; j++)
            {
                if (treeView2.Nodes[i].Nodes[j].Checked == true)
                {
                    station.Add(treeView2.Nodes[i].Tag.ToString());
                    dealer.Add(treeView2.Nodes[i].Nodes[j].Tag.ToString());
                    m2++;
                }
            }
        }

        string[] brands; string[] items; string[] stations; string[] dealers;
        brands = brand.ToArray();
        items = item.ToArray();
        stations = station.ToArray();
        dealers = dealer.ToArray();
        SqlConnection con = new SqlConnection(Global.constr);
        con.Open();
        SqlCommand del = new SqlCommand("delete PLReport", con);
        del.ExecuteNonQuery();
        del.Dispose();
        for (int i = 0; i < m1; i++)
        {

            prate = 0;
            SqlCommand cmdr = new SqlCommand("select top 1 purchasePrice from PurchaseDetails where ItemCode='" + item[i] + "' order by doc_date Desc", con);
            SqlDataReader drr = cmdr.ExecuteReader();
            while (drr.Read())
            {
                prate = Convert.ToDouble(drr[0]);

            }
            drr.Close();
            cmdr.Dispose();
            for (int j = 0; j < m2; j++)
            {
                SqlCommand cmd33 = new SqlCommand("select slno from SalesDetails where ItemCode='" + items[i] + "' and dealer='" + dealers[j] + "' and date between '" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "' and '" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "'", con);
                SqlDataReader r33 = cmd33.ExecuteReader();
                if (!r33.HasRows)
                    continue;
                r33.Close();
                cmd33.Dispose();
                SqlCommand cmd = new SqlCommand("select sum(Qty),sum(rate) from SalesDetails where ItemCode='" + items[i] + "' and dealer='" + dealers[j] + "' and date between '" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "' and '" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "' group by ItemCode,Dealer", con);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    //MessageBox.Show("fgfg");
                    srate = 0;
                    b = double.TryParse(dr[1].ToString(), out srate);
                    rate = 0;
                    if (srate != 0)
                        rate = srate - prate;
                    qty = 0;
                    b = double.TryParse(dr[0].ToString(), out qty);
                    val = qty * rate;
                    if (checkBox3.Checked)//zero valued entry
                    {
                        SqlCommand cmd2 = new SqlCommand("insert PLReport values('" + stations[j] + "','" + dealers[j] + "','" + brands[i] + "','" + items[i] + "','" + val + "')", con);
                        cmd2.ExecuteNonQuery();
                        cmd2.Dispose();
                    }
                    else
                    {
                        if (val != 0)
                        {
                            SqlCommand cmd2 = new SqlCommand("insert PLReport values('" + stations[j] + "','" + dealers[j] + "','" + brands[i] + "','" + items[i] + "','" + val + "')", con);
                            cmd2.ExecuteNonQuery();
                            cmd2.Dispose();
                        }
                    }
                }
                dr.Close();
                cmd.Dispose();
            }


        }


            frmPLReport obj = new frmPLReport();
            obj.ttle = "Item Wise Profit & Loss report from " + dateTimePicker1.Value.ToString("dd/MM/yyyy") + " to " + dateTimePicker2.Value.ToString("dd/MM/yyyy");
            obj.option = 0;
            obj.ShowDialog();

        con.Close();
    }

一个“i”循环大约需要 4 秒来执行,我有大约 8000 条记录和(项目)1000+(经销商)400 个条件。

这部分需要很长时间才能执行:

SqlCommand cmd = new SqlCommand("select sum(Qty),sum(rate) from SalesDetails where ItemCode='" + items[i] + "' and dealer='" + dealers[j] + "' and date between '" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "' and '" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "' group by ItemCode,Dealer", con);
SqlDataReader dr = cmd.ExecuteReader();

时间太长了怎么解决这个问题。我的表结构中不允许索引。任何其他解决方案都会有所帮助。

4

1 回答 1

2

首先,我不明白对数据库的查询是否花费了太长时间或函数的其余部分或两者兼而有之。

如果你不知道,你应该从测试它开始。你可以用一个简单的DateTime.Now,看看每个部分有多少滴答声。

如果你想用更好的方法测试它,你可以像 V4Vendetta 所说的那样使用 StopWatch。你可以在这里找到一个例子,基本上语法如下:

// Create new stopwatch
Stopwatch stopwatch = new Stopwatch();

// Begin timing
stopwatch.Start();

// YOUR CODE HERE

// Stop timing
stopwatch.Stop();

// Write result
Console.WriteLine("Time elapsed: {0}",
    stopwatch.Elapsed);

因为数据库访问需要太长时间,我建议您不要调用太多的 sql 查询。我看到你在一个for循环中调用:

“从 SalesDetails 中选择 slno...”

获取所有表格并使用 linq 可能会更好。这样您就可以节省大量的数据库调用。其余的数据库查询也是如此,我认为我们最多可以使用 3-5 个数据库调用。

于 2013-09-03T11:15:31.660 回答