Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmey-Jiang committed Jan 4, 2018
1 parent 8d89a98 commit ff08de3
Show file tree
Hide file tree
Showing 27 changed files with 707 additions and 0 deletions.
Binary file modified Utility基础类大全/.vs/Common.Utility/v14/.suo
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
10 changes: 10 additions & 0 deletions Utility基础类大全/Common.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
<Compile Include="Chart图形\OWCChart11.cs" />
<Compile Include="cmd\RunCmd.cs" />
<Compile Include="CSV文件转换\CsvHelper.cs" />
<Compile Include="DataTable转实体\DataTableExtensions.cs" />
<Compile Include="DataTable转实体\ListExtensions.cs" />
<Compile Include="EcanConvertToCh.cs" />
<Compile Include="FormulaExpress.cs" />
<Compile Include="GridViewHelper.cs" />
Expand Down Expand Up @@ -141,6 +143,14 @@
<Compile Include="ResourceManager\Resources.cs" />
<Compile Include="RMB.cs" />
<Compile Include="SegList.cs" />
<Compile Include="SQL语句拦截器\BaseConfig.cs" />
<Compile Include="SQL语句拦截器\DbDistributor.cs" />
<Compile Include="SQL语句拦截器\DbGroupConfiguration.cs" />
<Compile Include="SQL语句拦截器\DbNodeContext.cs" />
<Compile Include="SQL语句拦截器\DefaultWeight.cs" />
<Compile Include="SQL语句拦截器\FileSerialize.cs" />
<Compile Include="SQL语句拦截器\SqlInterceptor.cs" />
<Compile Include="SQL语句拦截器\WeightObject.cs" />
<Compile Include="SysHelper.cs" />
<Compile Include="Tools.cs" />
<Compile Include="URL的操作类\UrlOper.cs" />
Expand Down
109 changes: 109 additions & 0 deletions Utility基础类大全/DataTable转实体/DataTableExtensions.cs
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;
//}
}
}
81 changes: 81 additions & 0 deletions Utility基础类大全/DataTable转实体/ListExtensions.cs
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;
}
}
}
51 changes: 51 additions & 0 deletions Utility基础类大全/SQL语句拦截器/BaseConfig.cs
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);
}
}
}
33 changes: 33 additions & 0 deletions Utility基础类大全/SQL语句拦截器/DbDistributor.cs
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();
}
}
}
52 changes: 52 additions & 0 deletions Utility基础类大全/SQL语句拦截器/DbGroupConfiguration.cs
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
{
}
}
Loading

0 comments on commit ff08de3

Please sign in to comment.