首页  ·  知识 ·  编程语言
用实现数据同步功能
yqy542006  http://www.cnblogs.com/yqy542006/archive/2007/06/0  .NET  编辑:dezai  图片来源:网络
postBody> using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Dra

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

      
    }
}

本文作者:yqy542006 来源:http://www.cnblogs.com/yqy542006/archive/2007/06/0
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读