目录
一 设计原型
二 后台源码
一 设计原型
二 后台源码
using SqlSugar;namespace 自动化上位机设计
{public partial class Form1 : Form{SqlHelper sqlHelper = new SqlHelper();SqlSugarClient dbContent = null;bool IsRun = false;int Count = 0;public Form1(){InitializeComponent();dbContent = sqlHelper.db;timer1.Enabled = true;timer1.Tick += Timer1_Tick;}private void Timer1_Tick(object? sender, EventArgs e){this.Invoke(() =>{uiLedDisplay1.Text = DateTime.Now.ToString("HH:mm:ss");});}private void Form1_Load(object sender, EventArgs e){sqlHelper.CreateTable<EntryClass>();//开始批量插入数据Task.Run(() => { AddData(); });}private void AddData(){while (true){if (!IsRun) continue;EntryClass ec = new EntryClass();ec.Len = new Random().Next(200, 300).ToString();ec.Width = new Random().Next(40, 300).ToString();ec.Hight = new Random().Next(150, 250).ToString();ec.R = new Random().Next(10, 360).ToString();ec.Weit = new Random().Next(20, 30).ToString();ec.ZY = new Random().Next(50, 90).ToString();ec.DC = new Random().Next(40, 300).ToString();ec.DR = new Random().Next(400, 600).ToString();ec.YL = new Random().Next(28, 88).ToString();ec.WD = new Random().Next(89, 900).ToString();ec.SD = new Random().Next(234, 454).ToString();ec.LJ = new Random().Next(67, 89).ToString();//数据放入数据库中dbContent.Insertable(ec).ExecuteCommand();this.Invoke(() =>{//同步展示参数CD.Text = ec.Len;KD.Text = ec.Width;GD.Text = ec.Hight;JD.Text = ec.R;ZL.Text = ec.Weit;ZY.Text = ec.ZY;DY.Text = ec.DC;NZ.Text = ec.DR;PRESS.Text = ec.YL;CC.Text = ec.WD;SD.Text = ec.SD;LJJ.Text = ec.LJ;//参数展示到集合中Data.Rows.Add(ec.Len, ec.Width, ec.Hight, ec.R, ec.Weit, ec.ZY, ec.DC, ec.DR, ec.YL, ec.WD, ec.SD, ec.LJ, DateTime.Now.ToShortTimeString(), "OK", "测试数据");//计数Count++;uiLedDisplay2.Text = Count.ToString();//打印日志RunLog.AppendText("设备运行中,数据已加入到数据库\r\n");ErorrLog.AppendText("设备运行中,数据已加入到数据库\r\n");Log.AppendText("设备运行中,数据已加入到数据库\r\n");});Thread.Sleep(2000);}}private void uiSwitch1_ValueChanged(object sender, bool value){if (uiSwitch1.Active){uiLight1.OnColor = Color.Green;IsRun = true;RunLog.AppendText("设备启动。。。\r\n");ErorrLog.AppendText("设备启动。。。\r\n");Log.AppendText("设备启动。。。\r\n");}else{uiLight1.OnColor = Color.Red;IsRun = false;RunLog.AppendText("设备停止。。。\r\n");ErorrLog.AppendText("设备停止。。。\r\n");Log.AppendText("设备停止。。。\r\n");}}}
}
数据库实体类:
using SqlSugar;namespace 自动化上位机设计
{//实体与数据库结构一样//数据是自增需要加上IsIdentity//数据库是主键需要加上IsPrimaryKey//注意:要完全和数据库一致2个属性[SugarTable("EntryClass")]public class EntryClass{public string Len { get; set; }public string Width { get; set; }public string Hight { get; set; }public string R { get; set; }public string Weit { get; set; }public string ZY { get; set; }public string DC { get; set; }public string DR { get; set; }public string YL { get; set; }public string WD { get; set; }public string SD { get; set; }public string LJ { get; set; }}
}