how to avoid duplicates in remote db while syncing with local database in c#





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I tried the code and failed because of the duplication of rows in remote database, while inserting from local database.



using System.Data.SQLite;
using System.Net.NetworkInformation;
using MySql.Data.MySqlClient;
namespace super
{
public partial class neworupload : Form
{
SQLiteConnection con = new SQLiteConnection(@"connection string");
MySqlConnection conn;
String connstring;
public neworupload()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool connection = NetworkInterface.GetIsNetworkAvailable();
if (connection == true)
{
connstring = "SERVER=your ip;PORT=port;DATABASE=dbname;UID=userid;PASSWORD=password;SslMode = none;";
try
{
con.Open();
MessageBox.Show("Connection success");
String qu = "select * from users ";
SQLiteCommand cmd = new SQLiteCommand(qu, con);
SQLiteDataReader data = cmd.ExecuteReader();
while (data.Read())
{
String a = data["iduser"].ToString();
String b = data["name"].ToString();
String c = data["type"].ToString();
String d = data["email"].ToString();
String f = data["hash"].ToString();
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
Console.WriteLine(f);
conn = new MySqlConnection();
conn.ConnectionString = connstring;
conn.Open();
String qudemo = "select iduser from users";
Console.WriteLine("dddd" + qudemo);
String qu1 = "INSERT INTO users(iduser,name,type,email,hash) Values(@iduser,@name,@type,@email,@hash)";
MySqlCommand cmd1 = new MySqlCommand(qu1, conn);
cmd1.Parameters.AddWithValue("@iduser", a);
cmd1.Parameters.AddWithValue("@name", b);
cmd1.Parameters.AddWithValue("@type", c);
cmd1.Parameters.AddWithValue("@email", d);
cmd1.Parameters.AddWithValue("@hash", f);
cmd1.ExecuteNonQuery();
conn.Close();
}
con.Close();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Console.WriteLine(ex);
}
}
else
{
MessageBox.Show("Check connectivity and try again!");
}
}
}
}


Here local database contain 7 rows and remote database contain 5 rows. The first 5 rows in local and remote database are the same. So while we inserting query failed with duplication of rows. Actually the extra 2 rows must be inserted to the remote database. How to avoid this problem?










share|improve this question























  • How do you know if the rows are duplicate ? Is there a primary / unique key which can be used to identify the duplicates ?

    – Chowkidar Madhur Bhaiya
    Nov 22 '18 at 4:33











  • You can use insert ignore; replace into etc. Please refer this tutorial: tutorialspoint.com/mysql/mysql-handling-duplicates.htm and official documentation at: dev.mysql.com/doc/refman/5.5/en/insert.html Following answer may also help: stackoverflow.com/a/548570/2469308

    – Chowkidar Madhur Bhaiya
    Nov 22 '18 at 4:36


















0















I tried the code and failed because of the duplication of rows in remote database, while inserting from local database.



using System.Data.SQLite;
using System.Net.NetworkInformation;
using MySql.Data.MySqlClient;
namespace super
{
public partial class neworupload : Form
{
SQLiteConnection con = new SQLiteConnection(@"connection string");
MySqlConnection conn;
String connstring;
public neworupload()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool connection = NetworkInterface.GetIsNetworkAvailable();
if (connection == true)
{
connstring = "SERVER=your ip;PORT=port;DATABASE=dbname;UID=userid;PASSWORD=password;SslMode = none;";
try
{
con.Open();
MessageBox.Show("Connection success");
String qu = "select * from users ";
SQLiteCommand cmd = new SQLiteCommand(qu, con);
SQLiteDataReader data = cmd.ExecuteReader();
while (data.Read())
{
String a = data["iduser"].ToString();
String b = data["name"].ToString();
String c = data["type"].ToString();
String d = data["email"].ToString();
String f = data["hash"].ToString();
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
Console.WriteLine(f);
conn = new MySqlConnection();
conn.ConnectionString = connstring;
conn.Open();
String qudemo = "select iduser from users";
Console.WriteLine("dddd" + qudemo);
String qu1 = "INSERT INTO users(iduser,name,type,email,hash) Values(@iduser,@name,@type,@email,@hash)";
MySqlCommand cmd1 = new MySqlCommand(qu1, conn);
cmd1.Parameters.AddWithValue("@iduser", a);
cmd1.Parameters.AddWithValue("@name", b);
cmd1.Parameters.AddWithValue("@type", c);
cmd1.Parameters.AddWithValue("@email", d);
cmd1.Parameters.AddWithValue("@hash", f);
cmd1.ExecuteNonQuery();
conn.Close();
}
con.Close();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Console.WriteLine(ex);
}
}
else
{
MessageBox.Show("Check connectivity and try again!");
}
}
}
}


