send data from mysql to MS-SQL database
I have a table name student with fields, name and roll in my-sql database. Also, I have a same table name student in MS-SQL database with the fields, name and roll.
When a va开发者_运维问答lue inserted into my MySQL database student table, I need to auto-insert the same value into the MS-SQL database student table.
Why are you using two separate databases to do the same thing? can't you just choose one? If you are just trying to export and import data you should be able to export as a csv from one database and import into the other, and then only use the database you need to.
If not i suppose you could write a class that wraps around two database objects and actually executes every query on both databases.
for example:
public class database
{
public DbConnection mySqlConnection;
public DbConnection sqlConnection;
/// <summary>
/// open two db connections
/// </summary>
public database()
{
mySqlConnection =//string that will open the mysql
mySqlconnection = //insert string will open the sql server database
}
public excecuteNonQuery(String SQL)
{
//cmd1 = mysql command
//cmd2 = mssql command
cmd1.excecute(SQL);
cmd2.execute(SQL);
}
}
And just create functions that handle preparing the commands and inserting the data, so that the operations are performed at the same time on both of the databases. I'm not aware of any method of replicating a mysql database to a mssql database.
You could have a cron that runs every so often to insert records it hasn't seen yet.
// Assuming the primary key is an id int auto_increment
startId = getLastIdFromPreviousRun();
lastId = 0;
offset = 0;
while(true) {
mysqlrs = mysqlQuery(
"SELECT * FROM users WHERE id > ? ORDER BY id LIMIT ?, 1000",
startId,
offset
);
if (!mysqlrs || mysqlrs.count() == 0)
break;
foreach(mysqlrs as mysqlrow) {
mssqlInsert('users', mysqlrow);
lastId = mysqlrow['id'];
}
offset += 1000;
}
if (lastId) {
setLastIdForNextRun(lastId);
}
精彩评论