forked from laochiangx/Common.Utility
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jimmey-Jiang
committed
Jan 4, 2018
1 parent
8d89a98
commit ff08de3
Showing
27 changed files
with
707 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Common.Utilities | ||
{ | ||
public static class DataTableExtensions | ||
{ | ||
public static T ToEntity<T>(this DataTable table) where T : new() | ||
{ | ||
T entity = new T(); | ||
foreach (DataRow row in table.Rows) | ||
{ | ||
foreach (var item in entity.GetType().GetProperties()) | ||
{ | ||
if (row.Table.Columns.Contains(item.Name)) | ||
{ | ||
if (DBNull.Value != row[item.Name]) | ||
{ | ||
Type newType = item.PropertyType; | ||
//判断type类型是否为泛型,因为nullable是泛型类, | ||
if (newType.IsGenericType | ||
&& newType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类 | ||
{ | ||
//如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换 | ||
System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(newType); | ||
//将type转换为nullable对的基础基元类型 | ||
newType = nullableConverter.UnderlyingType; | ||
} | ||
|
||
item.SetValue(entity, Convert.ChangeType(row[item.Name], newType), null); | ||
|
||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
return entity; | ||
} | ||
|
||
public static List<T> ToEntities<T>(this DataTable table) where T : new() | ||
{ | ||
List<T> entities = new List<T>(); | ||
if (table == null) | ||
return null; | ||
foreach (DataRow row in table.Rows) | ||
{ | ||
T entity = new T(); | ||
foreach (var item in entity.GetType().GetProperties()) | ||
{ | ||
if (table.Columns.Contains(item.Name)) | ||
{ | ||
if (DBNull.Value != row[item.Name]) | ||
{ | ||
Type newType = item.PropertyType; | ||
//判断type类型是否为泛型,因为nullable是泛型类, | ||
if (newType.IsGenericType | ||
&& newType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类 | ||
{ | ||
//如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换 | ||
System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(newType); | ||
//将type转换为nullable对的基础基元类型 | ||
newType = nullableConverter.UnderlyingType; | ||
} | ||
item.SetValue(entity, Convert.ChangeType(row[item.Name], newType), null); | ||
} | ||
} | ||
} | ||
entities.Add(entity); | ||
} | ||
return entities; | ||
} | ||
|
||
|
||
// public static dynamic ListToTranslate(this DataTable sender) | ||
//{ | ||
// var result = new System.Collections.Generic.List(); | ||
// if (string.IsNullOrWhiteSpace(sender.Namespace) || string.IsNullOrWhiteSpace(sender.TableName)) | ||
// throw new Exception("Namespace or TableName is NullOrWhiteSpace"); | ||
// var typeStr = ""; | ||
// foreach (DataColumn cloumn in sender.Columns) | ||
// { | ||
// typeStr += " public string @2{set;get;}## ##".Replace("@2", cloumn.ColumnName);//Replace("@1", cloumn.DataType.Name). | ||
// } | ||
// typeStr = "namespace @1##{## public class @2## {##@3## }##}".Replace("@1", sender.Namespace).Replace("@2", sender.TableName).Replace("@3", typeStr).Replace("##", "\r\n"); | ||
// var cr = new CSharpCodeProvider().CompileAssemblyFromSource(new CompilerParameters(new string[] { "System.dll" }), typeStr); | ||
// var type = cr.CompiledAssembly.GetType(string.Format("{0}.{1}", sender.Namespace, sender.TableName)); | ||
// var properties = type.GetProperties(); | ||
|
||
// foreach (DataRow row in sender.Rows) | ||
// { | ||
// var dm = Activator.CreateInstance(type); | ||
// foreach (DataColumn cloumn in sender.Columns) | ||
// { | ||
// var property = properties.FirstOrDefault(l => IsEnter(l.Name, cloumn.ColumnName)); | ||
// if (property != null) | ||
// { | ||
// property.SetValue(dm, row[cloumn].ToString()); | ||
// } | ||
// } | ||
// result.Add(dm); | ||
// } | ||
// return result; | ||
//} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace Common.Utilities | ||
{ | ||
public static class ListExtensions | ||
{ | ||
/// <summary> | ||
/// 将指定的集合转换成DataTable。 | ||
/// </summary> | ||
/// <param name="list">将指定的集合。</param> | ||
/// <returns>返回转换后的DataTable。</returns> | ||
public static DataTable ToDataTable(this IList list) | ||
{ | ||
DataTable table = new DataTable(); | ||
if (list.Count > 0) | ||
{ | ||
PropertyInfo[] propertys = list[0].GetType().GetProperties(); | ||
foreach (PropertyInfo pi in propertys) | ||
{ | ||
Type pt = pi.PropertyType; | ||
if ((pt.IsGenericType) && (pt.GetGenericTypeDefinition() == typeof(Nullable<>))) | ||
{ | ||
pt = pt.GetGenericArguments()[0]; | ||
} | ||
table.Columns.Add(new DataColumn(pi.Name, pt)); | ||
} | ||
|
||
for (int i = 0; i < list.Count; i++) | ||
{ | ||
ArrayList tempList = new ArrayList(); | ||
foreach (PropertyInfo pi in propertys) | ||
{ | ||
object obj = pi.GetValue(list[i], null); | ||
tempList.Add(obj); | ||
} | ||
object[] array = tempList.ToArray(); | ||
table.LoadDataRow(array, true); | ||
} | ||
} | ||
return table; | ||
} | ||
|
||
public static DataTable ToDataTable<T>(this List<T> list) | ||
{ | ||
DataTable table = new DataTable(); | ||
//创建列头 | ||
PropertyInfo[] propertys = typeof(T).GetProperties(); | ||
foreach (PropertyInfo pi in propertys) | ||
{ | ||
Type pt = pi.PropertyType; | ||
if ((pt.IsGenericType) && (pt.GetGenericTypeDefinition() == typeof(Nullable<>))) | ||
{ | ||
pt = pt.GetGenericArguments()[0]; | ||
} | ||
table.Columns.Add(new DataColumn(pi.Name, pt)); | ||
} | ||
//创建数据行 | ||
if (list.Count > 0) | ||
{ | ||
for (int i = 0; i < list.Count; i++) | ||
{ | ||
ArrayList tempList = new ArrayList(); | ||
foreach (PropertyInfo pi in propertys) | ||
{ | ||
object obj = pi.GetValue(list[i], null); | ||
tempList.Add(obj); | ||
} | ||
object[] array = tempList.ToArray(); | ||
table.LoadDataRow(array, true); | ||
} | ||
} | ||
return table; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Common.Utilities | ||
{ | ||
public abstract class BaseConfig<TConfig> | ||
{ | ||
private TConfig _current; | ||
|
||
public TConfig Current | ||
{ | ||
get | ||
{ | ||
if (_current == null) | ||
{ | ||
string filePath = GetFilePath(); | ||
if (!string.IsNullOrEmpty(filePath) | ||
&& File.Exists(filePath)) | ||
{ | ||
_current = FileSerialize.SerializerXml<TConfig>(filePath); | ||
} | ||
} | ||
return _current; | ||
} | ||
} | ||
|
||
public virtual string GetRootPath() | ||
{ | ||
string path = ConfigurationManager.AppSettings.Get("ConfigPath"); | ||
|
||
if (string.IsNullOrEmpty(path)) | ||
{ | ||
path = AppDomain.CurrentDomain.BaseDirectory + "\\Config"; | ||
} | ||
return path; | ||
} | ||
|
||
public abstract string GetPathName(); | ||
|
||
public virtual string GetFilePath() | ||
{ | ||
string path = GetRootPath(); | ||
string pathName = GetPathName(); | ||
return string.Format("{0}\\{1}", path, pathName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Common; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Common.Utilities | ||
{ | ||
internal class DbDistributor | ||
{ | ||
/// <summary> | ||
/// 切换到主库,此方法适用于SQL自动查询服务端版本用 | ||
/// 系统自动会使用 select cast(serverproperty('EngineEdition') as int) 来查询 | ||
/// </summary> | ||
/// <param name="conn"></param> | ||
public static void UpdateToMaster(DbConnection conn) | ||
{ | ||
//UpdateToServer(conn, DbNodeType.Master); | ||
} | ||
static void UpdateToServer(DbConnection conn, DbNodeType type) | ||
{ | ||
//bool exist = false; | ||
//DbNode node = DbNodeContext.Current.Pop(type, conn.ConnectionString, ref exist); | ||
//ConsoleHelper.WriteLineWithThreadGreen("数据库服务器切换:" + node.Name + (exist ? "检测到" + type.ToString() + "连接已切换过" : "检测到" + type.ToString() + "连接未切换过")); | ||
//DbDistributor.UpdateConnectionString(conn, node.ConnectionString); | ||
} | ||
|
||
internal static void UpdateToSlave(DbConnection connection) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Xml.Serialization; | ||
|
||
namespace Common.Utilities | ||
{ | ||
public class DbGroupConfiguration : BaseConfig<DbGroupConfiguration> | ||
{ | ||
|
||
public static DbGroupConfiguration Instance = new DbGroupConfiguration(); | ||
|
||
|
||
public override string GetPathName() | ||
{ | ||
return "DbGroup.config"; | ||
} | ||
|
||
[XmlElement] | ||
public DbGroup DbGroup { get; set; } | ||
} | ||
public class DbGroup | ||
{ | ||
[XmlElement(ElementName = "DbNode")] | ||
public List<DbNode> DbNodes { get; set; } | ||
[XmlAttribute] | ||
public int Interval { get; set; } //侦测的时间间隔 | ||
} | ||
public class DbNode | ||
{ | ||
[XmlAttribute] | ||
public string Name { get; set; } | ||
[XmlAttribute] | ||
public string ConnectionString { get; set; } | ||
public DbNodeState State { get; set; } | ||
[XmlAttribute] | ||
public DbNodeType NodeType { get; set; } | ||
[XmlAttribute] | ||
public string ProviderName { get; set; } //数据提供驱动 | ||
public DateTime LastModifyTime { get; set; } //最后一次侦测时间 | ||
|
||
} | ||
|
||
public class DbNodeType | ||
{ | ||
} | ||
|
||
public class DbNodeState | ||
{ | ||
} | ||
} |
Oops, something went wrong.