项目 6 数据存储:仿冒 SqlClient(T-SQL 转写成 SQLite)、数据库页面、DataGridView、DateTimePicker
- 每个学生一套数据库,长期保存(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:
1 parent
6b088240da
commit
670abe5921
72 files changed
+7253
-51
No files matched your search
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user