formLayout();
}
#endregion
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button btStop;
private System.Windows.Forms.Button btStart;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
}
}
2,后台cs代码
1,定义线程名,线程开始标识,连接服务器成功标识,退出程序标识,源数据库,目标数据库
private Thread preg;//线程名
private bool tureOfalse;//线程执行标识
public bool conYes;//连接服务成功失败标识
private bool one = false;//退出程序标识
string strdbHipis = System.Configuration.ConfigurationManager.AppSettings["NewHmFrameWork_QHS_SQ"].ToString();//源数据库
//string strdbPreg = System.Configuration.ConfigurationManager.AppSettings["NewHmFrameWork_QHS_SQ_QingHai"].ToString();//目标数据库
string strdbPreg = System.Configuration.ConfigurationManager.ConnectionStrings["NewHmFrameWork_QHS_SQ"].ToString();//目标数据库
2,主方法实行调用:
public MainForm()
{
InitializeComponent();
btStop.Enabled = false;//未开启进程,不允许取消进程
//委托
InitTrayIcon();//图标托盘委托
Control.CheckForIllegalCrossThreadCalls = false;//线程委托
this.FormClosing += new FormClosingEventHandler(form_Closing);//退出程序委托
}
3,图标声明
//图标声明
NotifyIcon trayIcon = new NotifyIcon();
//图标路径C:\Users\Administrator\Desktop\DataSynchronousBWokerUI\DataSynchronousBWokerUI\Images\_net_32.ico
private Icon mNetTrayIcon = new Icon("..//Images//_net_32.ico");
4,线程开始
//线程开始
private void btStart_Click(object sender, EventArgs e)
{
tureOfalse = true;
preg = new Thread(new ThreadStart(PREG));
preg.Start();
btStart.Enabled = false;
btStop.Enabled = true;
}
5,线程方法
//线程方法
private void PREG()
{
do
{
listBox1.Items.Add("正在连接源数据库... ...!");
listBox1.Items.Add("正在连接目标数据库... ...!");
ISopenCon();
} while (!conYes);
DoWork_select();
}
6,线程停止
//线程停止
private void btStop_Click(object sender, EventArgs e)
{
tureOfalse = false;
preg.Abort();
/*2015/2/2 调整:点击[取消同步],清空listbox当前数据*/
this.listBox1.Items.Clear();
listBox1.Items.Add("待同步... ...");
//控件显示隐藏
btStart.Enabled = true;
btStop.Enabled = false;
}
7,连接目标数据库失败将信息写入LOG
///
/// 连接目标数据库失败将信息写入LOG
///
///
///
private void fslog(Exception ex)
{
string filepath = DateTime.Now.ToString("yyyyMMdd") + ".log";
FileStream fs = new FileStream(filepath, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
DateTime time = DateTime.Now;
sw.WriteLine(time);
//sw.WriteLine("连接目标数据库失败");
sw.WriteLine(ex.Message);
sw.WriteLine("---------------------------------------------------------");
sw.Close();
fs.Close();
}
8,尝试连接源、目标数据库
///
/// 尝试连接源、目标数据库
///
///
///
private void ISopenCon()
{
try
{
SqlConnection conhipis = new SqlConnection(strdbHipis);
conhipis.Open();
conYes = true;
conhipis.Close();
SqlConnection conPreg = new SqlConnection(strdbPreg);
conPreg.Open();
conYes = true;
conPreg.Close();
}
catch (Exception ex)
{
conYes = false;
fslog(ex);
listBox1.Items.Add("连接数据库失败......");
Thread.Sleep(60000);
}
}
9,尝试删除目标数据库
///
/// 尝试删除目标数据库
///
///
///
private void DelPreg()
{
//try
//{
//bool delResult = true;
////插入数据之前