Codesmith Generate SQL Server View entity class script/corresponding build template

Source: Internet
Author: User
Tags string format

C # generates an entity class for SQL views

Using System;
Using System.Text;
Using Codesmith.engine;
Using Schemaexplorer;
Using System.ComponentModel;
Using System.Data;

Namespace Common.data
{
<summary>
Codesmith entity class scripts that generate SQL Server views
</summary>
public class Viewutility
{

Get Columns Info by TableName
Public viewcolumnschemacollection getcolumncollectionbytable (viewschema table)
{
viewcolumnschemacollection columns = new viewcolumnschemacollection (table. Columns);
return columns;
}

Get CamelCase Name,such as Customer,
public string Getcamelcasename (String str)
{
Return str. Substring (0, 1). ToUpper () + str. Substring (1);
}

Get, user,private const String User_field = "User"
public string getmemberconstantdeclarationstatement (Columnschema column)
{
Return Getmemberconstantdeclarationstatement ("Public Const String", column);
}

such as public const String user_table = "USER"
public string gettableconstantdeclarationstatement (Viewschema table)
{
Return Getmemberconstantdeclarationstatement ("Public const String", table);
}
Suck as User_table
public string getupperstatement (Viewschema table)
{
return table. Name.toupper () + "_table";
}
Suck as User_field
public string getupperstatement (Columnschema column)
{
Return column. Name.toupper () + "_field";
}

such as User_table = "USER"
public string Getmemberconstantdeclarationstatement (string protectionlevel, Viewschema table)
{
return ProtectionLevel + getupperstatement (table) + "=" + getcamelcasename (table. Name) + "";
}

such as Userid_field = "USERID"
public string Getmemberconstantdeclarationstatement (string protectionlevel, Columnschema column)
{
return ProtectionLevel + getupperstatement (column) + "=" + getcamelcasename (column. Name) + "";
}

public string Getcsharpvariabletype (Viewcolumnschema column)
{
Switch (column. DataType)
{
Case DbType.AnsiString:return "string";
Case DbType.AnsiStringFixedLength:return "string";
Case DbType.Binary:return "byte[]";
Case DbType.Boolean:return "BOOL";
Case DbType.Byte:return "int";
Case DbType.Currency:return "decimal";
Case DbType.Date:return "Datatime";
Case DbType.DateTime:return "DateTime";
Case DbType.Decimal:return "Decimal";
Case DbType.Double:return "Double";
Case DbType.Guid:return "Guid";
Case DbType.Int16:return "short";
Case DbType.Int32:return "int";
Case DbType.Int64:return "Long";
Case DbType.Object:return "Object";
Case DbType.SByte:return "SByte";
Case DbType.Single:return "float";
Case DbType.String:return "String";
Case DbType.StringFixedLength:return "string";
Case DbType.Time:return "TimeSpan";
Case DbType.UInt16:return "ushort";
Case DbType.UInt32:return "UINT";
Case DbType.UInt64:return "ULONG";
Case DbType.VarNumeric:return "decimal";
}

return null;
}

public string Getcsharpbasetype (Viewcolumnschema column)
{
Switch (column. DataType)
{
Case DbType.AnsiString:return "System.String";
Case DbType.AnsiStringFixedLength:return "System.String";
Case DbType.Binary:return "system.byte[]";
Case DbType.Boolean:return "System.Boolean";
Case DbType.Byte:return "System.Int32";
Case DbType.Currency:return "System.Decimal";
Case DbType.Date:return "System.datatime";
Case DbType.DateTime:return "System.datatime";
Case DbType.Decimal:return "System.Decimal";
Case DbType.Double:return "System.Double";
Case DbType.Guid:return "System.Guid";
Case DbType.Int16:return "System.Int16";
Case DbType.Int32:return "System.Int32";
Case DbType.Int64:return "System.Int64";
Case DbType.Object:return "System.Object";
Case DbType.SByte:return "System.SByte";
Case DbType.Single:return "System.Single";
Case DbType.String:return "System.String";
Case DbType.StringFixedLength:return "System.String";
Case DbType.Time:return "System.TimeSpan";
Case DbType.UInt16:return "System.UInt16";
Case DbType.UInt32:return "System.UInt32";
Case DbType.UInt64:return "System.UInt64";
Case DbType.VarNumeric:return "System.Decimal";
}
return null;
}
}
}

The corresponding. CST template

