1. 程式人生 > >基於Webservice API 的資料同步的reset的實現

基於Webservice API 的資料同步的reset的實現

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>");
            }