这是我的代码
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();
时间太长了怎么解决这个问题。我的表结构中不允许索引。任何其他解决方案都会有所帮助。