- 每个学生一套数据库,长期保存(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>
61 lines
2.4 KiB
C#
61 lines
2.4 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Drawing;
|
||
using System.Globalization;
|
||
using System.Text.Json.Nodes;
|
||
using System.Windows.Forms;
|
||
|
||
namespace Sim.Host;
|
||
|
||
/// <summary>把控件属性值转换成发给浏览器的 JSON(格式见 web/src/winforms/format.ts)</summary>
|
||
public static class PropValue
|
||
{
|
||
public static JsonNode? From(object? value) => value switch
|
||
{
|
||
null => null,
|
||
// 控件自己组织好的结构(如 DataGridView 的列和行)原样发送
|
||
JsonNode node => node.Parent == null ? node : node.DeepClone(),
|
||
string s => JsonValueOf(s),
|
||
bool b => JsonValueOf(b),
|
||
char c => JsonValueOf(c == '\0' ? "" : c.ToString()),
|
||
int or long or short or byte => JsonValueOf(Convert.ToInt64(value, CultureInfo.InvariantCulture)),
|
||
float or double or decimal => JsonValueOf(Convert.ToDouble(value, CultureInfo.InvariantCulture)),
|
||
Enum e => JsonValueOf(e.ToString()),
|
||
Point p => new JsonObject { ["x"] = p.X, ["y"] = p.Y },
|
||
Size s => new JsonObject { ["width"] = s.Width, ["height"] = s.Height },
|
||
Color c => ColorCss(c),
|
||
Font f => new JsonObject
|
||
{
|
||
["family"] = f.Name,
|
||
["size"] = f.SizeInPoints,
|
||
["bold"] = f.Bold,
|
||
["italic"] = f.Italic,
|
||
["underline"] = f.Underline,
|
||
["strikeout"] = f.Strikeout,
|
||
},
|
||
Image image => new JsonObject { ["key"] = SimHost.ImageKey(image), ["width"] = image.Width, ["height"] = image.Height },
|
||
Padding p => new JsonObject { ["left"] = p.Left, ["top"] = p.Top, ["right"] = p.Right, ["bottom"] = p.Bottom },
|
||
IEnumerable list => ListOf(list),
|
||
_ => JsonValueOf(value.ToString() ?? ""),
|
||
};
|
||
|
||
/// <summary>未设置的颜色(Color.Empty)返回 null,表示沿用父控件的颜色</summary>
|
||
public static JsonNode? ColorCss(Color color)
|
||
{
|
||
if (color.IsEmpty) return null;
|
||
if (color.A == 0) return "transparent";
|
||
if (color.A < 255)
|
||
return $"rgba({color.R},{color.G},{color.B},{(color.A / 255.0).ToString("0.###", CultureInfo.InvariantCulture)})";
|
||
return $"#{color.R:x2}{color.G:x2}{color.B:x2}";
|
||
}
|
||
|
||
private static JsonArray ListOf(IEnumerable items)
|
||
{
|
||
var array = new JsonArray();
|
||
foreach (var item in items) array.Add(From(item));
|
||
return array;
|
||
}
|
||
|
||
private static JsonNode JsonValueOf<T>(T value) => JsonValue.Create(value)!;
|
||
}
|