上位机是指可以直接发出操控命令的计算机,一般是PC/host computer/master computer/upper computer,屏幕上显示各种信号变化(液压,水位,温度等)。下位机是直接控制设备获取设备状况的计算机,一般是PLC/单片机single chip microcomputer/slave computer/lower computer之类的。上位机发出的命令首先给下位机,下位机再根据此命令解释成相应时序信号直接控制相应设备。下位机不时读取设备状态数据(一般为模拟量),转换成数字信号反馈给上位机。简言之如此,实际情况千差万别,但万变不离其宗:上下位机都需要编程,都有专门的开发系统。
在概念上,控制者和提供服务者是上位机,被控制者和被服务者是下位机,也可以理解为主机和从机的关系,但上位机和下位机是可以转换的。
完成图图示:
所需要的软件:Visual studio 2022 ,SQL
最终界面布局
源代码:
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 数据库操作UI
{
public partial class Form1 : UIForm
{
private testEntities testEntities;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
testEntities = new testEntities();//代表数据库链接==》对应的数据库
List<score> scorelist = testEntities.scores.ToList();//testEntities.scores==>score表
this.uiDataGridView1.DataSource = scorelist;
this.uiDataGridView1.Columns[0].HeaderText = "学号";
this.uiDataGridView1.Columns[1].HeaderText = "姓名";
this.uiDataGridView1.Columns[2].HeaderText = "C#成绩";
this.uiDataGridView1.Columns[3].HeaderText = "uniapp成绩";
//this.FormBorderStyle = FormBorderStyle.FixedSingle; // 或 Fixed3D, FixedDialog
//this.MaximizeBox = false;
this.uiDataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // 启用列宽自动填充
this.uiSymbolButtonCancel.Click += MyDeleteRecord;
}
private void MyDeleteRecord(object sender, EventArgs e)
{
try
{
DataGridViewSelectedRowCollection selectRows = this.uiDataGridView1.SelectedRows;//获取选择的所有行
if(selectRows.Count == 0)
{
UIMessageBox.ShowInfo("请选择要删除的记录!");
return;
}
foreach (DataGridViewRow row in selectRows)
{
String number = row.Cells[0].Value.ToString();
score s1 = testEntities.scores.Find(number);
if (s1 != null)
{
testEntities.scores.Remove(s1);
}
}
testEntities.SaveChanges();//保存数据库修改
UIMessageBox.ShowSuccess("删除成功!");
//更新显示
List<score> scorelist = testEntities.scores.ToList();//testEntities.scores==>score表
this.uiDataGridView1.DataSource = scorelist;
}
catch(Exception e2)
{
UIMessageBox.ShowError("删除失败,原因:" + e2.Message);
}
}
private void UiSymbolButton1_Click(object sender, EventArgs e)
{
AddForm addForm = new AddForm(this.uiDataGridView1);
addForm.ShowDialog();
}
private void UiSymbolButtonUpdate_Click(object sender, EventArgs e)
{
DataGridViewSelectedCellCollection cells = this.uiDataGridView1.SelectedCells;
if(cells.Count ==0)
{
return;
}
DataGridViewRow row = cells[0].OwningRow;
if(row == null)
{
return;
}
try
{
String number = row.Cells[0].Value.ToString();
String name = row.Cells[1].Value.ToString();
double csharp = (double)row.Cells[2].Value;
double uniapp = (double)row.Cells[3].Value;
score s = testEntities.scores.Find(number);
s.name = name;
s.csharp = csharp;
s.uniapp = uniapp;
testEntities.SaveChanges();
UIMessageBox.ShowSuccess("修改成功!");
}
catch(Exception e2)
{
UIMessageBox.ShowError("修改失败,原因:" + e2.Message);
}
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UiAvatar1_Click(object sender, EventArgs e)
{
String text = this.uiTextBoxSearch.Text.Trim();
List<score> scorelist = testEntities.scores.Where(x => x.name.Contains(text)).ToList();
this.uiDataGridView1.DataSource = scorelist;
}
}
}