Here local database contain 7 rows and remote database contain 5 rows. The first 5 rows in local and remote database are the same. So while we inserting query failed with duplication of rows. Actually the extra 2 rows must be inserted to the remote database. How to avoid this problem?










share|improve this question























  • How do you know if the rows are duplicate ? Is there a primary / unique key which can be used to identify the duplicates ?

    – Chowkidar Madhur Bhaiya
    Nov 22 '18 at 4:33











  • You can use insert ignore; replace into etc. Please refer this tutorial: tutorialspoint.com/mysql/mysql-handling-duplicates.htm and official documentation at: dev.mysql.com/doc/refman/5.5/en/insert.html Following answer may also help: stackoverflow.com/a/548570/2469308

    – Chowkidar Madhur Bhaiya
    Nov 22 '18 at 4:36














0












0








0








I tried the code and failed because of the duplication of rows in remote database, while inserting from local database.



using System.Data.SQLite;
using System.Net.NetworkInformation;
using MySql.Data.MySqlClient;
namespace super
{
public partial class neworupload : Form
{
SQLiteConnection con = new SQLiteConnection(@"connection string");
MySqlConnection conn;
String connstring;
public neworupload()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool connection = NetworkInterface.GetIsNetworkAvailable();
if (connection == true)
{
connstring = "SERVER=your ip;PORT=port;DATABASE=dbname;UID=userid;PASSWORD=password;SslMode = none;";
try
{
con.Open();
MessageBox.Show("Connection success");
String qu = "select * from users ";
SQLiteCommand cmd = new SQLiteCommand(qu, con);
SQLiteDataReader data = cmd.ExecuteReader();
while (data.Read())
{
String a = data["iduser"].ToString();
String b = data["name"].ToString();
String c = data["type"].ToString();
String d = data["email"].ToString();
String f = data["hash"].ToString();
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
Console.WriteLine(f);
conn = new MySqlConnection();
conn.ConnectionString = connstring;
conn.Open();
String qudemo = "select iduser from users";
Console.WriteLine("dddd" + qudemo);
String qu1 = "INSERT INTO users(iduser,name,type,email,hash) Values(@iduser,@name,@type,@email,@hash)";
MySqlCommand cmd1 = new MySqlCommand(qu1, conn);
cmd1.Parameters.AddWithValue("@iduser", a);
cmd1.Parameters.AddWithValue("@name", b);
cmd1.Parameters.AddWithValue("@type", c);
cmd1.Parameters.AddWithValue("@email", d);
cmd1.Parameters.AddWithValue("@hash", f);
cmd1.ExecuteNonQuery();
conn.Close();
}
con.Close();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Console.WriteLine(ex);
}
}
else
{
MessageBox.Show("Check connectivity and try again!");
}
}
}
}


Here local database contain 7 rows and remote database contain 5 rows. The first 5 rows in local and remote database are the same. So while we inserting query failed with duplication of rows. Actually the extra 2 rows must be inserted to the remote database. How to avoid this problem?










share|improve this question














I tried the code and failed because of the duplication of rows in remote database, while inserting from local database.



using System.Data.SQLite;
using System.Net.NetworkInformation;
using MySql.Data.MySqlClient;
namespace super
{
public partial class neworupload : Form
{
SQLiteConnection con = new SQLiteConnection(@"connection string");
MySqlConnection conn;
String connstring;
public neworupload()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool connection = NetworkInterface.GetIsNetworkAvailable();
if (connection == true)
{
connstring = "SERVER=your ip;PORT=port;DATABASE=dbname;UID=userid;PASSWORD=password;SslMode = none;";
try
{
con.Open();
MessageBox.Show("Connection success");
String qu = "select * from users ";
SQLiteCommand cmd = new SQLiteCommand(qu, con);
SQLiteDataReader data = cmd.ExecuteReader();
while (data.Read())
{
String a = data["iduser"].ToString();
String b = data["name"].ToString();
String c = data["type"].ToString();
String d = data["email"].ToString();
String f = data["hash"].ToString();
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
Console.WriteLine(f);
conn = new MySqlConnection();
conn.ConnectionString = connstring;
conn.Open();
String qudemo = "select iduser from users";
Console.WriteLine("dddd" + qudemo);
String qu1 = "INSERT INTO users(iduser,name,type,email,hash) Values(@iduser,@name,@type,@email,@hash)";
MySqlCommand cmd1 = new MySqlCommand(qu1, conn);
cmd1.Parameters.AddWithValue("@iduser", a);
cmd1.Parameters.AddWithValue("@name", b);
cmd1.Parameters.AddWithValue("@type", c);
cmd1.Parameters.AddWithValue("@email", d);
cmd1.Parameters.AddWithValue("@hash", f);
cmd1.ExecuteNonQuery();
conn.Close();
}
con.Close();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Console.WriteLine(ex);
}
}
else
{
MessageBox.Show("Check connectivity and try again!");
}
}
}
}


