123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742 |
- using MeterVision.model;
- using MeterVision.Stand;
- using System;
- using System.Collections.Generic;
- using System.Data.SQLite;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MeterVision.db
- {
- public class DBStation
- {
- public static bool InsertTStation(TStation tStation)
- {
- // 构建插入的 SQL 语句
- string sql = "INSERT OR IGNORE INTO t_station (id,station_id, station_name, device_sn,meter_type, bright_val, flow_rate, " +
- "dial_region, num_count, ind_count, feature_region, last_unit, " +
- "last_value, last_time, create_time, stand_id) " +
- "VALUES (@StationId, @StationName, @DeviceSn, @MeterType, @BrightVal, @FlowRate, " +
- "@DialRegion, @NumCount, @IndCount, @FeatureRegion, @LastUnit, " +
- "@LastValue, @LastTime, @CreateTime, @StandId)";
- // 创建参数数组
- SQLiteParameter[] parameters = new SQLiteParameter[]
- {
- new SQLiteParameter("@Id", tStation.Id),
- new SQLiteParameter("@StationId", tStation.StationId),
- new SQLiteParameter("@StationName", tStation.StationName),
- new SQLiteParameter("@DeviceSn", tStation.DeviceSn),
- new SQLiteParameter("@MeterType", tStation.MeterType),
- new SQLiteParameter("@BrightVal", tStation.BrightVal),
- new SQLiteParameter("@FlowRate", tStation.FlowRate),
- new SQLiteParameter("@DialRegion", tStation.DialRegion),
- new SQLiteParameter("@NumCount", tStation.NumCount),
- new SQLiteParameter("@IndCount", tStation.IndCount),
- new SQLiteParameter("@FeatureRegion", tStation.FeatureRegion),
- new SQLiteParameter("@LastUnit", tStation.LastUnit),
- new SQLiteParameter("@LastValue", tStation.LastValue),
- new SQLiteParameter("@LastTime", tStation.LastTime),
- new SQLiteParameter("@CreateTime", tStation.CreateTime),
- new SQLiteParameter("@StandId", tStation.StandId),
- };
- try
- {
- // 调用 SQLiteHelper 执行插入操作
- int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
- // 如果插入成功,返回 true,否则返回 false
- return rowsAffected > 0;
- }
- catch (Exception ex)
- {
- // 处理异常(如果有的话)
- Console.WriteLine("Error inserting data: " + ex.Message);
- return false;
- }
- }
- public static bool InsertTStations(List<StationInfo> stationInfos)
- {
- //INSERT OR IGNORE INTO
- string sql = @"
- INSERT OR IGNORE INTO t_station (id, stand_id,create_time, station_id, device_sn, num_count, last_unit)
- VALUES (@Id, @StandId, @CreateTime, @StationId, @DeviceSn, @NumCount, @LastUnit);";
- try
- {
- // 开始事务
- using (SQLiteConnection connection = SQLiteHelper.GetConnection())
- {
- connection.Open();
- using (SQLiteTransaction transaction = connection.BeginTransaction())
- {
- // 遍历列表插入每一条数据
- foreach (var stationInfo in stationInfos)
- {
- SQLiteParameter[] parameters = new SQLiteParameter[]
- {
- new SQLiteParameter("@Id", Guid.NewGuid().ToString()),
- new SQLiteParameter("@StandId", stationInfo.StandId),
- new SQLiteParameter("@CreateTime", ThisApp.GetNowTime_yyyyMMddHHmmss()),
- new SQLiteParameter("@StationId", stationInfo.StationId),
- new SQLiteParameter("@DeviceSn", stationInfo.DeviceSn),
- new SQLiteParameter("@NumCount", stationInfo.NumCount),
- new SQLiteParameter("@LastUnit", stationInfo.LastUnit2)
- };
- // 执行插入操作
- SQLiteHelper.ExecuteNonQuery(sql, parameters, transaction);
- }
- // 提交事务
- transaction.Commit();
- }
- }
- return true;
- }
- catch(Exception ex)
- {
- Console.WriteLine(ex.Message);
- return false;
- }
- }
- public static Tuple<int, int, List<TStation>> GetPagedTStations(int pageNumber, int pageSize, string stationId)
- {
- // 计算 OFFSET 值
- int offset = (pageNumber - 1) * pageSize;
- // 定义 SQL 查询语句,带有分页
- // 定义 SQL 查询语句的基础部分
- string sql = @"
- SELECT id,station_id, station_name, device_sn,meter_type, bright_val, flow_rate, dial_region,
- num_count, ind_count, feature_region, last_unit,
- last_value, last_time, create_time,stand_id
- FROM t_station ";
- // 如果传入的 stationId 不为空,增加过滤条件
- if (!string.IsNullOrEmpty(stationId))
- {
- sql += "WHERE station_id LIKE @StationId ";
- }
- // 添加排序和分页
- sql += "ORDER BY station_id ASC LIMIT @PageSize OFFSET @Offset;";
- // 创建 SQL 参数
- List<SQLiteParameter> parameters = new List<SQLiteParameter>
- {
- new SQLiteParameter("@PageSize", pageSize),
- new SQLiteParameter("@Offset", offset),
- };
- // 如果传入了 stationId,添加到参数中
- if (!string.IsNullOrEmpty(stationId))
- {
- parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
- }
- // 执行查询并获取结果
- List<TStation> stations = new List<TStation>();
- using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
- {
- while (reader.Read())
- {
- // 将查询结果映射到 TStation 对象
- TStation station = new TStation
- {
- Id = reader.GetString(reader.GetOrdinal("id")),
- StationId = reader.GetString(reader.GetOrdinal("station_id")),
- StationName = reader.GetString(reader.GetOrdinal("station_name")),
- DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
- MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
- BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
- FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
- DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
- NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
- IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
- FeatureRegion = reader.GetString(reader.GetOrdinal("feature_region")),
- LastUnit = reader.GetDouble(reader.GetOrdinal("last_unit")),
- LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
- LastTime = reader.GetString(reader.GetOrdinal("last_time")),
- CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
- StandId = reader.GetString(reader.GetOrdinal("stand_id"))
- };
- stations.Add(station);
- }
- }
- // 获取总记录数,用于计算总页数
- string countSql = "SELECT COUNT(*) FROM t_station";
- int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql));
- // 计算总页数
- int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
- // 返回元组,包含总记录数、总页数和结果列表
- return Tuple.Create(totalRecords, totalPages, stations);
- }
- //public static Tuple<int, int, List<TStation>> GetPagedTStations(int pageNumber, int pageSize,string stationId,string standId)
- //{
- // // 计算 OFFSET 值
- // int offset = (pageNumber - 1) * pageSize;
- // // 定义 SQL 查询语句,带有分页
- // // 定义 SQL 查询语句的基础部分
- // string sql = @"
- // SELECT id,station_id, station_name, device_sn,meter_type, bright_val, flow_rate, dial_region,
- // num_count, ind_count, num_region, ht_region, last_num_unit, last_ind_unit,
- // last_value, last_time, create_time,stand_id
- // FROM t_station ";
- // sql += "WHERE stand_id = @StandId";
- // // 如果传入的 stationId 不为空,增加过滤条件
- // if (!string.IsNullOrEmpty(stationId))
- // {
- // //sql += "WHERE station_id LIKE @StationId ";
- // sql += " AND station_id LIKE @StationId ";
- // }
- // // 添加排序和分页
- // sql += " ORDER BY station_id ASC LIMIT @PageSize OFFSET @Offset;";
- // // 创建 SQL 参数
- // List<SQLiteParameter> parameters = new List<SQLiteParameter>
- // {
- // new SQLiteParameter("@PageSize", pageSize),
- // new SQLiteParameter("@Offset", offset),
- // new SQLiteParameter("@StandId",standId)
- // };
- // // 如果传入了 stationId,添加到参数中
- // if (!string.IsNullOrEmpty(stationId))
- // {
- // parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
- // }
- // // 执行查询并获取结果
- // List<TStation> stations = new List<TStation>();
- // using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
- // {
- // while (reader.Read())
- // {
- // // 将查询结果映射到 TStation 对象
- // TStation station = new TStation
- // {
- // Id = reader.GetString(reader.GetOrdinal("id")),
- // StationId = reader.GetString(reader.GetOrdinal("station_id")),
- // StationName = reader.GetString(reader.GetOrdinal("station_name")),
- // DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
- // MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
- // BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
- // FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
- // DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
- // NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
- // IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
- // NumRegion = reader.GetString(reader.GetOrdinal("num_region")),
- // HtRegion = reader.GetString(reader.GetOrdinal("ht_region")),
- // LastNumUnit = reader.GetDouble(reader.GetOrdinal("last_num_unit")),
- // LastIndUnit = reader.GetDouble(reader.GetOrdinal("last_ind_unit")),
- // LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
- // LastTime = reader.GetString(reader.GetOrdinal("last_time")),
- // CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
- // StandId = reader.GetString(reader.GetOrdinal("stand_id"))
- // };
- // stations.Add(station);
- // }
- // }
- // // 获取总记录数,用于计算总页数
- // string countSql = "SELECT COUNT(*) FROM t_station ";
- // int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql));
- // // 计算总页数
- // int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
- // // 返回元组,包含总记录数、总页数和结果列表
- // return Tuple.Create(totalRecords, totalPages, stations);
- //}
- public static Tuple<int, int, List<TStation>> GetPagedTStations(int pageNumber, int pageSize, string stationId, string standId)
- {
- // 计算 OFFSET 值
- int offset = (pageNumber - 1) * pageSize;
- // 定义 SQL 查询语句
- string baseSql = @"
- FROM t_station
- WHERE stand_id = @StandId";
- // 如果传入的 stationId 不为空,增加过滤条件
- if (!string.IsNullOrEmpty(stationId))
- {
- baseSql += " AND station_id LIKE @StationId";
- }
- // 查询分页数据
- string sql = $@"
- SELECT id, station_id, station_name, device_sn, meter_type, bright_val, flow_rate, dial_region,
- num_count, ind_count, feature_region, last_unit,
- last_value, last_time, create_time, stand_id
- {baseSql}
- ORDER BY station_id ASC
- LIMIT @PageSize OFFSET @Offset;";
- // 查询总记录数
- string countSql = $"SELECT COUNT(*) {baseSql};";
- // 创建 SQL 参数
- List<SQLiteParameter> parameters = new List<SQLiteParameter>
- {
- new SQLiteParameter("@StandId", standId),
- new SQLiteParameter("@PageSize", pageSize),
- new SQLiteParameter("@Offset", offset)
- };
- // 如果传入了 stationId,添加到参数中
- if (!string.IsNullOrEmpty(stationId))
- {
- parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
- }
- // 获取数据列表
- List<TStation> stations = new List<TStation>();
- using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
- {
- while (reader.Read())
- {
- TStation station = new TStation
- {
- Id = reader.GetString(reader.GetOrdinal("id")),
- StationId = reader.GetString(reader.GetOrdinal("station_id")),
- StationName = reader.GetString(reader.GetOrdinal("station_name")),
- DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
- MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
- BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
- FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
- DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
- NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
- IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
- FeatureRegion = reader.GetString(reader.GetOrdinal("feature_region")),
- LastUnit = reader.GetDouble(reader.GetOrdinal("last_unit")),
- LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
- LastTime = reader.GetString(reader.GetOrdinal("last_time")),
- CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
- StandId = reader.GetString(reader.GetOrdinal("stand_id"))
- };
- stations.Add(station);
- }
- }
- // 获取符合条件的总记录数
- int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql, parameters.ToArray()));
- // 计算总页数
- int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
- // 返回元组,包含总记录数、总页数和结果列表
- return Tuple.Create(totalRecords, totalPages, stations);
- }
- public static Tuple<int, int, List<TStation>> GetPagedVStations(int pageNumber, int pageSize,
- string stationId, MarkFindType markFindType,string standId)
- {
- // 计算 OFFSET 值
- int offset = (pageNumber - 1) * pageSize;
- // 定义 SQL 查询语句
- string baseSql = @"
- FROM v_station
- WHERE stand_id = @StandId";
- // 如果传入的 stationId 不为空,增加过滤条件
- if (!string.IsNullOrEmpty(stationId))
- {
- baseSql += " AND station_id LIKE @StationId";
- }
- if(markFindType == MarkFindType.ALL)
- {
- }
- else if(markFindType == MarkFindType.MARK_YES)
- {
- baseSql += " AND mark_count > 0";
- }
- else if(markFindType == MarkFindType.MARK_NO)
- {
- baseSql += " And mark_count = 0";
- }
- // 查询分页数据
- string sql = $@"
- SELECT id, stand_id, station_id, create_time, mark_count
- {baseSql}
- ORDER BY station_id ASC
- LIMIT @PageSize OFFSET @Offset;";
- // 查询总记录数
- string countSql = $"SELECT COUNT(*) {baseSql};";
- // 创建 SQL 参数
- List<SQLiteParameter> parameters = new List<SQLiteParameter>
- {
- new SQLiteParameter("@StandId", standId),
- new SQLiteParameter("@PageSize", pageSize),
- new SQLiteParameter("@Offset", offset)
- };
- // 如果传入了 stationId,添加到参数中
- if (!string.IsNullOrEmpty(stationId))
- {
- parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
- }
- // 获取数据列表
- List<TStation> stations = new List<TStation>();
- using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
- {
- while (reader.Read())
- {
- TStation station = new TStation
- {
- Id = reader.GetString(reader.GetOrdinal("id")),
- StandId = reader.GetString(reader.GetOrdinal("stand_id")),
- StationId = reader.GetString(reader.GetOrdinal("station_id")),
- CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
- MarkCount = reader.GetInt32(reader.GetOrdinal("mark_count")),
- };
- stations.Add(station);
- }
- }
- // 获取符合条件的总记录数
- int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql, parameters.ToArray()));
- // 计算总页数
- int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
- // 返回元组,包含总记录数、总页数和结果列表
- return Tuple.Create(totalRecords, totalPages, stations);
- }
- public static int GetMarkCount(string id)
- {
- int iMarkCount = 0;
- string sql = $@"
- SELECT mark_count
- FROM v_station
- WHERE id = @Id";
- // 定义参数
- SQLiteParameter[] parameters = new SQLiteParameter[]
- {
- new SQLiteParameter("@Id", id)
- };
- // 执行查询并读取数据
- try
- {
- using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters))
- {
- if (reader.Read())
- {
- return reader.GetInt32(reader.GetOrdinal("mark_count"));
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"查询最小 SampleTime 记录时发生错误:{ex.Message}", ex);
- }
- return iMarkCount;
- }
- public static List<TStation> GetAllStation(string standId)
- {
- // 查询分页数据
- string sql = $@"
- SELECT id, station_id, station_name, device_sn, meter_type, bright_val, flow_rate, dial_region,
- num_count, ind_count, feature_region, last_unit,
- last_value, last_time, create_time, stand_id
- FROM t_station
- WHERE stand_id = @StandId AND meter_type > 0
- AND dial_region <> ''
- AND feature_region <> ''
- AND last_time <> ''
- ORDER BY station_id ASC";
- // 定义参数
- SQLiteParameter[] parameters = new SQLiteParameter[]
- {
- new SQLiteParameter("@StandId", standId)
- };
- // 执行查询并读取数据
- List<TStation> stations = new List<TStation>();
- try
- {
- using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
- {
- while (reader.Read())
- {
- TStation station = new TStation
- {
- Id = reader.GetString(reader.GetOrdinal("id")),
- StationId = reader.GetString(reader.GetOrdinal("station_id")),
- StationName = reader.GetString(reader.GetOrdinal("station_name")),
- DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
- MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
- BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
- FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
- DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
- NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
- IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
- FeatureRegion = reader.GetString(reader.GetOrdinal("feature_region")),
- LastUnit = reader.GetDouble(reader.GetOrdinal("last_unit")),
- LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
- LastTime = reader.GetString(reader.GetOrdinal("last_time")),
- CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
- StandId = reader.GetString(reader.GetOrdinal("stand_id"))
- };
- stations.Add(station);
- }
- }
- }
- catch (Exception ex)
- {
- // 根据需要记录日志或抛出异常
- throw new Exception($"查询 TStation表 时发生错误:{ex.Message}", ex);
- }
- return stations;
- }
- public static List<TStation> GetAllVStation(string standId)
- {
- // 查询分页数据
- string sql = $@"
- SELECT id, stand_id, station_id, create_time, mark_count
- FROM v_station
- WHERE stand_id = @StandId AND mark_count > 0
- ORDER BY station_id ASC";
- // 定义参数
- SQLiteParameter[] parameters = new SQLiteParameter[]
- {
- new SQLiteParameter("@StandId", standId)
- };
- // 执行查询并读取数据
- List<TStation> stations = new List<TStation>();
- try
- {
- using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
- {
- while (reader.Read())
- {
- TStation station = new TStation
- {
- Id = reader.GetString(reader.GetOrdinal("id")),
- StandId = reader.GetString(reader.GetOrdinal("stand_id")),
- StationId = reader.GetString(reader.GetOrdinal("station_id")),
- CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
- MarkCount = reader.GetInt32(reader.GetOrdinal("mark_count"))
- };
- stations.Add(station);
- }
- }
- }
- catch (Exception ex)
- {
- // 根据需要记录日志或抛出异常
- throw new Exception($"查询 TStation表 时发生错误:{ex.Message}", ex);
- }
- return stations;
- }
- public static bool DeleteTStation(string id)
- {
- // 定义删除的 SQL 语句
- string sql = "DELETE FROM t_station WHERE id = @Id";
- // 创建 SQL 参数
- SQLiteParameter[] parameters = new SQLiteParameter[]
- {
- new SQLiteParameter("@Id", id)
- };
- try
- {
- // 调用 SQLiteHelper 执行删除操作
- int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
- // 如果删除成功,返回 true,否则返回 false
- return rowsAffected > 0;
- }
- catch (Exception ex)
- {
- // 处理异常(如果有的话)
- Console.WriteLine("Error deleting data: " + ex.Message);
- return false;
- }
- }
- public static bool DeleteTStationByStandId(string standId)
- {
- // 定义删除的 SQL 语句
- string sql = "DELETE FROM t_station WHERE stand_id = @StandId";
- // 创建 SQL 参数
- SQLiteParameter[] parameters = new SQLiteParameter[]
- {
- new SQLiteParameter("@StandId", standId)
- };
- try
- {
- // 调用 SQLiteHelper 执行删除操作
- int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
- // 如果删除成功,返回 true,否则返回 false
- //return rowsAffected > 0;
- return true;
- }
- catch (Exception ex)
- {
- // 处理异常(如果有的话)
- Console.WriteLine("Error deleting data: " + ex.Message);
- return false;
- }
- }
- public static bool DeleteStationAndStandDetails(string standId,string stationId)
- {
- // 定义删除的 SQL 语句,使用事务来保证一致性
- string sql = @"
- BEGIN TRANSACTION;
- -- 删除 t_stand_detail 表中与 stand_id 相关的数据
- DELETE FROM t_stand_detail WHERE station_id = @StationId AND stand_id = @StandId;
- -- 删除 t_station 表中与 stand_id 相关的数据
- DELETE FROM t_station WHERE station_id = @StationId AND stand_id = @StandId;
- COMMIT;";
- // 创建参数
- SQLiteParameter[] parameters = new SQLiteParameter[]
- {
- new SQLiteParameter("@StandId", standId),
- new SQLiteParameter("@StationId",stationId)
- };
- try
- {
- // 执行 SQL 删除操作
- int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
- // 返回是否成功删除
- return rowsAffected > 0;
- }
- catch (Exception ex)
- {
- // 处理异常(如果有的话)
- Console.WriteLine("Error deleting data: " + ex.Message);
- return false;
- }
- }
- public static bool UpdateTStation(TStation tStation)
- {
- // 定义更新的 SQL 语句
- string sql = @"
- UPDATE t_station
- SET
- station_name = @StationName,
- meter_type = @MeterType,
- bright_val = @BrightVal,
- flow_rate = @FlowRate,
- dial_region = @DialRegion,
- num_count = @NumCount,
- ind_count = @IndCount,
- feature_region = @FeatureRegion,
- last_unit = @LastUnit,
- last_value = @LastValue,
- last_time = @LastTime
- WHERE id = @Id";
- // 创建 SQL 参数
- SQLiteParameter[] parameters = new SQLiteParameter[]
- {
- //new SQLiteParameter("@StationId", tStation.StationId),
- new SQLiteParameter("@Id",tStation.Id),
- new SQLiteParameter("@StationName", tStation.StationName),
- new SQLiteParameter("@MeterType", tStation.MeterType),
- new SQLiteParameter("@BrightVal", tStation.BrightVal),
- new SQLiteParameter("@FlowRate", tStation.FlowRate),
- new SQLiteParameter("@DialRegion", tStation.DialRegion),
- new SQLiteParameter("@NumCount", tStation.NumCount),
- new SQLiteParameter("@IndCount", tStation.IndCount),
- new SQLiteParameter("@FeatureRegion", tStation.FeatureRegion),
- new SQLiteParameter("@LastUnit", tStation.LastUnit),
- new SQLiteParameter("@LastValue", tStation.LastValue),
- new SQLiteParameter("@LastTime", tStation.LastTime)
- };
- try
- {
- // 调用 SQLiteHelper 执行更新操作
- int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
- // 如果更新成功,返回 true,否则返回 false
- return rowsAffected > 0;
- }
- catch (Exception ex)
- {
- // 处理异常(如果有的话)
- Console.WriteLine("Error updating data: " + ex.Message);
- return false;
- }
- }
- public static bool ClearTStationTable()
- {
- // 构建清空表的 SQL 语句
- string sql = "DELETE FROM t_station";
- try
- {
- // 调用 SQLiteHelper 执行清空操作
- SQLiteHelper.ExecuteSql(sql, null);
- // 返回 true 表示操作成功
- return true;
- }
- catch (Exception ex)
- {
- // 处理异常(如果有的话)
- Console.WriteLine("Error clearing table: " + ex.Message);
- return false;
- }
- }
- //-----------------------------------------------------
- }
- }
|