项目 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

+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 相同(环境数据表)"
}