项目 6 数据存储:仿冒 SqlClient(T-SQL 转写成 SQLite)、数据库页面、DataGridView、DateTimePicker
CI/CD / test (push) Canceled after 0s
CI/CD / deploy (push) Canceled after 0s

- 每个学生一套数据库,长期保存(data/databases/<用户编号>/库名.db);学生程序运行时挂进沙箱可读写
- Sim.Data:SqlConnection / SqlCommand / SqlDataReader / SqlDataAdapter / SqlCommandBuilder / SqlParameter;
  连接字符串只按库名打开学生自己的库;ScriptDom 解析 T-SQL 后按白名单转写成 SQLite 语句,
  STRICT 表 + 元数据表还原 SQL Server 的列类型,报错编号和中文信息与 SQL Server 一致;
  INSERT 不写列名时跳过自增列,日期统一格式(书上 DateTime.ToString() 拼接的日期能正确比较)
- 常驻 SQL 服务(Sim.Runner sql-server):数据库页面的查询、对象资源管理器,评分前按实验 database.sql 建库
- 数据库页面(代替 SSMS):对象资源管理器、查询窗口、编辑前 200 行、表设计器、恢复实验初始数据
- DataGridView(绑定 DataView/DataTable/DataSet,单元格修改写回 DataTable)、DateTimePicker;设计器工具箱加入
- 评分:每个检查项一份新建的实验库;新增点 / 修改表格单元格、选日期、表格里有 / 没有某行的检查
- 示例:项目 6 任务 2、3;时间格式改成和中文 Windows 一致(9:05:03);异常堆栈不暴露服务器路径
- 测试:数据库端到端、接口(每人只看到自己的库、恢复实验数据)、评分出错情况、T-SQL 生成

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-10-23 09:18:56 +08:00
1 parent 6b088240da
commit 670abe5921
72 files changed
+7253 -51

No files matched your search