<%@ codetemplate language= "C #" responseencoding= "UTF-8" targetlanguage= "T-SQL" description= "generates a stored procedure that updates the view."%>
<%@ Assembly name= "Schemaexplorer"%>
<%@ Import namespace= "Schemaexplorer"%>
<%@ Property Name= "SourceTable" type= "Schemaexplorer.viewschema" category= "Context" description= "The view name of the stored procedure operation."% >
<%@ Assembly src= "ViewUtility.cs"%>
<%@ Import namespace= "Common.data"%>
<script runat= "Template" >
public string formattable (string param)
{
String[] P =param. Split ('_');
Return p[p.length-1]. ToString ();
}
public string Replacetableprex (string param)
{
return param. Replace ("Fw_", "" ");
}
public string Format (string param1)
{
String Param= "";
Switch (param1)
{
Case "System.String":
{
param = "convert.tostring";
Break
}
Case "System.DateTime":
{
param = "Convert.todatetime";
Break
}
Case "System.Decimal":
{
Param= "Convert.todecimal";
Break
}
Case "System.Boolean":
{
Param= "Convert.toboolean";
Break
}
Default
{
param = "Convert.ToInt32";
Break
}

}
return param;

}
public string Formatvalue (string param1)
{
String Param= "";
Switch (param1)
{
Case "System.String":
{
param = "\" \ "";
Break
}
Case "System.DateTime":
{
param = "DateTime.Now";
Break
}
Case "System.Decimal":
{
param= "Decimal.Parse (\" 0.00\ ")";
Break
}
Case "System.Boolean":
{
Param= "false";
Break
}
Default
{
param = "0";
Break
}

}
return param;
}
Viewutility rule=new viewutility ();
</script>
Using System;
Using Com.enkj.kernel;

Namespace Com.enkj.SongHe.Model
{
<summary>
Read the database corresponding view ' <%= sourcetable.name%> '
</summary>
[Serializable]
public class <%=formattable (Sourcetable.name)%>:ientity
{
<summary>
Table name
</summary>
Public CONST STRING table_name = "<%=SourceTable.Name%>";

<summary>
All field collections in the table
</summary>
Public Const string all_filed = @ "<%for (int i=0;i<sourcetable.columns.count;i++) {%><%if (i< sourcetable.columns.count-1) {%>[<%=sourcetable.columns[i]. Name%>],<%}else{%>[<%=sourcetable.columns[i]. Name%>]<%}%><%}%> ";


<%for (int i=0;i<sourcetable.columns.count;i++) {%>
<summary>
<%=sourcetable.columns[i]. description%> Field
</summary>
Public const string Fld_<%=sourcetable.columns[i]. Name%>= "<%=sourcetable.columns[i". Name%> ";
<summary>
<%=sourcetable.columns[i]. description%> parameter fields
</summary>
Public const string Far_<%=sourcetable.columns[i]. Name%>= "@<%=sourcetable.columns[i". Name%> ";
Private <%=sourcetable.columns[i]. Systemtype%> _<%=sourcetable.columns[i]. Name%>=<%=formatvalue (Sourcetable.columns[i]. Systemtype.tostring ())%>;

<summary>
<%=sourcetable.columns[i]. Description%>
</summary>
Public <%=sourcetable.columns[i]. Systemtype%> <%=sourcetable.columns[i]. Name%>
{
get {return _<%=sourcetable.columns[i]. name%>; }
set {_<%=sourcetable.columns[i]. name%> = value; }
}
<%}%>


<summary>
No parameter constructor
</summary>
Public <%=formattable (Sourcetable.name)%> ()
{
}
<summary>
Instantiating an object from a DataRow
</summary>
<param name= "_datarow" > Row Records </param>
public override void Setdatarow (System.Data.DataRow _datarow)
{
<%for (int i=0;i<sourcetable.columns.count;i++) {%>
Try
{
if (_datarow.table.columns.contains (fld_<%=sourcetable.columns[i). name%>))
This.<%=sourcetable.columns[i]. name%> = <%=format (Sourcetable.columns[i]. Systemtype.tostring ())%> (_datarow[fld_<%=sourcetable.columns[i]. Name%>]);
}
catch (Exception ex)
{
throw new Exception ("<%=sourcetable.name%>::setdatarow: statement: this.<%=sourcetable.columns[i]. name%> = <%=format (Sourcetable.columns[i]. Systemtype.tostring ())%> (_datarow[fld_<%=sourcetable.columns[i]. Name%>]); Conversion failed ");
}
<%}%>

}
}
}

Codesmith Generate SQL Server View entity class script/corresponding build template

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.