|
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>
/// ** Description: entity generation class
/// ** Founding Date:
/// ** Modification time :-
/// ** Prepared by sunkaixuan
/// ** Qq: 610262374 welcome to the exchange, joint improvement, naming syntax, and other poorly written areas. You are welcome to give valuable suggestions.
/// </Summary>
Public class ClassGenerating
{
/// <Summary>
/// Obtain the object class String Based on the anonymous class
/// </Summary>
/// <Param name = "entity"> anonymous object </param>
/// <Param name = "className"> generated class name </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>
/// Obtain the object class String Based on the DataTable
/// </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>
/// Obtain the object class String Based on the SQL statement
/// </Summary>
/// <Param name = "SQL"> SQL statement </param>
/// <Param name = "className"> generated class name </param>
/// <Param name = "server"> service name </param>
/// <Param name = "database"> database name </param>
/// <Param name = "uid"> account </param>
/// <Param name = "pwd"> password </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>
/// Obtain the object class String Based on the SQL statement
/// </Summary>
/// <Param name = "SQL"> SQL statement </param>
/// <Param name = "className"> generated class name </param>
/// <Param name = "connName"> connectionStrings name of webconfig </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>
/// Matching type
/// </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;
}
}
}
|