+282
View File
@@ -0,0 +1,282 @@
namespace 校园数据存储
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnDelete = new System.Windows.Forms.Button();
this.btnUpdate = new System.Windows.Forms.Button();
this.btn_save = new System.Windows.Forms.Button();
this.btn_get = new System.Windows.Forms.Button();
this.txt_humidity = new System.Windows.Forms.TextBox();
this.txt_temperature = new System.Windows.Forms.TextBox();
this.txt_light = new System.Windows.Forms.TextBox();
this.lbl_humidity = new System.Windows.Forms.Label();
this.lbl_temperature = new System.Windows.Forms.Label();
this.lbl_light = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.btn_Open = new System.Windows.Forms.Button();
this.com_port = new System.Windows.Forms.ComboBox();
this.label4 = new System.Windows.Forms.Label();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnDelete);
this.groupBox1.Controls.Add(this.btnUpdate);
this.groupBox1.Controls.Add(this.btn_save);
this.groupBox1.Controls.Add(this.btn_get);
this.groupBox1.Controls.Add(this.txt_humidity);
this.groupBox1.Controls.Add(this.txt_temperature);
this.groupBox1.Controls.Add(this.txt_light);
this.groupBox1.Controls.Add(this.lbl_humidity);
this.groupBox1.Controls.Add(this.lbl_temperature);
this.groupBox1.Controls.Add(this.lbl_light);
this.groupBox1.Location = new System.Drawing.Point(207, 11);
this.groupBox1.Margin = new System.Windows.Forms.Padding(2);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Padding = new System.Windows.Forms.Padding(2);
this.groupBox1.Size = new System.Drawing.Size(418, 180);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "数据显示";
//
// btnDelete
//
this.btnDelete.Location = new System.Drawing.Point(229, 138);
this.btnDelete.Margin = new System.Windows.Forms.Padding(2);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(72, 30);
this.btnDelete.TabIndex = 9;
this.btnDelete.Text = "删除";
this.btnDelete.UseVisualStyleBackColor = true;
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnUpdate
//
this.btnUpdate.Location = new System.Drawing.Point(106, 138);
this.btnUpdate.Margin = new System.Windows.Forms.Padding(2);
this.btnUpdate.Name = "btnUpdate";
this.btnUpdate.Size = new System.Drawing.Size(65, 30);
this.btnUpdate.TabIndex = 8;
this.btnUpdate.Text = "修改";
this.btnUpdate.UseVisualStyleBackColor = true;
this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
//
// btn_save
//
this.btn_save.Location = new System.Drawing.Point(229, 88);
this.btn_save.Margin = new System.Windows.Forms.Padding(2);
this.btn_save.Name = "btn_save";
this.btn_save.Size = new System.Drawing.Size(103, 26);
this.btn_save.TabIndex = 7;
this.btn_save.Text = "保存";
this.btn_save.UseVisualStyleBackColor = true;
this.btn_save.Click += new System.EventHandler(this.btn_save_Click);
//
// btn_get
//
this.btn_get.Location = new System.Drawing.Point(229, 35);
this.btn_get.Margin = new System.Windows.Forms.Padding(2);
this.btn_get.Name = "btn_get";
this.btn_get.Size = new System.Drawing.Size(103, 26);
this.btn_get.TabIndex = 6;
this.btn_get.Text = "获取";
this.btn_get.UseVisualStyleBackColor = true;
this.btn_get.Click += new System.EventHandler(this.btn_get_Click);
//
// txt_humidity
//
this.txt_humidity.Location = new System.Drawing.Point(82, 100);
this.txt_humidity.Margin = new System.Windows.Forms.Padding(2);
this.txt_humidity.Name = "txt_humidity";
this.txt_humidity.Size = new System.Drawing.Size(110, 21);
this.txt_humidity.TabIndex = 5;
//
// txt_temperature
//
this.txt_temperature.Location = new System.Drawing.Point(82, 70);
this.txt_temperature.Margin = new System.Windows.Forms.Padding(2);
this.txt_temperature.Name = "txt_temperature";
this.txt_temperature.Size = new System.Drawing.Size(110, 21);
this.txt_temperature.TabIndex = 4;
//
// txt_light
//
this.txt_light.Location = new System.Drawing.Point(82, 35);
this.txt_light.Margin = new System.Windows.Forms.Padding(2);
this.txt_light.Name = "txt_light";
this.txt_light.Size = new System.Drawing.Size(110, 21);
this.txt_light.TabIndex = 3;
//
// lbl_humidity
//
this.lbl_humidity.AutoSize = true;
this.lbl_humidity.Location = new System.Drawing.Point(40, 102);
this.lbl_humidity.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.lbl_humidity.Name = "lbl_humidity";
this.lbl_humidity.Size = new System.Drawing.Size(29, 12);
this.lbl_humidity.TabIndex = 2;
this.lbl_humidity.Text = "湿度";
//
// lbl_temperature
//
this.lbl_temperature.AutoSize = true;
this.lbl_temperature.Location = new System.Drawing.Point(40, 70);
this.lbl_temperature.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.lbl_temperature.Name = "lbl_temperature";
this.lbl_temperature.Size = new System.Drawing.Size(29, 12);
this.lbl_temperature.TabIndex = 1;
this.lbl_temperature.Text = "温度";
//
// lbl_light
//
this.lbl_light.AutoSize = true;
this.lbl_light.Location = new System.Drawing.Point(40, 38);
this.lbl_light.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.lbl_light.Name = "lbl_light";
this.lbl_light.Size = new System.Drawing.Size(29, 12);
this.lbl_light.TabIndex = 0;
this.lbl_light.Text = "光照度";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.btn_Open);
this.groupBox2.Controls.Add(this.com_port);
this.groupBox2.Controls.Add(this.label4);
this.groupBox2.Location = new System.Drawing.Point(4, 10);
this.groupBox2.Margin = new System.Windows.Forms.Padding(2);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Padding = new System.Windows.Forms.Padding(2);
this.groupBox2.Size = new System.Drawing.Size(199, 181);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "配置";
//
// btn_Open
//
this.btn_Open.Location = new System.Drawing.Point(55, 102);
this.btn_Open.Margin = new System.Windows.Forms.Padding(2);
this.btn_Open.Name = "btn_Open";
this.btn_Open.Size = new System.Drawing.Size(53, 20);
this.btn_Open.TabIndex = 4;
this.btn_Open.Text = "开启";
this.btn_Open.UseVisualStyleBackColor = true;
this.btn_Open.Click += new System.EventHandler(this.btn_Open_Click);
//
// com_port
//
this.com_port.FormattingEnabled = true;
this.com_port.Location = new System.Drawing.Point(68, 53);
this.com_port.Margin = new System.Windows.Forms.Padding(2);
this.com_port.Name = "com_port";
this.com_port.Size = new System.Drawing.Size(115, 20);
this.com_port.TabIndex = 3;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(12, 56);
this.label4.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(29, 12);
this.label4.TabIndex = 2;
this.label4.Text = "串口";
//
// groupBox3
//
this.groupBox3.Controls.Add(this.dataGridView1);
this.groupBox3.Location = new System.Drawing.Point(4, 195);
this.groupBox3.Margin = new System.Windows.Forms.Padding(2);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Padding = new System.Windows.Forms.Padding(2);
this.groupBox3.Size = new System.Drawing.Size(625, 214);
this.groupBox3.TabIndex = 3;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "历史记录";
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(7, 19);
this.dataGridView1.Margin = new System.Windows.Forms.Padding(2);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 27;
this.dataGridView1.Size = new System.Drawing.Size(614, 191);
this.dataGridView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(640, 412);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Margin = new System.Windows.Forms.Padding(2);
this.Name = "Form1";
this.Text = "校园灾情数据存储";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btn_save;
private System.Windows.Forms.Button btn_get;
private System.Windows.Forms.TextBox txt_humidity;
private System.Windows.Forms.TextBox txt_temperature;
private System.Windows.Forms.TextBox txt_light;
private System.Windows.Forms.Label lbl_humidity;
private System.Windows.Forms.Label lbl_temperature;
private System.Windows.Forms.Label lbl_light;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button btn_Open;
private System.Windows.Forms.ComboBox com_port;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnUpdate;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.DataGridView dataGridView1;
}
}
+129
View File
@@ -0,0 +1,129 @@
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;
using System.Data.SqlClient;//引用数据库操作库
using System.IO.Ports;
using DeviceCtrlLibrary;
using DeviceCtrlLibrary.Model;//引用ADAM4017ResModel
namespace 校园数据存储
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string sqlConnectString = @"Data Source=.;Initial Catalog=环境数据表;User ID=sa;Password=123456";//数据库的连接字符串
SqlConnection mySqlConnection;
SqlCommand mySqlCmd;
SqlDataAdapter sqldata;
bool IsOpen = false;
ADAM4017ResModel ResModel;
DataSet ds;
private void Form1_Load(object sender, EventArgs e)
{
mySqlConnection = new SqlConnection(sqlConnectString);
mySqlConnection.Open();
//将系统串口名依次加入下拉列表框中
foreach (string item in SerialPort.GetPortNames())
{
com_port.Items.Add(item);
}
}
private void btn_Open_Click(object sender, EventArgs e)
{
if (!IsOpen)
{
DeviceCtrlHelper.adam4017Helper.Open(com_port.Text);
btn_Open.Text = "关闭";
IsOpen = true;
}
else
{
DeviceCtrlHelper.adam4017Helper.Close();
btn_Open.Text = "开启";
IsOpen = false;
}
}
private void btn_get_Click(object sender, EventArgs e)
{
ResModel = DeviceCtrlHelper.adam4017Helper.SetData();
txt_light.Text = ResModel.lightValue;
txt_humidity.Text = ResModel.humidityValue;
txt_temperature.Text = ResModel.temperatureValue;
}
//数据绑定:查询表中全部数据,显示在表格中
private void Databind()
{
mySqlConnection = new SqlConnection(sqlConnectString);
mySqlConnection.Open();
SqlCommand sqlcmd = new SqlCommand("select ID as '编号',temperature as '温度',humidity as '湿度',light as '光照度', addtime as '时间' from CampusEnvironment", mySqlConnection);
sqldata = new SqlDataAdapter(sqlcmd);
ds = new DataSet();
sqldata.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void btn_save_Click(object sender, EventArgs e)
{
string light = txt_light.Text;
string temperature = txt_temperature.Text;
string humidity = txt_humidity.Text;
try
{
//写明列名,值按列名的顺序存入对应的列
mySqlCmd = new SqlCommand("insert into CampusEnvironment (temperature, humidity, light, addtime) values (" + temperature + "," + humidity + "," + light + ", '" + DateTime.Now.ToString() + "')", mySqlConnection);
mySqlCmd.ExecuteNonQuery();
MessageBox.Show("保存成功!");
Databind();
}
catch (Exception ex)
{
MessageBox.Show("保存失败:" + ex.ToString());
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
//根据查询语句自动生成更新语句,把表格中修改过的数据写回数据库
SqlCommandBuilder cmdb = new SqlCommandBuilder(sqldata);
sqldata.UpdateCommand = cmdb.GetUpdateCommand();
if (sqldata.Update(ds.Tables[0]) == 1)
{
MessageBox.Show("修改成功!");
Databind();
}
else
{
MessageBox.Show("修改失败!");
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
mySqlCmd = new SqlCommand("delete from CampusEnvironment where id=" + dataGridView1.CurrentRow.Cells[0].Value.ToString(), mySqlConnection);
mySqlCmd.ExecuteNonQuery();
MessageBox.Show("删除成功!");
Databind();
}
catch (Exception ex) //捕获数据库操作异常信息
{
MessageBox.Show("删除失败:" + ex.ToString());
}
}
}
}
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 校园数据存储
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
@@ -0,0 +1,44 @@
# 评分规则:书上评价表的功能项。每项单独启动一次程序(使用 database.sql 建的初始数据库),按步骤操作,核对结果。
# 格式说明见 server/src/grading/checks.ts
- title: 能正确显示光照度、温度、湿度
steps:
- select: { control: com_port, item: COM2 }
- click: btn_Open
- expect: { control: btn_Open, text: 关闭, desc: 按钮文字变成"关闭" }
- board: { light: 733, temperature: 31, humidity: 42 }
- click: btn_get
- expect: { control: txt_light, text: '733' }
- expect: { control: txt_temperature, text: '31' }
- expect: { control: txt_humidity, text: '42' }
- title: 能把数据保存到数据库,并显示在表格中
steps:
- select: { control: com_port, item: COM2 }
- click: btn_Open
- board: { light: 733, temperature: 31, humidity: 42 }
- click: btn_get
- click: btn_save
- expect: { messageBox: 保存成功 }
- expect: { control: dataGridView1, row: [温度=31, 湿度=42, 光照度=733], desc: 表格里有刚保存的一行,温度、湿度、光照度各在对应的列 }
- title: 能删除选中的数据
steps:
- select: { control: com_port, item: COM2 }
- click: btn_Open
- click: btn_get
- click: btn_save
- cell: { control: dataGridView1, row: 0, column: 0 }
- click: btnDelete
- expect: { messageBox: 删除成功 }
- expect: { control: dataGridView1, noRow: [编号=1], desc: 第 1 行(编号为 1)已被删除 }
- title: 能在表格中修改数据并保存
steps:
- select: { control: com_port, item: COM2 }
- click: btn_Open
- click: btn_get
- click: btn_save
- edit: { control: dataGridView1, row: 1, column: 温度, value: '35' }
- click: btnUpdate
- expect: { messageBox: 修改成功 }
- expect: { control: dataGridView1, row: [编号=2, 温度=35], desc: 编号为 2 的一行温度改成了 35 }
@@ -0,0 +1,14 @@
-- 实验的初始数据库:任务 1 建的"环境数据表"库和 EnvironmentWork 表,任务 2 第 1 步建的 CampusEnvironment 表。
-- 学生可以在数据库页面"恢复实验初始数据";评分时每个检查项用一份新的。
CREATE DATABASE 环境数据表
GO
USE 环境数据表
GO
CREATE TABLE EnvironmentWork (light varchar(MAX) NULL, temperature varchar(MAX) NULL, humidity varchar(MAX) NULL)
CREATE TABLE CampusEnvironment (ID int IDENTITY(1,1) PRIMARY KEY, light varchar(50), humidity varchar(50), temperature varchar(50), addtime datetime)
GO
INSERT INTO EnvironmentWork VALUES ('520', '24', '58')
INSERT INTO EnvironmentWork VALUES ('480', '25', '61')
INSERT INTO CampusEnvironment (light, humidity, temperature, addtime) VALUES ('520', '58', '24', '2024-01-16 09:00:00')
INSERT INTO CampusEnvironment (light, humidity, temperature, addtime) VALUES ('610', '55', '26', '2024-01-18 14:30:00')
INSERT INTO CampusEnvironment (light, humidity, temperature, addtime) VALUES ('300', '63', '21', '2024-01-23 08:15:00')
@@ -0,0 +1,4 @@
{
"title": "项目6 任务2 存储校园环境数据",
"source": "教材书 p119–124,界面取自配套参考工程,控件名按书上表格修改;修正 True/False 大写、全角引号;INSERT 写明列名(书上不写列名时,值的顺序和建表的列顺序不一致,温度和光照会存反)"
}
+138
View File
@@ -0,0 +1,138 @@
namespace 校园数据存储
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.btnCx = new System.Windows.Forms.Button();
this.LblStartTime = new System.Windows.Forms.Label();
this.LblEndTime = new System.Windows.Forms.Label();
this.StartTime = new System.Windows.Forms.DateTimePicker();
this.EndTime = new System.Windows.Forms.DateTimePicker();
this.groupBox4.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// groupBox4
//
this.groupBox4.Controls.Add(this.dataGridView1);
this.groupBox4.Location = new System.Drawing.Point(1, 52);
this.groupBox4.Margin = new System.Windows.Forms.Padding(2);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Padding = new System.Windows.Forms.Padding(2);
this.groupBox4.Size = new System.Drawing.Size(642, 283);
this.groupBox4.TabIndex = 3;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "历史记录";
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(4, 17);
this.dataGridView1.Margin = new System.Windows.Forms.Padding(2);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 27;
this.dataGridView1.Size = new System.Drawing.Size(634, 261);
this.dataGridView1.TabIndex = 0;
//
// btnCx
//
this.btnCx.Location = new System.Drawing.Point(541, 19);
this.btnCx.Name = "btnCx";
this.btnCx.Size = new System.Drawing.Size(79, 23);
this.btnCx.TabIndex = 4;
this.btnCx.Text = "查询";
this.btnCx.UseVisualStyleBackColor = true;
this.btnCx.Click += new System.EventHandler(this.btnCx_Click);
//
// LblStartTime
//
this.LblStartTime.AutoSize = true;
this.LblStartTime.Location = new System.Drawing.Point(25, 23);
this.LblStartTime.Name = "LblStartTime";
this.LblStartTime.Size = new System.Drawing.Size(53, 12);
this.LblStartTime.TabIndex = 5;
this.LblStartTime.Text = "开始日期";
//
// LblEndTime
//
this.LblEndTime.AutoSize = true;
this.LblEndTime.Location = new System.Drawing.Point(272, 25);
this.LblEndTime.Name = "LblEndTime";
this.LblEndTime.Size = new System.Drawing.Size(53, 12);
this.LblEndTime.TabIndex = 6;
this.LblEndTime.Text = "结束日期";
//
// StartTime
//
this.StartTime.Location = new System.Drawing.Point(84, 19);
this.StartTime.Name = "StartTime";
this.StartTime.Size = new System.Drawing.Size(136, 21);
this.StartTime.TabIndex = 7;
//
// EndTime
//
this.EndTime.Location = new System.Drawing.Point(358, 19);
this.EndTime.Name = "EndTime";
this.EndTime.Size = new System.Drawing.Size(148, 21);
this.EndTime.TabIndex = 8;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(654, 346);
this.Controls.Add(this.EndTime);
this.Controls.Add(this.StartTime);
this.Controls.Add(this.LblEndTime);
this.Controls.Add(this.LblStartTime);
this.Controls.Add(this.btnCx);
this.Controls.Add(this.groupBox4);
this.Margin = new System.Windows.Forms.Padding(2);
this.Name = "Form1";
this.Text = "查询校园灾情数据";
this.groupBox4.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button btnCx;
private System.Windows.Forms.Label LblStartTime;
private System.Windows.Forms.Label LblEndTime;
private System.Windows.Forms.DateTimePicker StartTime;
private System.Windows.Forms.DateTimePicker EndTime;
}
}
+41
View File
@@ -0,0 +1,41 @@
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;
using System.Data.SqlClient;
using System.IO.Ports;
namespace 校园数据存储
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string sqlConnectString = @"Data Source=.;Initial Catalog=环境数据表;User ID=sa;Password=123456";//数据库的连接字符串
SqlConnection mySqlConnection;
SqlCommand mySqlCmd;
SqlDataAdapter sqldata;
DataSet ds;
private void btnCx_Click(object sender, EventArgs e)
{
mySqlConnection = new SqlConnection(sqlConnectString);//创建数据库连接对象
mySqlConnection.Open();//打开数据库
//查询开始日期和结束日期之间的数据
SqlCommand sqlcmd = new SqlCommand("select ID as '编号',temperature as '温度',humidity as '湿度',light as '光照度',addtime as '时间' from CampusEnvironment where addtime BETWEEN '" + StartTime.Value.ToString() + "' AND '" + EndTime.Value.ToString() + "'", mySqlConnection);
sqldata = new SqlDataAdapter(sqlcmd);
ds = new DataSet();
sqldata.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
mySqlConnection.Close();//关闭数据库
}
}
}
+22
View File
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 校园数据存储
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
+16
View File
@@ -0,0 +1,16 @@
# 评分规则:书上评价表的功能项。每项单独启动一次程序(使用 database.sql 建的初始数据库),按步骤操作,核对结果。
# 初始数据有 3 行:2024-01-16 09:00、2024-01-18 14:30、2024-01-23 08:15
# 格式说明见 server/src/grading/checks.ts
- title: 能实现按日期查询灾情数据
steps:
- pick: { control: StartTime, value: '2024-01-15' }
- pick: { control: EndTime, value: '2024-01-20' }
- click: btnCx
- expect: { control: dataGridView1, row: [编号=1], desc: 查到 1 月 16 日的数据 }
- expect: { control: dataGridView1, row: [编号=2], desc: 查到 1 月 18 日的数据 }
- expect: { control: dataGridView1, noRow: [编号=3], desc: 不包含时间段以外(1 月 23 日)的数据 }
- pick: { control: StartTime, value: '2024-01-21' }
- pick: { control: EndTime, value: '2024-01-31' }
- click: btnCx
- expect: { control: dataGridView1, row: [编号=3], desc: 改变日期后重新查询,查到 1 月 23 日的数据 }
- expect: { control: dataGridView1, noRow: [编号=1], desc: 改变日期后重新查询,不再包含 1 月 16 日的数据 }
@@ -0,0 +1,14 @@
-- 实验的初始数据库:任务 1 建的"环境数据表"库和 EnvironmentWork 表,任务 2 第 1 步建的 CampusEnvironment 表。
-- 学生可以在数据库页面"恢复实验初始数据";评分时每个检查项用一份新的。
CREATE DATABASE 环境数据表
GO
USE 环境数据表
GO
CREATE TABLE EnvironmentWork (light varchar(MAX) NULL, temperature varchar(MAX) NULL, humidity varchar(MAX) NULL)
CREATE TABLE CampusEnvironment (ID int IDENTITY(1,1) PRIMARY KEY, light varchar(50), humidity varchar(50), temperature varchar(50), addtime datetime)
GO
INSERT INTO EnvironmentWork VALUES ('520', '24', '58')
INSERT INTO EnvironmentWork VALUES ('480', '25', '61')
INSERT INTO CampusEnvironment (light, humidity, temperature, addtime) VALUES ('520', '58', '24', '2024-01-16 09:00:00')
INSERT INTO CampusEnvironment (light, humidity, temperature, addtime) VALUES ('610', '55', '26', '2024-01-18 14:30:00')
INSERT INTO CampusEnvironment (light, humidity, temperature, addtime) VALUES ('300', '63', '21', '2024-01-23 08:15:00')
+4
View File
@@ -0,0 +1,4 @@
{
"title": "项目6 任务3 查询校园安防数据",
"source": "教材书 p127–128,界面取自配套参考工程;补上书上漏写的 ds = new DataSet();(不写时 Fill 报空引用)。数据库和任务 2 相同(环境数据表)"
}