
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DatabaseSynchronization
{
public partial class DatabaseSynchronizatoin : Form
{
public DatabaseSynchronizatoin()
{
InitializeComponent();
}
int count=0;
private string ConnString(TextBox txtServerName, TextBox txtdbName, TextBox txtUserName, TextBox txtPassword)
//数据库连接字符串的构造函数
{
string ServerName = txtServerName.Text;
string dbName = txtdbName.Text;
string UserName = txtUserName.Text;
string Password = txtPassword.Text;
string ConnectionString = "data source =" + ServerName + ";initial catalog=" + dbName
+ ";user id=" + UserName + ";password=" + Password + ";";
return ConnectionString;
}
private bool CheckConnection(SqlConnection conn)//数据库连接检测函数
{
try
{
conn.Open();
conn.Close();
return true;
}
catch
{
return false;
}
}
private void DeleteRecorders(SqlConnection conn, string tablename)//删除数据表中的数据
{
conn.Open();
SqlCommand cmddeleterecorders=conn.CreateCommand();
= "Delete from " + tablename;
cmddeleterecorders.ExecuteNonQuery();
cmddeleterecorders.Dispose();
conn.Close();
}
private void DatabaseMain()//数据同步实现的过程
{
string connstr1 = ConnString(txtSourceSeverName, txtSourcedbName, txtSourceUserName, txtSourcePassword);
//源数据库的连接字符串
string connstr2 = ConnString(txtAimServerName, txtAimdbName, txtAimUserName, txtAimPassword);
//目标数据库的链接字符串
if (connstr1 == connstr2)
{
MessageBox.Show("目标数据库与源数据库不能是同一数据库!");
return;
}
SqlConnection conn1 = new SqlConnection(connstr1); //源数据库的数据链接
SqlConnection conn2 = new SqlConnection(connstr2); //目标数据库的数据链接
if (!CheckConnection(conn1)) { lblzhishi.Text="连接失败!"; this.Show(); return; }//检测源数据库连接
if (!CheckConnection(conn2)) { lblzhishi.Text = "连接失败"; this.Show(); return; }//检测目标数据库连接
lblzhishi.Text = "连接成功!"; //数据库连接成功的提示
DataSet ds = new DataSet();
//下面开始获取表名与表的个数
string selectext = "select name from sysobjects where xtype='u' order by id desc";
conn2.Open();
SqlDataAdapter datablenames = new SqlDataAdapter(selectext, conn2);
datablenames.Fill(ds,"tablenames");
datablenames.Dispose();
conn2.Close();
int max_tablecount=ds.Tables["tablenames"].Rows.Count;
//数据库中数据表的个数,数据验证MessageBox.Show(max_tablecount.ToString());
for (int tablecount = 0; tablecount < max_tablecount; tablecount++)//遍历所有数据表
{
string tablename = ds.Tables["tablenames"].Rows[tablecount][0].ToString();
//取出当前数据表表名 MessageBox.Show(tablename);
if (tablename == "dtproperties") { continue; }
DeleteRecorders(conn2,tablename);
string selectrecorders = "select * from " + tablename;
conn1.Open();
SqlDataAdapter darecorders = new SqlDataAdapter(selectrecorders, conn1);
darecorders.Fill(ds,tablename);
darecorders.Dispose();
conn1.Close();
int max_colid=ds.Tables[tablename].Columns.Count;
//当前表的列数,数据验证MessageBox.Show(max_colid.ToString(),tablename);
int max_rowid = ds.Tables[tablename].Rows.Count;
//当前表的记录数,数据验证MessageBox.Show(max_rowid.ToString(),tablename);
for (int rowcount=0; rowcount < max_rowid; rowcount++)//遍历当前表中所有的记录
{
string insertrecorder = " insert into "+ tablename+"(";
for (int colcount = 0; colcount < max_colid; colcount++)//将列名写入数据插入命令
{
string colname = ds.Tables[tablename].Columns[colcount].ColumnName;
//取得当前列列名
insertrecorder += colname+",";
string checktable = "SELECT COLUMNPROPERTY(object_id('"+tablename+"'),'"+colname+"','IsIdentity')";
conn1.Open();
SqlDataAdapter dachecktable = new SqlDataAdapter(checktable,conn1);
dachecktable.Fill(ds,"check"+tablename);
dachecktable.Dispose();
conn1.Close();
if(Convert.ToBoolean(Convert.ToInt32(ds.Tables["check"+tablename].Rows[0][0].ToString())))
{
if (insertrecorder.Substring(0, 3) != "set")
{
insertrecorder = "set identity_insert " + tablename + " on " + insertrecorder;
}
}
}
insertrecorder = insertrecorder.Substring(0, insertrecorder.Length - 1);
insertrecorder += ") values" + "(";
for (int colcount = 0; colcount < max_colid; colcount++)//将列值写入命令
{
if (ds.Tables[tablename].Rows[rowcount][colcount].ToString().Replace('\'', ' ').Trim() == "Null")
{
ds.Tables[tablename].Rows[rowcount][colcount] = null;
}
//string ceshi = ds.Tables[tablename].Rows[rowcount][colcount].GetType().ToString();
if (ds.Tables[tablename].Rows[rowcount][colcount].GetType().ToString() == "System.Decimal")
{
insertrecorder = insertrecorder + ds.Tables[tablename].Rows[rowcount][colcount].ToString().Trim() + ",";
}
else
{
insertrecorder = insertrecorder + " \'" + ds.Tables[tablename].Rows[rowcount][colcount].ToString().Trim() + "',";
}
}
insertrecorder = insertrecorder.Substring(0,insertrecorder.Length-1);
insertrecorder += ")";//MessageBox.Show(insertrecorder);
conn2.Open();
SqlCommand cmdinset = conn2.CreateCommand();
= insertrecorder;
cmdinset.ExecuteNonQuery();
cmdinset.Dispose();
conn2.Close();
}
}
conn1.Dispose();
conn2.Dispose();
count++;
lblzhishi1.Visible = true;
lblzhishi2.Visible = true;
lblzhishi2.Text = count.ToString();
lblzhishi3.Visible = true;
lblzhishi.Visible = false;
}
private void btnDataExchange_Click(object sender, EventArgs e)
{
DatabaseMain();
int operatorIndex=cbbtime.SelectedIndex;
if (txttime.Text=="")
{
MessageBox.Show("请设置同步周期!");
return;
}
int tongbutime=Convert.ToInt32(txttime.Text);
switch (operatorIndex)
{
case 0:
tongbutime *= 1000 * 60 * 60 * 24 * 7;
break;
case 1:
tongbutime *= 1000 * 60 * 60 * 24;
break;
case 2:
tongbutime *= 1000 * 60 * 60;
break;
case 3:
tongbutime *= (1000 * 60);
break;
case 4:
tongbutime *= 1000;
break;
case -1:
MessageBox.Show("请设置同步周期!");
tongbutime = 1;
return;
}
timer.Interval = tongbutime;
timer.Enabled = true;
btnzuixiaohua.Enabled = true;
}
private void timer_Tick(object sender, EventArgs e)
{
DatabaseMain();
}
private void btnEnd_Click(object sender, EventArgs e)
{
timer.Enabled = false;
lblzhishi.Text = "用户终止同步";
lblzhishi.Visible = true;
lblzhishi1.Visible = false;
lblzhishi2.Visible = false;
lblzhishi3.Visible = false;
}
private void btnzuixiaohua_Click(object sender, EventArgs e)
{
this.Hide();
nfiDatabase.Visible = true;
}
private void nfiDatabase_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
nfiDatabase.Visible = false;
}
}
}
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DatabaseSynchronization
{
public partial class DatabaseSynchronizatoin : Form
{
public DatabaseSynchronizatoin()
{
InitializeComponent();
}
int count=0;
private string ConnString(TextBox txtServerName, TextBox txtdbName, TextBox txtUserName, TextBox txtPassword)
//数据库连接字符串的构造函数
{
string ServerName = txtServerName.Text;
string dbName = txtdbName.Text;
string UserName = txtUserName.Text;
string Password = txtPassword.Text;
string ConnectionString = "data source =" + ServerName + ";initial catalog=" + dbName
+ ";user id=" + UserName + ";password=" + Password + ";";
return ConnectionString;
}
private bool CheckConnection(SqlConnection conn)//数据库连接检测函数
{
try
{
conn.Open();
conn.Close();
return true;
}
catch
{
return false;
}
}
private void DeleteRecorders(SqlConnection conn, string tablename)//删除数据表中的数据
{
conn.Open();
SqlCommand cmddeleterecorders=conn.CreateCommand();
= "Delete from " + tablename;
cmddeleterecorders.ExecuteNonQuery();
cmddeleterecorders.Dispose();
conn.Close();
}
private void DatabaseMain()//数据同步实现的过程
{
string connstr1 = ConnString(txtSourceSeverName, txtSourcedbName, txtSourceUserName, txtSourcePassword);
//源数据库的连接字符串
string connstr2 = ConnString(txtAimServerName, txtAimdbName, txtAimUserName, txtAimPassword);
//目标数据库的链接字符串
if (connstr1 == connstr2)
{
MessageBox.Show("目标数据库与源数据库不能是同一数据库!");
return;
}
SqlConnection conn1 = new SqlConnection(connstr1); //源数据库的数据链接
SqlConnection conn2 = new SqlConnection(connstr2); //目标数据库的数据链接
if (!CheckConnection(conn1)) { lblzhishi.Text="连接失败!"; this.Show(); return; }//检测源数据库连接
if (!CheckConnection(conn2)) { lblzhishi.Text = "连接失败"; this.Show(); return; }//检测目标数据库连接
lblzhishi.Text = "连接成功!"; //数据库连接成功的提示
DataSet ds = new DataSet();
//下面开始获取表名与表的个数
string selectext = "select name from sysobjects where xtype='u' order by id desc";
conn2.Open();
SqlDataAdapter datablenames = new SqlDataAdapter(selectext, conn2);
datablenames.Fill(ds,"tablenames");
datablenames.Dispose();
conn2.Close();
int max_tablecount=ds.Tables["tablenames"].Rows.Count;
//数据库中数据表的个数,数据验证MessageBox.Show(max_tablecount.ToString());
for (int tablecount = 0; tablecount < max_tablecount; tablecount++)//遍历所有数据表
{
string tablename = ds.Tables["tablenames"].Rows[tablecount][0].ToString();
//取出当前数据表表名 MessageBox.Show(tablename);
if (tablename == "dtproperties") { continue; }
DeleteRecorders(conn2,tablename);
string selectrecorders = "select * from " + tablename;
conn1.Open();
SqlDataAdapter darecorders = new SqlDataAdapter(selectrecorders, conn1);
darecorders.Fill(ds,tablename);
darecorders.Dispose();
conn1.Close();
int max_colid=ds.Tables[tablename].Columns.Count;
//当前表的列数,数据验证MessageBox.Show(max_colid.ToString(),tablename);
int max_rowid = ds.Tables[tablename].Rows.Count;
//当前表的记录数,数据验证MessageBox.Show(max_rowid.ToString(),tablename);
for (int rowcount=0; rowcount < max_rowid; rowcount++)//遍历当前表中所有的记录
{
string insertrecorder = " insert into "+ tablename+"(";
for (int colcount = 0; colcount < max_colid; colcount++)//将列名写入数据插入命令
{
string colname = ds.Tables[tablename].Columns[colcount].ColumnName;
//取得当前列列名
insertrecorder += colname+",";
string checktable = "SELECT COLUMNPROPERTY(object_id('"+tablename+"'),'"+colname+"','IsIdentity')";
conn1.Open();
SqlDataAdapter dachecktable = new SqlDataAdapter(checktable,conn1);
dachecktable.Fill(ds,"check"+tablename);
dachecktable.Dispose();
conn1.Close();
if(Convert.ToBoolean(Convert.ToInt32(ds.Tables["check"+tablename].Rows[0][0].ToString())))
{
if (insertrecorder.Substring(0, 3) != "set")
{
insertrecorder = "set identity_insert " + tablename + " on " + insertrecorder;
}
}
}
insertrecorder = insertrecorder.Substring(0, insertrecorder.Length - 1);
insertrecorder += ") values" + "(";
for (int colcount = 0; colcount < max_colid; colcount++)//将列值写入命令
{
if (ds.Tables[tablename].Rows[rowcount][colcount].ToString().Replace('\'', ' ').Trim() == "Null")
{
ds.Tables[tablename].Rows[rowcount][colcount] = null;
}
//string ceshi = ds.Tables[tablename].Rows[rowcount][colcount].GetType().ToString();
if (ds.Tables[tablename].Rows[rowcount][colcount].GetType().ToString() == "System.Decimal")
{
insertrecorder = insertrecorder + ds.Tables[tablename].Rows[rowcount][colcount].ToString().Trim() + ",";
}
else
{
insertrecorder = insertrecorder + " \'" + ds.Tables[tablename].Rows[rowcount][colcount].ToString().Trim() + "',";
}
}
insertrecorder = insertrecorder.Substring(0,insertrecorder.Length-1);
insertrecorder += ")";//MessageBox.Show(insertrecorder);
conn2.Open();
SqlCommand cmdinset = conn2.CreateCommand();
= insertrecorder;
cmdinset.ExecuteNonQuery();
cmdinset.Dispose();
conn2.Close();
}
}
conn1.Dispose();
conn2.Dispose();
count++;
lblzhishi1.Visible = true;
lblzhishi2.Visible = true;
lblzhishi2.Text = count.ToString();
lblzhishi3.Visible = true;
lblzhishi.Visible = false;
}
private void btnDataExchange_Click(object sender, EventArgs e)
{
DatabaseMain();
int operatorIndex=cbbtime.SelectedIndex;
if (txttime.Text=="")
{
MessageBox.Show("请设置同步周期!");
return;
}
int tongbutime=Convert.ToInt32(txttime.Text);
switch (operatorIndex)
{
case 0:
tongbutime *= 1000 * 60 * 60 * 24 * 7;
break;
case 1:
tongbutime *= 1000 * 60 * 60 * 24;
break;
case 2:
tongbutime *= 1000 * 60 * 60;
break;
case 3:
tongbutime *= (1000 * 60);
break;
case 4:
tongbutime *= 1000;
break;
case -1:
MessageBox.Show("请设置同步周期!");
tongbutime = 1;
return;
}
timer.Interval = tongbutime;
timer.Enabled = true;
btnzuixiaohua.Enabled = true;
}
private void timer_Tick(object sender, EventArgs e)
{
DatabaseMain();
}
private void btnEnd_Click(object sender, EventArgs e)
{
timer.Enabled = false;
lblzhishi.Text = "用户终止同步";
lblzhishi.Visible = true;
lblzhishi1.Visible = false;
lblzhishi2.Visible = false;
lblzhishi3.Visible = false;
}
private void btnzuixiaohua_Click(object sender, EventArgs e)
{
this.Hide();
nfiDatabase.Visible = true;
}
private void nfiDatabase_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
nfiDatabase.Visible = false;
}
}
}
这是项目代码
[ 本帖最后由 zghnxzdcx 于 2010-9-24 12:49 编辑 ]