如何从C#运行两个PHP脚本 [英] How can I run two PHP scripts from C#

查看:161
本文介绍了如何从C#运行两个PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#和MySQL,我正在创建一个项目。还有来自本地和远程的数据库。首先将记录插入或更新到本地数据库,然后将新记录(新插入本地或本地更改的记录)上载到远程数据库。在这里,我做了所有这些。但每个过程都是分开的。我的问题是如何将所有这些作为一个完成。



查看图像 [ ^ ]



我尝试过:



C#编码

显示按钮




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到JSon按钮



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编码

phpExecute.php(将数据从JSon文件加载到远程数据库)



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(与上传到服务器按钮一起使用)



fileUpload.php (Used with Upload to server button)

<?php
    $filepath = $_FILES["file"]["tmp_name"];
    move_uploaded_file($filepath,"path.json");
?>

推荐答案

connect = mysqli_connect(localhost,fiart,,dbsync) ;
connect = mysqli_connect("localhost", "fiart", "", "dbsync");


query ='';
query = '';


table_data ='';
table_data = '';


这篇关于如何从C#运行两个PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