diff --git a/PEIS/Helper/SqlHelper.cs b/PEIS/Helper/SqlHelper.cs index 651280e..73fdf69 100644 --- a/PEIS/Helper/SqlHelper.cs +++ b/PEIS/Helper/SqlHelper.cs @@ -6,11 +6,11 @@ using System.Data.SqlClient; using System.Dynamic; using System.Linq; -namespace WomenAndChildren.Helper.SqlServer +namespace PEIS.Helper.SqlServer { public class SqlHelper { - public static readonly string ConnectString = ConfigurationManager.AppSettings["ConnectString"]; + public static readonly string ConnectString = ""; private SqlConnection _con; private SqlCommand _cmd; @@ -196,6 +196,20 @@ namespace WomenAndChildren.Helper.SqlServer _cmd.ExecuteNonQuery(); CloseDataBase(); } + + public void Run(string sql, T parameters) + { + //连接数据库 + _con.Open(); + var cmd = new SqlCommand(sql, _con); + foreach (var parameter in typeof(T).GetProperties()) + { + cmd.Parameters.AddWithValue($"@{parameter.Name}", + typeof(T).GetProperty(parameter.Name)?.GetValue(parameters, null)); + } + //获取返回数据 + cmd.ExecuteNonQuery(); + } /// /// 返回查询结果的首行首列 /// @@ -219,7 +233,7 @@ namespace WomenAndChildren.Helper.SqlServer return returnSql; } - public static List ConvertDataTableToEntity(DataTable dt) + public static List Select(DataTable dt) { return (from DataRow row in dt.Rows select GetItem(row)).ToList(); } @@ -266,5 +280,61 @@ namespace WomenAndChildren.Helper.SqlServer return dynamicEntity; } + + /// + /// 根据实体插入 + /// + /// + /// 实体插入类型,实体类型要跟数据库字段名一致 + public void Insert(List list) + { + var tableName = typeof(T).FullName; + var rowName = string.Empty; + var value = string.Empty; + foreach (var item in typeof(T).GetProperties()) + { + rowName += $"{item.Name},"; + value += $"@{item.Name},"; + } + foreach (var entity in list) + { + Run($"insert into {tableName}({rowName.Substring(0, rowName.Length - 1)}) values ({value.Substring(0, value.Length - 1)})", entity); + } + _con.Close(); + } + /// + /// 根据实体更新 + /// + /// + /// 表名 + /// 实体类型 实体的字段名需要跟数据库字段名一致 + /// where 条件 + public void Update(T entity, Dictionary keys) + { + var tableName = typeof(T).FullName; + var keyWord = string.Empty; + var value = typeof(T).GetProperties().Aggregate(string.Empty, (current, item) => current + $"{item.Name}=@{item.Name},"); + keyWord = keys.Aggregate(keyWord, (current, key) => current + $"{key.Key}=\'{key.Value}\' and"); + Run( + string.IsNullOrEmpty(keyWord) + ? $"update {tableName} set {value.Substring(0, value.Length - 1)}" + : $"update {tableName} set {value.Substring(0, value.Length - 1)} where {keyWord.Substring(0, keyWord.Length - 3)}", + entity); + _con.Close(); + } + + + public void Delete(T entity,Dictionary keys) + { + var tableName = typeof(T).FullName; + var keyWord = string.Empty; + keyWord = keys.Aggregate(keyWord, (current, key) => current + $"{key.Key}=\'{key.Value}\' and"); + Run( + string.IsNullOrEmpty(keyWord) + ? $"truncate table {tableName}" + : $"delete from {tableName} where {keyWord.Substring(0, keyWord.Length - 3)}", + entity); + _con.Close(); + } } } diff --git a/PEIS/PEIS.csproj b/PEIS/PEIS.csproj index f412fd7..893c301 100644 --- a/PEIS/PEIS.csproj +++ b/PEIS/PEIS.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + 7.1 AnyCPU @@ -30,6 +31,7 @@ TRACE prompt 4 + 7.1