Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
With using C# and MySQL, I am creating an project. There is databases from local and remote also. Firstly the records are inserted or updated to the local database then the new records(newly inserted to the local or changed records in local) should be uploaded to the remote database. Here I did all of these. But every process are separately. My problem is how can I do all of these as one.

See the image [^]

What I have tried:

C# coding
Display button


private void btnDisplay_Click(object sender, EventArgs e)
        {
            try
            {
                _dbConnection.Open();
                const string selectQuery =
                    "SELECT * FROM tbl_sales WHERE (row_inserted_on >= @dtp_last_updated) AND (last_edited_on >= @dtp_last_updated)";

                using (var cmdLocal = new MySqlCommand(selectQuery, _dbConnection))
                {
                    cmdLocal.Parameters.Add("@dtp_last_updated", MySqlDbType.DateTime).Value =
                        DateTime.Parse(dtpLastServerUpdated.Text);
                    cmdLocal.Connection = _dbConnection;
                    cmdLocal.CommandText = selectQuery;
                    _dbDataAdapter = new MySqlDataAdapter();
                    _dbDataAdapter.SelectCommand = cmdLocal;
                    _dbDataTable = new DataTable();
                    _dbDataAdapter.Fill(_dbDataTable);
                    dataGridView1.DataSource = _dbDataTable;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                _dbDataAdapter.Dispose();
                _dbConnection.Close();
            }
        }


Expoert to JSon button
private void btnExportToJson_Click(object sender, EventArgs e)
        {
            var jasonData = (DataTableToJson(_dbDataTable));
            //MessageBox.Show(afd);
            System.IO.File.WriteAllText(@"C:\Users\BAALAN-PC\Desktop\path.json", jasonData);
            Application.Exit();
        }


Upload to Server button
private void btnHttpRequest_Click(object sender, EventArgs e)
        {
            try
            {
                WebClient client = new WebClient();
                string myFile = @"C:\Users\BAALAN-PC\Desktop\path.json";
                client.Credentials = CredentialCache.DefaultCredentials;
                client.UploadFile(@"http://myWeb.com/myWorkDb/fileUpload.php", "POST", myFile);
                client.Dispose();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }


PHP coding
phpExecute.php (loads the data from JSon file to remote database)
<?php
    try
    {
        $connect = mysqli_connect("localhost", "fiart", "", "dbsync");    
        $query = '';
        $table_data = '';
        $filename = "path.json";

        $data = file_get_contents($filename);
        $array = json_decode($data, true); 

        foreach($array as $row) 
        {
            $query .= "INSERT INTO tbl_sales(sale_item, sale_qty, row_inserted_on, last_edited_on, id) VALUES ('".$row["sale_item"]."', '".$row["sale_qty"]."', '".$row["row_inserted_on"]."', '".$row["last_edited_on"]."', '".$row["id"]."') ON DUPLICATE KEY UPDATE sale_item='".$row["sale_item"]."', sale_qty='".$row["sale_qty"]."', row_inserted_on='".$row["row_inserted_on"]."', last_edited_on='".$row["last_edited_on"]."';";
        }

            mysqli_multi_query($connect, $query);  

            echo "<h1>All data appeded </h1>";
    } 

    catch(Exception $e)
    {   
        echo $e->getMessage();    
    }
?>


fileUpload.php (Used with Upload to server button)
<?php
    $filepath = $_FILES["file"]["tmp_name"];
    move_uploaded_file($filepath,"path.json");
?>
Posted
Updated 21-May-18 19:54pm

1 solution

C#
const string selectQuery = "SELECT * FROM tbl_sales WHERE (row_inserted_on >= @dtp_last_updated) AND (last_edited_on >= @dtp_last_updated)";

C#
$query .= "INSERT INTO tbl_sales(sale_item, sale_qty, row_inserted_on, last_edited_on, id) VALUES ('".$row["sale_item"]."', '".$row["sale_qty"]."', '".$row["row_inserted_on"]."', '".$row["last_edited_on"]."', '".$row["id"]."') ON DUPLICATE KEY UPDATE sale_item='".$row["sale_item"]."', sale_qty='".$row["sale_qty"]."', row_inserted_on='".$row["row_inserted_on"]."', last_edited_on='".$row["last_edited_on"]."';";

SQL injection: Looks like you know the problem and the cure Why still use unprotected code ?
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
v2
Comments
S.SAKTHYBAALAN 22-May-18 2:12am    
Thank you, @ppolymorphe. How can I change this ?
Patrice T 22-May-18 2:15am    
Read links at the end of solution.
The 'select' is an example of the solution.
S.SAKTHYBAALAN 22-May-18 3:11am    
So, where should I want to change?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900