1、数据库A主键增长值2 数据库B主键增长值2
2、设计数据库A的公共API
3、调用的实现
SqlConnection myconn = new SqlConnection("server=.;database=;integrated security=True");
myconn.Open();
localhost.Service service = new localhost.Service(); //本地测试的服务器
SqlDataAdapter sda1 = new SqlDataAdapter("select count(*) from tableName where id%2=0", myconn);
DataTable dt1 = new DataTable();
sda1.Fill(dt1);
int numB = Convert.ToInt32(dt1.Rows[0][0]);
int numA=service.getMessageLengthEven();
myconn.Close();
myconn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from tableName where id%2=0 order by id asc", myconn);
DataTable dt = new DataTable();
sda.Fill(dt);
myconn.Close();
int i=0;
if(numA<=numB){
for(;i<numA;i++)
{
int id=Convert.ToInt32(dt.Rows[i][0]);
string name=dt.Rows[i][1].ToString();
int check=service.sendMessageArray(id,name);
if (check == 0)
{
service.updateMessageArray(id,name);
}
}
for(int j=numA;j<numB;j++)
{
int id = Convert.ToInt32(dt.Rows[j][0]);
string name = dt.Rows[j][1].ToString();
service.insertDB(id,name);
}
}
else
{
Response.Write("<script>alert('数据库B出现异常情况请联系后台管理员,电话*****!!!!'); history.go(-1);</script>");
}
myconn.Open();
SqlDataAdapter sda2= new SqlDataAdapter("select count(*) from customers where id%2=1", myconn);
DataTable dt2 = new DataTable();
sda2.Fill(dt2);
int numB1 = Convert.ToInt32(dt2.Rows[0][0]);
int numA1 = service.getMessageLengthOdd();
myconn.Close();
myconn.Open();
SqlDataAdapter sda3 = new SqlDataAdapter("select * from customers where id%2=1 order by id asc", myconn);
DataTable dt3 = new DataTable();
sda3.Fill(dt3);
myconn.Close();
//循环判断数据库A是否查询结束
if (numA1 >= numB1)
{
for (i=0; i < numB1; i++) {
string[] Str = service.getMessageArray(i*2+1);
int id = Convert.ToInt32(dt3.Rows[i][0]);
string name = dt3.Rows[i][1].ToString();
// int check = service.sendMessageArray(id, name);
//int select = service.selectMessageByID(id);
if (id ==Convert.ToInt32(Str[0])&&name!=Str[1])
{
// string[] Str = service.getMessageArray(id).ToArray();
myconn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = myconn;
cmd.CommandText = "UPDATE customers SET name='" + Str[1] + "' WHERE id=" + Str[0];
cmd.ExecuteNonQuery();
myconn.Close();
}
for(int j=numB1;j<numA1;j++){
// string[] Str = service.getMessageArray(id).ToArray();
myconn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = myconn;
cmd.CommandText = "INSERT INTO customers (id,name) VALUES(" + Str[0] + ",'" + Str[1] + "')";
cmd.ExecuteNonQuery();
myconn.Close();
}
}
Response.Write("<script>alert('同步操作还原成功!!!!'); history.go(-1);</script>");
}
else
{
Response.Write("<script>alert('数据库A出现异常情况请联系后台管理员,电话*****!!!!'); history.go(-1);</script>");
}
myconn.Open();
localhost.Service service = new localhost.Service(); //本地测试的服务器
SqlDataAdapter sda1 = new SqlDataAdapter("select count(*) from tableName where id%2=0", myconn);
DataTable dt1 = new DataTable();
sda1.Fill(dt1);
int numB = Convert.ToInt32(dt1.Rows[0][0]);
int numA=service.getMessageLengthEven();
myconn.Close();
myconn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from tableName where id%2=0 order by id asc", myconn);
DataTable dt = new DataTable();
sda.Fill(dt);
myconn.Close();
int i=0;
if(numA<=numB){
for(;i<numA;i++)
{
int id=Convert.ToInt32(dt.Rows[i][0]);
string name=dt.Rows[i][1].ToString();
int check=service.sendMessageArray(id,name);
if (check == 0)
{
service.updateMessageArray(id,name);
}
}
for(int j=numA;j<numB;j++)
{
int id = Convert.ToInt32(dt.Rows[j][0]);
string name = dt.Rows[j][1].ToString();
service.insertDB(id,name);
}
}
else
{
Response.Write("<script>alert('数据库B出现异常情况请联系后台管理员,电话*****!!!!'); history.go(-1);</script>");
}
myconn.Open();
SqlDataAdapter sda2= new SqlDataAdapter("select count(*) from customers where id%2=1", myconn);
DataTable dt2 = new DataTable();
sda2.Fill(dt2);
int numB1 = Convert.ToInt32(dt2.Rows[0][0]);
int numA1 = service.getMessageLengthOdd();
myconn.Close();
myconn.Open();
SqlDataAdapter sda3 = new SqlDataAdapter("select * from customers where id%2=1 order by id asc", myconn);
DataTable dt3 = new DataTable();
sda3.Fill(dt3);
myconn.Close();
//循环判断数据库A是否查询结束
if (numA1 >= numB1)
{
for (i=0; i < numB1; i++) {
string[] Str = service.getMessageArray(i*2+1);
int id = Convert.ToInt32(dt3.Rows[i][0]);
string name = dt3.Rows[i][1].ToString();
// int check = service.sendMessageArray(id, name);
//int select = service.selectMessageByID(id);
if (id ==Convert.ToInt32(Str[0])&&name!=Str[1])
{
// string[] Str = service.getMessageArray(id).ToArray();
myconn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = myconn;
cmd.CommandText = "UPDATE customers SET name='" + Str[1] + "' WHERE id=" + Str[0];
cmd.ExecuteNonQuery();
myconn.Close();
}
for(int j=numB1;j<numA1;j++){
// string[] Str = service.getMessageArray(id).ToArray();
myconn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = myconn;
cmd.CommandText = "INSERT INTO customers (id,name) VALUES(" + Str[0] + ",'" + Str[1] + "')";
cmd.ExecuteNonQuery();
myconn.Close();
}
}
Response.Write("<script>alert('同步操作还原成功!!!!'); history.go(-1);</script>");
}
else
{
Response.Write("<script>alert('数据库A出现异常情况请联系后台管理员,电话*****!!!!'); history.go(-1);</script>");
}