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

  • 格式:pdf
  • 大小:29.00 KB
  • 文档页数:2

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

如下代码摘⾃

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 = "";

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 = "";

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

}

}

}