- 示例 p8-t1…t5(代码逐个累加):主界面、校园环境界面、环境采集与分析(路灯手动 / 自动控制、 LED 推送、自动推送火情、7 天温度图表)、校园安防(入侵提示、摄像机方向、拍照入库)、历史图片 (按日期查询、左右切换),各有评分规则 - 按书补全参考工程里空着的"推送LED信息""自动控制",给"自动推送火情"绑定 CheckedChanged (书上第 23 步写成了 ch_autoLamp);任务 5 每次查询前清空 Keys - 素材:项目 8 的 20 多张图片全部自绘,名字和尺寸与教材一致(project8.py 生成 SVG, build-gallery.sh 渲染成 PNG;源文件移到 svg/) - 数据库 WisdomCampus 按教材的表结构建(EnvironmentData、SecurityData、Users),预置 7 天环境数据 - 测试:参考答案全部通过;没绑定"自动推送火情"、开灯换错背景图片时报出对应原因 - 文档:教材 8 个项目全部有示例,共 23 个 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
4.0 KiB
C#
107 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace 智慧校园.SQLHelpers
|
|
{
|
|
class SQLBLL
|
|
{
|
|
|
|
public static SQLRes SecurityUpData(Image img)//把图片保存到数据库
|
|
{
|
|
SQLRes sqlRes = new SQLRes();
|
|
byte[] imgByte = null;
|
|
|
|
using (MemoryStream ms = new MemoryStream())//图片转换为二进制数组
|
|
{
|
|
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
imgByte = new byte[ms.Length];
|
|
imgByte = ms.ToArray();
|
|
}
|
|
|
|
string sqlStr = "INSERT INTO SecurityData (EvenTime,EvenImage) VALUES (@time,@img)";
|
|
SqlParameter[] sqlParameter = new SqlParameter[] { new SqlParameter("time", DateTime.Now.ToString()), new SqlParameter("img", imgByte) };
|
|
|
|
if (SQLHelper.Exec(sqlStr, sqlParameter) >= 1)
|
|
{
|
|
sqlRes.Result = true;
|
|
sqlRes.Msg = "成功!";
|
|
}
|
|
else
|
|
{
|
|
sqlRes.Result = false;
|
|
sqlRes.Msg = "失败!";
|
|
}
|
|
|
|
return sqlRes;
|
|
}
|
|
|
|
public static Dictionary<string, Image> GetSecurityData(DateTime startDate, DateTime endDate)
|
|
{
|
|
Dictionary<string, Image> ImgDictionary = new Dictionary<string, Image>();
|
|
|
|
if (string.IsNullOrWhiteSpace(startDate.Month.ToString()) && string.IsNullOrWhiteSpace(endDate.Month .ToString()))
|
|
{
|
|
return ImgDictionary;
|
|
}
|
|
//选择查询日期日期
|
|
string sql = "SELECT * FROM SecurityData WHERE EvenTime between @time_start and @time_end";
|
|
SqlParameter[] sqlParameter = new SqlParameter[] { new SqlParameter("time_start", startDate.Year + "/" + startDate.Month + "/" + startDate.Day), new SqlParameter("time_end", endDate.Year + "/" + endDate.Month + "/" + endDate.Day+" 23:59:59" )};
|
|
|
|
DataSet ds = SQLHelper.getData(sql, sqlParameter);
|
|
|
|
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
|
|
{
|
|
ImgDictionary.Add(ds.Tables[0].Rows[i]["EvenTime"].ToString(), Image.FromStream(new MemoryStream((byte[])ds.Tables[0].Rows[i]["EvenImage"])));
|
|
}
|
|
|
|
return ImgDictionary;
|
|
}
|
|
|
|
public static SQLRes EnvironmentUpData(bool fire, bool smoke, string light, string temperature, string humidity)
|
|
{
|
|
|
|
SQLRes sqlRes = new SQLRes();
|
|
|
|
string sqlStr = "INSERT INTO EnvironmentData (Fire ,Smoke ,Temperature ,Humidity , Light, _Time) VALUES (@fire,@smoke,@temperature,@humidity,@light,@time)";
|
|
SqlParameter[] sqlParameter = new SqlParameter[] { new SqlParameter("time", DateTime.Now.ToString()), new SqlParameter("fire", fire), new SqlParameter("smoke", smoke), new SqlParameter("temperature", temperature), new SqlParameter("humidity", humidity), new SqlParameter("light", light) };
|
|
|
|
if (SQLHelper.Exec(sqlStr, sqlParameter) >= 1)
|
|
{
|
|
sqlRes.Result = true;
|
|
sqlRes.Msg = "成功!";
|
|
}
|
|
else
|
|
{
|
|
sqlRes.Result = false;
|
|
sqlRes.Msg = "失败!";
|
|
}
|
|
|
|
return sqlRes;
|
|
|
|
}
|
|
|
|
public static DataTable GetEnvironmentGraphData()
|
|
{
|
|
string sqlStr = "SELECT TOP 7 CONVERT(varchar(10),_Time,120) AS _Time, ROUND(Max(Temperature),2) AS MaxTemperature, ROUND(AVG(Temperature),2) AS AVGTemperature FROM EnvironmentData GROUP BY CONVERT(varchar(10),_Time,120) ORDER BY CONVERT(varchar(10),_Time,120) DESC ";
|
|
return SQLHelper.getData(sqlStr, null).Tables[0];
|
|
}
|
|
|
|
|
|
}
|
|
|
|
class SQLRes
|
|
{
|
|
public string Msg { get; set; }
|
|
public bool Result { get; set; }
|
|
}
|
|
|
|
}
|
|
|