Here local database contain 7 rows and remote database contain 5 rows. The first 5 rows in local and remote database are the same. So while we inserting query failed with duplication of rows. Actually the extra 2 rows must be inserted to the remote database. How to avoid this problem?







c# mysql sqlite






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 4:31









priyanka spriyanka s

5618




5618













  • How do you know if the rows are duplicate ? Is there a primary / unique key which can be used to identify the duplicates ?

    – Chowkidar Madhur Bhaiya
    Nov 22 '18 at 4:33











  • You can use insert ignore; replace into etc. Please refer this tutorial: tutorialspoint.com/mysql/mysql-handling-duplicates.htm and official documentation at: dev.mysql.com/doc/refman/5.5/en/insert.html Following answer may also help: stackoverflow.com/a/548570/2469308

    – Chowkidar Madhur Bhaiya
    Nov 22 '18 at 4:36



















  • How do you know if the rows are duplicate ? Is there a primary / unique key which can be used to identify the duplicates ?

    – Chowkidar Madhur Bhaiya
    Nov 22 '18 at 4:33











  • You can use insert ignore; replace into etc. Please refer this tutorial: tutorialspoint.com/mysql/mysql-handling-duplicates.htm and official documentation at: dev.mysql.com/doc/refman/5.5/en/insert.html Following answer may also help: stackoverflow.com/a/548570/2469308

    – Chowkidar Madhur Bhaiya
    Nov 22 '18 at 4:36

















How do you know if the rows are duplicate ? Is there a primary / unique key which can be used to identify the duplicates ?

– Chowkidar Madhur Bhaiya
Nov 22 '18 at 4:33





How do you know if the rows are duplicate ? Is there a primary / unique key which can be used to identify the duplicates ?

– Chowkidar Madhur Bhaiya
Nov 22 '18 at 4:33













You can use insert ignore; replace into etc. Please refer this tutorial: tutorialspoint.com/mysql/mysql-handling-duplicates.htm and official documentation at: dev.mysql.com/doc/refman/5.5/en/insert.html Following answer may also help: stackoverflow.com/a/548570/2469308

– Chowkidar Madhur Bhaiya
Nov 22 '18 at 4:36





You can use insert ignore; replace into etc. Please refer this tutorial: tutorialspoint.com/mysql/mysql-handling-duplicates.htm and official documentation at: dev.mysql.com/doc/refman/5.5/en/insert.html Following answer may also help: stackoverflow.com/a/548570/2469308

– Chowkidar Madhur Bhaiya
Nov 22 '18 at 4:36












1 Answer
1






active

oldest

votes


















0














Store last saved User ID in your local db and when you again go to remote db to sync your data, put this where clause in your query where RemoteDB.UserID > local.LastStoredUserID. So it will only fetch new records...






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423929%2fhow-to-avoid-duplicates-in-remote-db-while-syncing-with-local-database-in-c-shar%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Store last saved User ID in your local db and when you again go to remote db to sync your data, put this where clause in your query where RemoteDB.UserID > local.LastStoredUserID. So it will only fetch new records...






    share|improve this answer




























      0














      Store last saved User ID in your local db and when you again go to remote db to sync your data, put this where clause in your query where RemoteDB.UserID > local.LastStoredUserID. So it will only fetch new records...






      share|improve this answer


























        0












        0








        0







        Store last saved User ID in your local db and when you again go to remote db to sync your data, put this where clause in your query where RemoteDB.UserID > local.LastStoredUserID. So it will only fetch new records...






        share|improve this answer













        Store last saved User ID in your local db and when you again go to remote db to sync your data, put this where clause in your query where RemoteDB.UserID > local.LastStoredUserID. So it will only fetch new records...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 7:10









        Aamir NakhwaAamir Nakhwa

        1299




        1299
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423929%2fhow-to-avoid-duplicates-in-remote-db-while-syncing-with-local-database-in-c-shar%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)