您的位置:首页 > 其它

使用后台线程备份单个数据表

2008-11-06 11:16 337 查看
private void 备份数据ToolStripMenuItem1_Click(object sender, EventArgs e)//使用线程进行备份
{
Thread t = new Thread(new ThreadStart(BackUpProcess));
t.Start();
}

private void BackUpProcess()
{
string[] m_TableName ={ "Table2","Table1", "Logon" };
try
{
if (MessageBox.Show("确定要备份数据到E盘吗?", "备份数据", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
this.Cursor = Cursors.WaitCursor;
for (int i = 0; i < m_TableName.Length; i++)
{
string sql;
sql = "EXEC master..xp_cmdshell 'bcp LightCtrDB.dbo." + m_TableName[i] + " out E://" + m_TableName[i] + ".txt -c -S QRSHZL -U sa -P 123'";
G_SqlExecute.GetExecute(sql);
double percentDone = ((Convert.ToDouble(i + 1) / m_TableName.Length) * 100);
UpdateProcess(percentDone);
}
MessageBox.Show("备份数据表成功!");
}
else
{
MessageBox.Show("备份数据失败!");
}
}
catch (Exception ex)
{
MessageBox.Show("错误" + ex.ToString());
}
finally
{
this.Cursor = Cursors.Default;
toolStripStatusLabel7.Text = null;
ProgressBar1.Value = 0;
}
}

private delegate void UpdateProgressDeletegate(double percentDone);
protected void UpdateProcess(double percentDone)
{
if (InvokeRequired == true)//判断是 主线程还是其他线程。
{
BeginInvoke(
new UpdateProgressDeletegate(UpdateProcess),
new object[] { percentDone });
return;
}
ProgressBar1.Value = Convert.ToInt32(percentDone);

toolStripStatusLabel7.Text = ProgressBar1.Value.ToString() + "% Complete!";
if (percentDone == 0)
{ toolStripStatusLabel7.Text = string.Empty; }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: