博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

利用C#实现数据同步功能

Posted on 2010-08-17 23:13  moss_tan_jun  阅读(2688)  评论(0编辑  收藏  举报

如下代码摘自

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.IO;
using System.Xml;


namespace LocalTest
{
    public partial class Form6 : Form
    {

        string remoteString;
        string localString;
        SqlCeConnection conn = new SqlCeConnection("DataSource=Test.sdf");

        public Form6()
        {
            InitializeComponent();
        }

        private void Form_Load(object sender, EventArgs e)
        {  
           
            //conn.ConnectionString = "DataSource=mysdf.sdf";
            remoteString = "provider=sqloledb;Data Source=172.18.188.31;Initial Catalog=InfoDB;Persist Security Info=True;User ID=Test;Password=sa";
            localString = "DataSource=Test.sdf";
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                string ls_pcno, ls_pcxh, ls_type, ls_date;
                ls_pcno = tb_no.Text.ToString().Trim();
                ls_pcxh = tb_xh.Text.ToString().Trim();
                ls_type = tb_type.Text.ToString().Trim();
                ls_date = tb_date.Text.ToString().Trim();
                string ls_sql = "insert into pc values ('" + ls_pcno + "', '" + ls_pcxh + "', '" + ls_type + "', '" + ls_date + "')";
                MessageBox.Show(ls_sql);
                SqlCeCommand cmd = new SqlCeCommand(ls_sql);
                conn.Open();
                cmd.Connection = conn;
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("插入数据成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("插入数据失败!" + ex.ToString());
            }
        }

       

        //从SQL Server 把数据同步过来PULL
        private void btn_rec_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess();
                rda.LocalConnectionString = localString;
                rda.InternetUrl = "http://172.18.188.31/test/sqlcesa30.dll";
                rda.Pull("pcinfo", "select * from pc", remoteString,RdaTrackOption.TrackingOn,"ErrorList");
                MessageBox.Show("获取数据成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取数据失败" + ex.ToString());
            }

        }


        // 将修改过的数据同步到SQL Server 数据库中去PUSH
        private void btn_send_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess();
                rda.LocalConnectionString = localString;
                rda.InternetUrl = "http://172.18.188.31/test/sqlcesa30.dll";
                rda.Push("pcinfo", remoteString, RdaBatchOption.BatchingOn);
                MessageBox.Show("同步数据成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("同步数据失败" + ex.ToString());         
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
    }
}