search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

結合C#+PHP+MYSQL+WGET 模擬將硬體訊號上傳至雲端資料庫紀錄 範例 – jashliao部落格

結合C#+PHP+MYSQL+WGET 模擬將硬體訊號上傳至雲端資料庫紀錄 範例

 

PHP

/*****************************
*数据库连接

conn.php
*****************************/
$conn = @mysql_connect(“localhost”,”root”,”usbw”);
if (!$conn){
    die(“資料庫連接失敗:” . mysql_error());
}
mysql_select_db(“smart_firefighting”, $conn);
mysql_query(“set character set ‘utf8′”);
mysql_query(‘set names utf8’);
?>

/*****************************
insert_table.php
*****************************/

header(‘content-type:text/html;charset=utf-8’);
$device_id=””;
$value=””;
$years=””;
$months=””;
$days=””;
$hours=””;
$minutes=””;
$seconds=””;

if(isset($_POST[‘device_id’]))
{
    include(‘conn.php’);
    $device_id=$_POST[‘device_id’];
    $value=$_POST[‘value’];
    $years=$_POST[‘years’];
    $months=$_POST[‘months’];
    $days=$_POST[‘days’];
    $hours=$_POST[‘hours’];
    $minutes=$_POST[‘minutes’];
    $seconds=$_POST[‘seconds’];
    $sql = “INSERT INTO all_data(device_id,value,years,months,days,hours,minutes,seconds)VALUES(‘$device_id’,’$value’,’$years’,’$months’,’$days’,’$hours’,’$minutes’,’$seconds’)”;
    echo $sql;
    if(mysql_query($sql,$conn))
    {
        echo’INSERT Data Success’;
    }
    else
    {
        echo’INSERT Data Fail’;
    }
}
else
{
    echo’INSERT Data Fail’;
}

?>

MYSQL

— phpMyAdmin SQL Dump
— version 4.0.4.2
— http://www.phpmyadmin.net

— 主機: localhost
— 產生日期: 2016 年 06 月 05 日 09:54
— 伺服器版本: 5.6.13
— PHP 版本: 5.4.17

SET SQL_MODE = “NO_AUTO_VALUE_ON_ZERO”;
SET time_zone = “+00:00”;

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;


— 資料庫: `smart_firefighting`

CREATE DATABASE IF NOT EXISTS `smart_firefighting` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `smart_firefighting`;

— ——————————————————–


— 表的結構 `all_data`

CREATE TABLE IF NOT EXISTS `all_data` (
  `id` int(6) unsigned NOT NULL AUTO_INCREMENT,
  `device_id` varchar(30) NOT NULL,
  `value` varchar(30) NOT NULL,
  `years` int(6) DEFAULT NULL,
  `months` int(6) DEFAULT NULL,
  `days` int(6) DEFAULT NULL,
  `hours` int(6) DEFAULT NULL,
  `minutes` int(6) DEFAULT NULL,
  `seconds` int(6) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

 

C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Pause()
        {
            Console.Write(“Press any key to continue . . . “);
            Console.ReadKey(true);
        }
        static void CreateBat(String device_id, String value, String years, String months, String days, String hours, String minutes, String seconds)
        {
            String URL = “http://localhost:8080/sf/insert_table.php”;
            String StrData = String.Format(“wget –post-data=\”device_id={0}&value={1}&years={2}&months={3}&days={4}&hours={5}&minutes={6}&seconds={7}\” {8} -O log.txt”, device_id, value, years, months, days, hours, minutes, seconds, URL);
            StreamWriter sw = new StreamWriter(“wget_run.bat”);
            sw.WriteLine(StrData);// 寫入文字
            sw.Close();// 關閉串流
        }
        static void RunBat()
        {
            Process m_pro;
            String StrPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + “\\wget_run.bat”;
            Console.WriteLine(StrPath);
            m_pro = Process.Start(“wget_run.bat”);
        }
        
        static void Main(string[] args)
        {
            String device_id=”A001″;
            String value=”29.12″;
            String years=”2016″;
            String months=”06″;
            String days=”05″;
            String hours=”20″;
            String minutes=”52″;
            String seconds=”00″;
            CreateBat(device_id, value, years, months, days, hours, minutes, seconds);
            RunBat();
            Pause();
        }
    }
}

 

 

 

 



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