备份
工具 - 对象浏览器
打开左侧菜单
C/C++ Code复制内容到剪贴板
- String UserId = "sa"; //sqlserver 用户名
- String UserPwd = "sa"; // sqlserver 密码
- String Database = "xuante"; // 数据库名
- String ComputerName = "DESKTOP-RGLDQK0\\SQLEXPRESS";
- //String connsql = "Persist Security Info=True;User ID=" + UserId + ";Password=" + UserPwd + ";Initial Catalog=" + Database + ";Data Source=" + ComputerName;
- String connsql = "server=" + ComputerName + ";database=" + Database + ";integrated security=SSPI"; // 数据库连接字符串,database设置为自己的数据库名,以Windows身份验证
- try
- {
- using (SqlConnection conn = new SqlConnection())
- {
- conn.ConnectionString = connsql;
- // conn.Open(); // 打开数据库连接
- String sql = "select * from UserInfo"; // 查询语句
- // 实例化适配器
- using (SqlDataAdapter adapter = new SqlDataAdapter(sql, conn))
- {
- DataTable dt = new DataTable();// 实例化数据表
- //把数据库中的数据填充到内存表Dt中。
- //填充之前不需要打开数据库连接,Adapter会自动打开连接,并执行sql。
- adapter.Fill(dt);
- // dataGridView1.DataSource = dt; // 设置到DataGridView中
- // 将结果集转为json
- string dataResult = Tools.DataTableToJson(dt);
- //声明并添加元素
- Dictionary<string, string> PostData = new Dictionary<string, string>();
- String URL = "http://47.89.12.4:9507/";
- PostData.Add("data", dataResult);
- string result = Tools.HttpRequest(URL, PostData, "post");
- JavaScriptSerializer json = new JavaScriptSerializer();
- string para = json.Serialize(result); //转json字符串
- SynchronousParkData ret = json.Deserialize<SynchronousParkData>(result); //解析json字符串
- if (ret.errcode == 0)
- {
- string tips = "同步成功..." + "\n";
- // 将textBox1的内容插入到第一行
- // 索引0是 richText1 第一行位置
- richTextBox1.Text = richTextBox1.Text.Insert(0, tips);
- // MessageBox.Show("同步成功");
- }
- else
- {
- MessageBox.Show("同步失败");
- }
- //Console.Write(result);
- // var id = dt.Rows[0][1]; //第一行第一列的值
- }
- // conn.Close(); // 关闭数据库连接
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("错误信息:" + ex.Message, "出现错误");
- }
C/C++ Code复制内容到剪贴板
- DataSet ds = new DataSet();
- // ds.Tables[0]就是dataTable
C/C++ Code复制内容到剪贴板
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace WindowsFormsApplication1
- {
- class SynchronousGetParkData
- {
- public int errcode;
- public string errmsg;
- public listDetai[] data; // 数组处理
- // 构造函数
- public SynchronousGetParkData()
- {
- errcode = 0;
- errmsg = null;
- }
- public struct listDetai
- {
- public int id { get; set; }
- public string car_no { get; set; }
- public string card_no { get; set; }
- public string owner_name { get; set; } // 车主称呼
- public string tel { get; set; } // 联系电话
- public string addr { get; set; }
- public string car_name { get; set; } // 车牌号
- // 状态,1是启用,2是弃用
- public int status {
- get {
- if (status == 0)
- {
- return 1;
- }
- else
- {
- return status;
- }
- }
- set {
- status = value;
- }
- } // 是否启用,状态
- };
- }
- }
C/C++ Code复制内容到剪贴板
- using System;
- using System.Windows.Forms; // DataGridView控件类的命名空间
- using System.Web.Script.Serialization;
- using System.Collections.Generic;
- using System.Data; //DataSet类的命名空间
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- System.Timers.Timer parkSyncTime = new System.Timers.Timer(3000); //设置停车场数据同步时间间隔为60秒 * 5
- // 构造函数
- public Form1()
- {
- InitializeComponent();
- }
- // 初始化
- private void Form1_Load(object sender, EventArgs e)
- {
- Control.CheckForIllegalCrossThreadCalls = false;
- }
- // 【铵钮】停车场开始同步数据库 按钮点击事件
- private void parkTask_Click(object sender, EventArgs e)
- {
- /* 出入场数据同步发送 */
- parkSyncTime.Enabled = true;
- parkSyncTime.Elapsed += new System.Timers.ElapsedEventHandler(startParkTask);
- parkSyncTime.AutoReset = true; //每到指定时间Elapsed事件是触发一次(false),还是一直触发(true)
- /* 发送成功 */
- }
- // 【铵钮】暂停停车场同步任务
- private void stopParkTask_Click(object sender, EventArgs e)
- {
- parkSyncTime.Stop();
- string tips = DateTime.Now.ToString() + " - 暂停停车场数据同步" + "\n";
- string newLine = tips + Environment.NewLine;
- // 将textBox1的内容插入到第一行
- richTextBox1.Text = richTextBox1.Text.Insert(0, tips + Environment.NewLine);
- }
- //************************************************************************ 执行发送当前数据库的数据到远程服务器,再执行获取远程服务器的放行车辆数据insert到库中
- // 开始停车场同步任务 -> 获取数据库里的以json形式发送给指定接口
- private void startParkTask(object sender, EventArgs e)
- {
- string sql = "select * from UserInfo"; // 查询语句
- string dataResult = SynchronousParkData.QueryToJson(sql);
- //声明并添加元素
- Dictionary<string, string> PostData = new Dictionary<string, string>();
- String URL = "http://47.89.12.4:9507/";
- PostData.Add("data", dataResult);
- // System.Diagnostics.Debug.WriteLine(result);
- string result = Tools.HttpRequest(URL, PostData, "post");
- if (string.IsNullOrEmpty(result))
- {
- // 如果result为空或null
- string tips = DateTime.Now.ToString() + " 网络连接失败或服务器没有响应,请检查网络" + "\n";
- // 将textBox1的内容插入到第一行
- richTextBox1.Text = richTextBox1.Text.Insert(0, tips + Environment.NewLine);
- parkSyncTime.Stop();
- return;
- }
- //System.Diagnostics.Debug.WriteLine(result);
- JavaScriptSerializer json = new JavaScriptSerializer();
- SynchronousParkData ret = json.Deserialize<SynchronousParkData>(result); //解析json字符串
- if (ret.errcode == 0)
- {
- string tips = DateTime.Now.ToString() + " - 同步成功" + "\n";
- // 将textBox1的内容插入到第一行
- richTextBox1.Text = richTextBox1.Text.Insert(0, tips + Environment.NewLine);
- }
- else
- {
- string tips = DateTime.Now.ToString() + " - 同步数据失败,请联系管理员 => " + ret.errmsg + "\n";
- richTextBox1.Text = richTextBox1.Text.Insert(0, tips + Environment.NewLine);
- }
- // 同步放行车辆
- this.getParkInfo();
- }
- // 获取通行车辆信息同步至数据库
- private void getParkInfo()
- {
- //声明并添加元素
- Dictionary<string, string> PostData = new Dictionary<string, string>();
- String URL = "http://47.89.12.4:9507/";
- PostData.Add("act", "licensePlate");
- string result = Tools.HttpRequest(URL, PostData, "get");
- JavaScriptSerializer json = new JavaScriptSerializer();
- SynchronousGetParkData ret = json.Deserialize<SynchronousGetParkData>(result); //解析json字符串
- if (ret.errcode == 0)
- {
- string tips = DateTime.Now.ToString() + " - 放行车辆数据同步成功" + "\n";
- // 将textbox1的内容插入到第一行
- richTextBox1.Text = richTextBox1.Text.Insert(0, tips + Environment.NewLine);
- //for循环遍历
- // Array data = ret.data;
- SynchronousGetParkData.listDetai[] parkData = ret.data; // 声明
- for (int i=0; i<parkData.Length; i++)
- {
- //string sql = "select count(*) from Carinfo where CarName = '浙CFG093'";
- //string queyrRes = SynchronousParkData.QueryToJson(sql);
- int res = 0;
- string sence = "";
- bool carId = SynchronousParkData.dataExist("Carinfo", "CarName", parkData[i].car_name); // 车牌号是否已存在 parkData[i].car_name
- if (carId)
- {
- // 如果存在,更新
- Dictionary<string, string> setDict = new Dictionary<string, string>();
- setDict.Add("CarNo", parkData[i].card_no);
- setDict.Add("OwnerName", parkData[i].owner_name);
- setDict.Add("UseState", parkData[i].status.ToString());
- setDict.Add("Tel", parkData[i].tel);
- setDict.Add("Addr", parkData[i].addr);
- Dictionary<string, string> whereDict = new Dictionary<string, string>();
- whereDict.Add("CarName", parkData[i].car_name);
- res = SynchronousParkData.UpdateDictQuery("Carinfo", setDict, whereDict);
- sence = "更新";
- }
- else
- {
- // CarNo => 车号 CardNO => 卡片内码 OwnerName => 车主姓名 UseState => 使用状态 CarName => 车牌号 CarType => 车辆类型
- Dictionary<string, string> dataDict = new Dictionary<string, string>();
- dataDict.Add("CarNo", parkData[i].id.ToString());
- dataDict.Add("CardNO", "");
- dataDict.Add("UserType", parkData[i].roe_name);
- dataDict.Add("OwnerName", parkData[i].owner_name);
- dataDict.Add("UseState", parkData[i].status.ToString());
- dataDict.Add("Tel", parkData[i].tel);
- dataDict.Add("Addr", parkData[i].addr);
- dataDict.Add("CarName", parkData[i].car_name);
- res = SynchronousParkData.AddDictQuery("Carinfo", dataDict);
- sence = "写入";
- }
- if (res < 1)
- {
- tips = DateTime.Now.ToString() + " - 车牌号:" + parkData[i].car_name + "车主:" + parkData[i].owner_name + " 放行车辆 "+ sence +" 失败" + "\n";
- // 将textbox1的内容插入到第一行
- richTextBox1.Text = richTextBox1.Text.Insert(0, tips + Environment.NewLine);
- }
- }
- }
- else {
- string tips = DateTime.Now.ToString() + " - 放行车辆数据同步失败,请联系管理员 => " + ret.errmsg + "\n";
- richTextBox1.Text = richTextBox1.Text.Insert(0, tips + Environment.NewLine);
- }
- }
- private void richTextBox1_TextChanged(object sender, EventArgs e)
- {
- }
- }
- }
C/C++ Code复制内容到剪贴板
- // 本地到云端,人员信息同步
- private void localToServer_Click(object sender, EventArgs e)
- {
- string sql = "select * from UserInfo"; // 查询本地人员信息
- DataSet dataResult = SynchronousParkData.Query(sql);
- DataTable dt = dataResult.Tables[0];
- // 组合人员信息数组,发送给服务器
- String URL = "http://47.89.12.4:9507/";
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- Dictionary<string, string> PostData = new Dictionary<string, string>();
- PostData.Add("UID", dt.Rows[i]["UID"].ToString());
- PostData.Add("UserName", dt.Rows[i]["UserName"].ToString());
- PostData.Add("Remark", dt.Rows[i]["Remark"].ToString());
- //PostData.Add("data", dataResult2);
- // System.Diagnostics.Debug.WriteLine(result);
- string result = Tools.HttpRequest(URL, PostData, "post");
- JavaScriptSerializer json = new JavaScriptSerializer();
- SynchronousGetParkData ret = json.Deserialize<SynchronousGetParkData>(result); //解析json字符串
- if (ret.errcode == 0)
- {
- string tips = DateTime.Now.ToString() + " - 同步本机人员信息:" + dt.Rows[i]["UserName"].ToString() + " => 成功" + "\n";
- // 将textbox1的内容插入到第一行
- richTextBox1.Text = richTextBox1.Text.Insert(0, tips + Environment.NewLine);
- }
- else
- {
- string tips = DateTime.Now.ToString() + " - 同步本机人员信息:" + dt.Rows[i]["UserName"].ToString() + " => 失败" + "\n";
- // 将textbox1的内容插入到第一行
- richTextBox1.Text = richTextBox1.Text.Insert(0, tips + Environment.NewLine);
- }
- }
- return;
- }