利用C#實現資料同步功能

來源:互聯網
上載者:User

如下代碼摘自

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

      
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.