110,535
社区成员
发帖
与我相关
我的任务
分享
private void ExecJindu_Load(object sender, EventArgs e)
{
lab_AllNum.Text = sqlStr.Count.ToString();
lab_execNum.Text = "0";
pb_JinduTiao.Maximum = sqlStr.Count;
CheckForIllegalCrossThreadCalls = false;
ExecSql = new Thread(ExecSqlThr);
ExecSql.Start();
}
private void ExecSqlThr()
{
openConn();
SqlTransaction st = Conn.BeginTransaction();
try
{
int exNum = 0;
for (int i = 0; i < sqlStr.Count; i++)
{
//开始执行SQL
using (SqlCommand cmd = new SqlCommand(sqlStr[i].ToString(), Conn))
{
if (execDesc[i].ToString()!="")
{
lab_ExecIng.Text = "正在执行:" + execDesc[i].ToString();
lab_ExecIng.Location = new Point((this.Width - lab_ExecIng.Width) / 2, lab_ExecIng.Location.Y);
}
cmd.Transaction = st;
cmd.ExecuteNonQuery();
exNum++;
pb_JinduTiao.Value = exNum;
lab_execNum.Text = pb_JinduTiao.Value.ToString();
Thread.Sleep(800);
}
}
//全部执行完成后的代码,并提交SQL结果
if (exNum >= pb_JinduTiao.Maximum)
{
st.Commit();
Thread.Sleep(500);
Res.Text = exNum.ToString();
lab_ExecIng.Text = "执行成功....";
lab_ExecIng.Location = new Point((this.Width - lab_ExecIng.Width) / 2, lab_ExecIng.Location.Y);
lab_ExecIng.ForeColor = Color.Blue;
Thread.Sleep(1000);
}
else
{
Res.Text = "本次共需要执行:"+ pb_JinduTiao.Maximum.ToString()+"条命令,但系统只执行了:"+exNum.ToString()+"条命令\n本次操作失败,撤销已执行命令。\n情重试或联系开发商";
st.Rollback();
}
this.Close();
this.Dispose();
}
catch (Exception ex)
{
Res.Text = "错误信息:\n" + ex.Message;
st.Rollback();
this.Close();
this.Dispose();
}
finally
{
closeConn();
}
ExecSql.Abort();
}