【文件属性】:
文件名称:my sql与sql server实时同步源码,自定义设置同步时间
文件大小:358KB
文件格式:RAR
更新时间:2017-04-21 12:34:40
mysql sqlserver 实时数据同步
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
//本程序的功能是实时从mysql中读取一条记录中的一个字段,插入到sqlserver 中,经过长时间的运行,基本无错了,
而这数据同步,目前没有实时性强的软件,至少1分钟,太久了,自己写了一个,
//可以自己设置同步时间,字符连接串等,给了源码,而且对程序进行了很多优化
容错性很强,懂点程序的 就可以直接修改源码 就可以用了。由于没有时间按做界面,只能自己修改了,不过已经很稳定了,为了给大家提供点方便,免得还去查大量的资料。
namespace tool
{
public partial class Form1 : Form
{
public string mysql_Conn = "Database='cmccbbs';Data Source='211.139.22.124';UserId='cmcc';Password='CMCC2014cmcc2014';charset='utf8';pooling=true";
public string mysql_sql = "select max(id) from pre_sms_tempsend";
// public string sql_server_Conn = "Data Source=lei; DataBase=test; User=sa;PWD=123456"; 211.222.229.124
public string id, phone, verify, last_id;
SqlConnection my_sql_con;
MySqlConnection con;
MySqlCommand cmd;
DataSet ds;
public string sql_server_Conn = "Data Source=222.85.144.112,14444; DataBase=DB_CustomSMS; User=sms;PWD=sms2014";
// public string sql_sql = "select max(id) from pre_sms_tempsend";
MySqlDataAdapter md;
public int ii = 0;
public Form1()
{
InitializeComponent();
timer1.Interval = 1000;
//连接mysql
con = new MySql.Data.MySqlClient.MySqlConnection(mysql_Conn);
con.Open();
cmd = new MySqlCommand("select * from pre_sms_tempsend order by id desc limit 1", con);
cmd.Connection = con;
md = new MySqlDataAdapter();
ds = new DataSet();
//这样做避免一直new,导致内存泄露
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "start")
{
timer1.Enabled = true;
button1.Text = "stop";
}
else
{
timer1.Enabled = false;
button1.Text = "start";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
// //设置一个判断,当此次和上次产生的严重
if (con.State != ConnectionState.Open)
{
con.Open();
}
md.SelectCommand = cmd;
md.Fill(ds, "pre_sms_tempsend");
foreach (DataRow testRow in ds.Tables["pre_sms_tempsend"].Rows)
{
id = testRow["id"].ToString();
phone = testRow["mobile"].ToString();
verify = testRow["content"].ToString();
}
if (con.State != ConnectionState.Closed)
{
con.Close();
}
if (id != last_id)
{
//初始化sql 连接
my_sql_con = new SqlConnection(sql_server_Conn);
my_sql_con.Open();
SqlCommand sqlcom = new SqlCommand("insert into oa_smsend( Mobile,Content) values('" + phone + "','" + verify + "') ", this.my_sql_con);
sqlcom.ExecuteNonQuery();
last_id = id;
my_sql_con.Close();
}
}
catch (Exception ex)
{
throw ex ;
}
}
}
}
【文件预览】:
tool
----tool.suo(20KB)
----tool.sln(902B)
----UpgradeLog.XML(3KB)
----_UpgradeReport_Files()
--------UpgradeReport.xslt(12KB)
--------UpgradeReport_Minus.gif(69B)
--------UpgradeReport.css(3KB)
--------UpgradeReport_Plus.gif(71B)
----tool()
--------bin()
--------Form1.Designer.cs(2KB)
--------Program.cs(485B)
--------obj()
--------MySql.Data.dll(268KB)
--------Form1.cs(3KB)
--------Form1.resx(6KB)
--------tool.csproj(5KB)
--------Properties()
--------tool.csproj.user(478B)
网友评论
- 有点不好用。拿来改改才好用。
- 有点作用,学习了!
- 这个太简单了,没有校验等等,不可靠,学习一下。