I have this functionality is achieved when inserting data to a remote MySql database, insert a same data in the same local database MySql.
During insertion, at the same time I did not connect the two databases. Process is as follows:
- First, insert data using SQL statements to a remote database
- Then close the connection to the remote database
- And then start to connect the local database
- Then use SQL statements to insert data into a local database
- Finally, turn off the local database and remote database connection
Part of the code as follows:
- connect is used to connect the database, the remote database if the connection is not on, it is connected to the local database. judge for judgment, 1 represents a remote database, the local database is connected represents 0.
- disconnect the current connection is disconnected database.
- connectlocal is connected to a local database.
sql = "insert into doctor(user,pwd,reg_date) VALUE ('" + MD5_new_admin + "','" + password2 + "',now());";
Goal_Mysql_var.mysql.exec_mysql_command_execute(sql);
mysql_none mn = new mysql_none();
if (mn.getjudge().ToString() == "1")
{
mn.disconnect();
mn.connect1ocal();
sql = "insert into doctor(user,pwd,reg_date,judge) VALUE ('" + MD5_new_admin + "','" + password2 + "',now(),1);";
Goal_Mysql_var.mysql.exec_mysql_command_execute(sql);
mn.disconnect();
mn.connect();
}
public bool connect() //连接ssh主机,登陆MySQL数据库
{
string sqlConn = "Database=" + Mysql_Database + ";Data Source=" + sqlHost + ";Port=" + client_port.ToString() + ";User Id=" + Mysql_User + ";Password=" + Mysql_password + ";CharSet=" + Mysql_Charset + ";Connection Timeout=4"; //登陆MySQL命令
conn = new MySqlConnection("Server=;UserId=;Password=;Database=;Charset=utf8");//连接MySQL数据库
//远程数据库的信息都删除了,可填写自己的
//conn = new MySqlConnection("Server=localhost;UserId=root;Password=123456;Database=vr");//连接MySQL数据库
try
{
conn.Open();
judge = 1;
}
catch (Exception ee)
{
//连接MySQL数据库出错 此处一般是服务器端出错,或者本机网络突然崩溃 只要连接语句无错误 几乎不会再此处出现异常
// Console.WriteLine("first..." + ee.Message);
conn = new MySqlConnection("Server=localhost;UserId=root;Password=123456;Database=vr;Charset=utf8");//连接MySQL本地数据库
judge = 0;
return true;
}
return true;
}
public bool connect1ocal() //连接ssh主机,登陆MySQL数据库
{
string sqlConn = "Database=" + Mysql_Database + ";Data Source=" + sqlHost + ";Port=" + client_port.ToString() + ";User Id=" + Mysql_User + ";Password=" + Mysql_password + ";CharSet=" + Mysql_Charset + ";Connection Timeout=4"; //登陆MySQL命令
conn = new MySqlConnection("Server=localhost;UserId=root;Password=123456;Database=vr;Charset=utf8");//连接MySQL本地数据库
conn.Open();
return true;
}
public bool disconnect() //断开ssh和MySQL连接
{
try
{
// client.Disconnect(); //断开ssh连接
conn.Close(); //断开MySQL连接
}
catch (Exception e)
{
return false;
}
return true;
}