|
|
|
@ -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<T>(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(); |
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
|
/// 返回查询结果的首行首列 |
|
|
|
|
/// </summary> |
|
|
|
@ -219,7 +233,7 @@ namespace WomenAndChildren.Helper.SqlServer |
|
|
|
|
return returnSql; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static List<T> ConvertDataTableToEntity<T>(DataTable dt) |
|
|
|
|
public static List<T> Select<T>(DataTable dt) |
|
|
|
|
{ |
|
|
|
|
return (from DataRow row in dt.Rows select GetItem<T>(row)).ToList(); |
|
|
|
|
} |
|
|
|
@ -266,5 +280,61 @@ namespace WomenAndChildren.Helper.SqlServer |
|
|
|
|
|
|
|
|
|
return dynamicEntity; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 根据实体插入 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <typeparam name="T"></typeparam> |
|
|
|
|
/// <param name="list">实体插入类型,实体类型要跟数据库字段名一致</param> |
|
|
|
|
public void Insert<T>(List<T> 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(); |
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
|
/// 根据实体更新 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <typeparam name="T"></typeparam> |
|
|
|
|
/// <param name="tableName">表名</param> |
|
|
|
|
/// <param name="entity">实体类型 实体的字段名需要跟数据库字段名一致</param> |
|
|
|
|
/// <param name="keys">where 条件 </param> |
|
|
|
|
public void Update<T>(T entity, Dictionary<string, string> 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>(T entity,Dictionary<string,string> 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(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|