using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace SyntacticSugar
{
/// summary>
/// ** 描述:實(shí)體生成類
/// ** 創(chuàng)始時(shí)間:2015-4-17
/// ** 修改時(shí)間:-
/// ** 作者:sunkaixuan
/// ** qq:610262374 歡迎交流,共同提高 ,命名語法等寫的不好的地方歡迎大家的給出寶貴建議
/// /summary>
public class ClassGenerating
{
/// summary>
/// 根據(jù)匿名類獲取實(shí)體類的字符串
/// /summary>
/// param name="entity">匿名對(duì)象/param>
/// param name="className">生成的類名/param>
/// returns>/returns>
public static string DynamicToClass(object entity, string className)
{
StringBuilder reval = new StringBuilder();
StringBuilder propertiesValue = new StringBuilder();
var propertiesObj = entity.GetType().GetProperties();
string replaceGuid = Guid.NewGuid().ToString();
string nullable = string.Empty;
foreach (var r in propertiesObj)
{
var type = r.PropertyType;
if (type.IsGenericType type.GetGenericTypeDefinition() == typeof(Nullable>))
{
type = type.GetGenericArguments()[0];
nullable = "?";
}
if (!type.Namespace.Contains("System.Collections.Generic"))
{
propertiesValue.AppendLine();
string typeName = ChangeType(type);
propertiesValue.AppendFormat("public {0}{3} {1} {2}", typeName, r.Name, "{get;set;}", nullable);
propertiesValue.AppendLine();
}
}
reval.AppendFormat(@"
public class {0}{{
{1}
}}
", className, propertiesValue);
return reval.ToString();
}
/// summary>
/// 根據(jù)DataTable獲取實(shí)體類的字符串
/// /summary>
/// param name="sql">/param>
/// param name="className">/param>
/// returns>/returns>
public static string DataTableToClass(DataTable dt, string className)
{
StringBuilder reval = new StringBuilder();
StringBuilder propertiesValue = new StringBuilder();
string replaceGuid = Guid.NewGuid().ToString();
foreach (DataColumn r in dt.Columns)
{
propertiesValue.AppendLine();
string typeName = ChangeType(r.DataType);
propertiesValue.AppendFormat("public {0} {1} {2}", typeName, r.ColumnName, "{get;set;}");
propertiesValue.AppendLine();
}
reval.AppendFormat(@"
public class {0}{{
{1}
}}
", className, propertiesValue);
return reval.ToString();
}
/// summary>
/// 根據(jù)SQL語句獲取實(shí)體類的字符串
/// /summary>
/// param name="sql">SQL語句/param>
/// param name="className">生成的類名/param>
/// param name="server">服務(wù)名/param>
/// param name="database">數(shù)據(jù)庫名稱/param>
/// param name="uid">賬號(hào)/param>
/// param name="pwd">密碼/param>
/// returns>/returns>
public static string SqlToClass(string sql, string className, string server, string database, string uid, string pwd)
{
using (SqlConnection conn = new SqlConnection(string.Format("server={0};uid={2};pwd={3};database={1}", server, database, uid, pwd)))
{
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = sql;
DataTable dt = new DataTable();
SqlDataAdapter sad = new SqlDataAdapter(command);
sad.Fill(dt);
var reval = DataTableToClass(dt, className);
return reval;
}
}
/// summary>
/// 根據(jù)SQL語句獲取實(shí)體類的字符串
/// /summary>
/// param name="sql">SQL語句/param>
/// param name="className">生成的類名/param>
/// param name="connName">webconfig的connectionStrings name/param>
/// returns>/returns>
public static string SqlToClass(string sql, string className, string connName)
{
string connstr = System.Configuration.ConfigurationManager.ConnectionStrings[connName].ToString();
if (connstr.Contains("metadata"))//ef
connstr = Regex.Match(connstr, @"connection string\=""(.+)""").Groups[1].Value;
using (SqlConnection conn = new SqlConnection(connstr))
{
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = sql;
DataTable dt = new DataTable();
SqlDataAdapter sad = new SqlDataAdapter(command);
sad.Fill(dt);
var reval = DataTableToClass(dt, className);
return reval;
}
}
/// summary>
/// 匹配類型
/// /summary>
/// param name="type">/param>
/// returns>/returns>
private static string ChangeType(Type type)
{
string typeName = type.Name;
switch (typeName)
{
case "Int32": typeName = "int"; break;
case "String": typeName = "string"; break;
}
return typeName;
}
}
}
//通過匿名對(duì)象生成實(shí)體類
var dynamicObj = new { id = 1, name = "小名", entity = new enityt1() };
//注意:只能是單個(gè)實(shí)體不能傳入 ListT> ,集合需要 List[0]
string classCode = ClassGenerating.DynamicToClass(dynamicObj, "classDynamic");
//通過datatable生成實(shí)體類
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name");
classCode = ClassGenerating.DataTableToClass(dt, "classTatabale");
//通過sql語句生成實(shí)體類
classCode = ClassGenerating.SqlToClass("select * from note", "Note", "127.0.0.1", "MyWork", "sa", "sasa");
classCode = ClassGenerating.SqlToClass("select * from dbo.AccessoriesDetail", "AccessoriesDetail", "NFDEntities");//通過 config connstring名稱