数据库同步-本地和联机,无需复制

时间:2018-08-15 03:05:40

标签: c# php database mysql-8.0

因此,我有一个code,它通过比较online database的{​​{1}}来检查是否向rows添加了新数据。如果找到新数据,它将新数据插入online and local database

local database

我的代码:

public class Reservation
{
    public string res_no { get; set; }
    public string mem_fname { get; set; }
}

所以我的问题是这段代码可能会发生任何问题吗,它可以正常工作,但是有什么方法可以改善它。我知道MySQL复制会更好,但我只使用很少的MySQL特权的​​免费private async void updateDineList() { DBconnector.OpenConnection(); //Gets data from online database HttpClient client = new HttpClient(); var response = await client.GetStringAsync("http://example.com/Reservation/view_pending_reservation"); var persons = JsonConvert.DeserializeObject<List<Reservation>>(response); //Gets data from Local database string string_reservation = "SELECT res_no,mem_fname FROM res_no WHERE res_status='pending';"; DataTable reservation_table = new DataTable(); MySqlDataAdapter adapter_reservartion = new MySqlDataAdapter(string_reservation, DBconnector.Connection); adapter_reservartion.Fill(reservation_table); //Gets the row of each table int local = reservation_table.Rows.Count; int online = persons.Count; //Compares rows of online and local database if (local < online) { //if the rows of online database is greater than local database //inserts the new data from local database string Command_membership = "INSERT INTO reservation_details (res_no,mem_fname) VALUES (@res_no, @mem_fname);"; for (int i = local; i < online; i++) { //inserts new data from online to local database using (MySqlCommand myCmd = new MySqlCommand(Command_membership, DBconnector.Connection)) { myCmd.CommandType = CommandType.Text; myCmd.Parameters.AddWithValue("@res_no", persons[i].res_no); myCmd.Parameters.AddWithValue("@mem_fname", persons[i].mem_fname); myCmd.ExecuteNonQuery(); } } MessageBox.Show("New Records Found"); } else { MessageBox.Show("No new Records"); } DBconnector.Connection.Close(); }

1 个答案:

答案 0 :(得分:1)

明显的改进是不为每一行创建一个新命令。您应该创建一次命令和参数,然后设置参数并为每一行调用,或者最好将更新集打包为单个结构(例如和xml字符串),然后通过存储将整个批次传递给数据库过程调用。

其他可能有问题的问题是,您纯粹是基于行数进行检查。不知道这在您的情况下是否有效,但这听起来很危险。如果删除行怎么办?或者在您的情况下是不可能的其他一些检查最新更新的方法可能更可取。

我所能看到的没有更多上下文。