每次都是插5000+条记录进去,都要等个4-5秒,请问下有什么办法可以优化吗?
for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
{
string str = "insert into TEimpdatas Values('" + this.label3.Text + "','" + this.dataGridView1.Rows[i].Cells[0].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[1].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[2].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[3].Value.ToString() + "','" + DateTime.Now.ToLocalTime().ToString() + "','')";
SqlCommand objcom = new SqlCommand(str, objcon);
objcom.ExecuteNonQuery();
}
for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
{
string str = "insert into TEimpdatas Values('" + this.label3.Text + "','" + this.dataGridView1.Rows[i].Cells[0].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[1].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[2].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[3].Value.ToString() + "','" + DateTime.Now.ToLocalTime().ToString() + "','')";
SqlCommand objcom = new SqlCommand(str, objcon);
objcom.ExecuteNonQuery();
}
2. SqlBulkCopy
{
if (a == 0)
{
try
{
for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
{
string str = "insert into TEimpdatas Values('" + this.label3.Text + "','" + this.dataGridView1.Rows[i].Cells[0].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[1].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[2].Value.ToString() + "','"
+ this.dataGridView1.Rows[i].Cells[3].Value.ToString() + "','" + DateTime.Now.ToLocalTime().ToString() + "','')";
SqlCommand objcom = new SqlCommand(str, objcon);
objcom.ExecuteNonQuery();
}
MessageBox.Show("OK!"); }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
objcon.Close();
}
}
else
{
MessageBox.Show("文件已存在!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
SqlBulkCopy
http://www.cnblogs.com/wlb/archive/2010/03/02/1676136.html