Open start -- all programs --- codesmith professional 5.0.1.4983 -- codesmith Explorer
The name of the new connection to the database to be operated is customized As DB
Open start -- all programs --- codesmith professional 5.0.1.4983 -- codesmith Studio
Create a new template file businessobjctfortable. CST (the template file can be built and run)
Create a new file tableutility. CS (the database field is mapped to the vs2005 data type, and the namespace is consistent with the namespace above)
Copy the following code and click tools/build/run to generate the object class file of the data table.
Note that the template businessobjctfortable. name of the data table generated by CST. the namespace of the CS "object class file is common. model and the called (tableutility. CS) class file namespace common. whether data exists. it must exist in the vs2005 project or be changed as needed.
The businessobjctfortable. CST code is as follows:
<% @ Codetemplate Language = "C #" responseencoding = "UTF-8" targetlanguage = "T-SQL" Description = "generating stored procedures for updating a data table." %>
<% @ Assembly name = "schemaexplorer" %>
<% @ Import namespace = "schemaexplorer" %>
<% @ Property name = "sourcetable" type = "schemaexplorer. tableschema" Category = "context" Description = "Name of the data table for Stored Procedure operations." %>
<% @ Assembly src = "tableutility. cs" %>
<% @ Import namespace = "common. Data" %>
<SCRIPT runat = "template"> tableutility rule = new tableutility (); </SCRIPT>
Using system;
Using system. collections;
Using system. Collections. Generic;
Using system. Data;
Namespace common. Model
{
/// <Summary>
/// Read the data table '<% = sourcetable. name %>' of the database to generate an object class.
/// </Summary>
[Serializable]
Public class <% = sourcetable. name %>
{
# Region private member
<% For (INT I = 0; I <sourcetable. Columns. Count; I ++) {%>
Private <% = rule. getcsharpvariabletype (sourcetable. Columns [I]) %> _ <% = sourcetable. Columns [I]. name %>;
<% }%>
# Endregion
# Region Constructor
Public <% = sourcetable. name %> (){}
Public <% = sourcetable. name %> (
<% For (INT I = 0; I <sourcetable. Columns. Count-1; I ++) {%>
<% = Rule. getcsharpvariabletype (sourcetable. Columns [I]) %> <% = sourcetable. Columns [I]. name %>,
<% }%>
<% = Rule. getcsharpvariabletype (sourcetable. columns [sourcetable. columns. count-1]) %> <% = sourcetable. columns [sourcetable. columns. count-1]. name %>
)
{
<% For (INT I = 0; I <sourcetable. Columns. Count; I ++) {%>
_ <% = Sourcetable. Columns [I]. name % >=< % = sourcetable. Columns [I]. name %>;
<% }%>
}
# Endregion
# Region public properties
<% For (INT I = 0; I <sourcetable. Columns. Count; I ++) {%>
Public <% = rule. getcsharpvariabletype (sourcetable. Columns [I]) %> <% = rule. getcamelcasename (sourcetable. Columns [I]. Name) %>
{
Get {return _ <% = sourcetable. Columns [I]. name %> ;}
Set {_ <% = sourcetable. Columns [I]. name %> = value ;}
}
<% }%>
# Endregion
}
}
The Code of tableutility. CS is as follows:
Using system;
Using system. text;
Using codesmith. engine;
Using schemaexplorer;
Using system. componentmodel;
Using system. Data;
Namespace common. Data
{
//// <Summary>
/// Codesmith generates the SQL server data table entity class call script
/// </Summary>
Public class tableutility
{
// Get columns info by tablename
Public columnschemacollection getcolumncollectionbytable (tableschema table)
{
Columnschemacollection columns = new columnschemacollection (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 (tableschema table)
{
Return getmemberconstantdeclarationstatement ("Public const string", table );
}
// Suck as user_table
Public String getupperstatement (tableschema 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, tableschema 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 (columnschema 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 (columnschema 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;
}
}
}