<%@ CodeTemplate Language="C#" TargetLanguage="C#"
Description="" %> <%@ Assembly
Name="SchemaExplorer" %> <%@ Assembly
Name="CodeSmith.CustomProperties"%> <%@ Import
Namespace="SchemaExplorer" %> <%@ Property
Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context"
Description="" %> <%@ Property Name="NameSpace"
Type="String" Category="Context" Description=""
%> <%@ Property Name="IsListAll"
Type="System.Boolean" Category="Context" Description=""
%>
<script runat="template"> public
string GetType(ColumnSchema column) { switch
(column.DataType){ case DbType.Int16: return
"short"; case DbType.Int32: return
"int"; case DbType.Int64: return
"long"; } return ""; } public
string GetDbType(ColumnSchema column) { switch
(column.DataType){ case DbType.Int16: return
"SmallInt"; case DbType.Int32: return
"Int"; case DbType.Int64: return
"BigInt"; } return
""; } public string GetAllType(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
"DateTime"; 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
"DateTime"; case DbType.UInt16: return
"ushort"; case DbType.UInt32: return
"uint"; case DbType.UInt64: return
"ulong"; case DbType.VarNumeric: return
"decimal"; } return
""; } public string GetConvertType(ColumnSchema
column) { switch (column.DataType)
{ case DbType.AnsiString: return
"Convert.ToString"; case
DbType.AnsiStringFixedLength: return "Convert.ToString";
case DbType.Binary: return "Convert.ToByte";
case DbType.Boolean: return
"Convert.ToBoolean"; case DbType.Byte: return
"Convert.ToInt32"; case DbType.Currency:
return "Convert.ToDecimal"; case DbType.Date:
return "Convert.DateTime"; case
DbType.DateTime: return "Convert.DateTime";
case DbType.Decimal: return
"Convert.ToDecimal"; case DbType.Double:
return "Convert.ToDouble"; case DbType.Guid:
return "Convert.ToString"; case DbType.Int16:
return "Convert.ToInt16"; case DbType.Int32:
return "Convert.ToInt32"; case DbType.Int64:
return "Convert.ToInt64"; case DbType.Object:
return "Convert.ToString"; case DbType.SByte:
return "Convert.ToByte"; case DbType.Single:
return "Convert.ToInt32"; case DbType.String:
return "Convert.ToString"; case
DbType.StringFixedLength: return "Convert.ToString";
case DbType.Time: return "Convert.DateTime";
case DbType.UInt16: return "Convert.ToUInt16";
case DbType.UInt32: return "Convert.ToUInt32";
case DbType.UInt64: return "Convert.ToUInt64";
case DbType.VarNumeric: return
"Convert.ToDecimal"; } return
""; } public string GetAllDbType(ColumnSchema
column) { string re=""; switch
(column.NativeType.ToString().ToLower()) { case
"bigint":re="BigInt";break; case
"binary":re="Binary";break; case
"varbinary":re="VarBinary";break; case "char":re="Char";break;
case "carchar":re="VarChar";break; case
"nchar":re="NChar";break; case
"nvarchar":re="NVarChar";break; case
"varchar":re="VarChar";break; case
"datetime":re="DateTime";break; case
"smalldatetime":re="SmallDateTime";break; case
"decimal":re="Decimal";break; case
"numeric":re="Numeric";break; case
"float":re="Float";break; case
"real":re="Real";break; case
"int":re="Int";break; case
"smallint":re="SmallInt";break; case
"tinyint":re="TinyInt";break; case
"money":re="Money";break; case
"smallmoney":re="SmallMoney";break; case
"bit":re="Bit";break; case
"timestamp":re="Timestamp";break; case
"uniqueidentifier":re="UniqueIdentifier";break; case
"text":re="Text";break; case
"image":re="Image";break; case
"ntext":re="NText";break; }
if (column.Size >
0&&column.NativeType.ToString().ToLower()!="ntext"&&column.NativeType.ToString().ToLower()!="text")
{ re+=","+column.Size;
} //re=column.NativeType.ToLower(); return
re; } public string GetModifyColName(ColumnSchema
column) { if(((bool)column.ExtendedProperties["CS_IsIdentity"].Value)!=true&&column.Name.EndsWith("Id")) {
return
column.Name.Substring(0,column.Name.Length-2)+"."+column.Name; } else {
return
column.Name; } } </script>
using System; using
SystemFramework; using Business; using
System.Data; using System.Data.SqlClient;
// Description: Enter summary here after
generation. // --------------------- // Copyright .
<%= DateTime.Now.Year %> Our Client //
--------------------- // History // <%=
DateTime.Now.ToString() %>
namespace <%=NameSpace
%> { public sealed class <%=
SourceTable.Name %>Provider { public static
<%= SourceTable.Name %>Collection List<%=
SourceTable.Name %>(<%if(!IsListAll){%>int
PageNum,int PageSize<%}%>) {
<%= SourceTable.Name %>Collection <%=
SourceTable.Name %>s=new <%= SourceTable.Name
%>Collection(); using(SqlConnection
conn=MSSQLProvider.GetConnection()) {
using(SqlCommand cmd=new SqlCommand("List<%= SourceTable.Name
%>",conn)) { conn.Open();
cmd.CommandType=CommandType.StoredProcedure;
<%if(!IsListAll){%>
SqlParameter pa;
pa=cmd.Parameters.Add("@PageNum",SqlDbType.Int);
pa.Value=PageNum;
pa=cmd.Parameters.Add("@PageSize",SqlDbType.Int);
pa.Value=PageSize; <%}%>
SqlDataReader
dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
<%= SourceTable.Name %> obj;
while(dr.Read()) { obj=new <%=
SourceTable.Name %>(); //TODO:insert your code
for read list columns <%= SourceTable.Name
%>s.Add(obj); }
dr.Close(); } }
return <%= SourceTable.Name %>s; }
public static <%= SourceTable.Name %>
Get<%= SourceTable.Name
%>(<%=GetType(SourceTable.PrimaryKey.MemberColumns[0])%>
id) { <%= SourceTable.Name %>
obj=null; using(SqlConnection
conn=MSSQLProvider.GetConnection()) {
using(SqlCommand cmd=new SqlCommand("Get<%= SourceTable.Name
%>",conn)) { conn.Open();
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter pa;
pa=cmd.Parameters.Add("@id",SqlDbType.<%=GetDbType(SourceTable.PrimaryKey.MemberColumns[0])%>);
pa.Value=id; SqlDataReader
dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.Read()) { obj=new
<%= SourceTable.Name %>(); obj.<%=
SourceTable.Name %>Id=id; <% for (int i =
0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) {
%>
<%if(SourceTable.NonPrimaryKeyColumns[i].Name.EndsWith("Id")){%>
obj.<%=SourceTable.NonPrimaryKeyColumns[i].Name.Substring(0,SourceTable.NonPrimaryKeyColumns[i].Name.Length-2)%>=new
<%=SourceTable.NonPrimaryKeyColumns[i].Name.Substring(0,SourceTable.NonPrimaryKeyColumns[i].Name.Length-2)%>();
obj.<%=SourceTable.NonPrimaryKeyColumns[i].Name.Substring(0,SourceTable.NonPrimaryKeyColumns[i].Name.Length-2)%>.<%=
SourceTable.NonPrimaryKeyColumns[i].Name
%>=<%=GetConvertType(SourceTable.NonPrimaryKeyColumns[i])%>(dr["<%=
SourceTable.NonPrimaryKeyColumns[i].Name %>"]);
<%}else{%> obj.<%=
SourceTable.NonPrimaryKeyColumns[i].Name
%>=<%=GetConvertType(SourceTable.NonPrimaryKeyColumns[i])%>(dr["<%=
SourceTable.NonPrimaryKeyColumns[i].Name %>"]);
<%}%> <% } %>
}
dr.Close(); } } return obj; }
public static
<%=GetType(SourceTable.PrimaryKey.MemberColumns[0])%>
Insert<%= SourceTable.Name %>(<%= SourceTable.Name
%> obj) {
<%=GetType(SourceTable.PrimaryKey.MemberColumns[0])%>
re; using(SqlConnection
conn=MSSQLProvider.GetConnection()) {
using(SqlCommand cmd=new SqlCommand("Insert<%=
SourceTable.Name %>",conn)) {
conn.Open();
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter pa; <% for (int i = 0; i <
SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
pa=cmd.Parameters.Add("@<%=
SourceTable.NonPrimaryKeyColumns[i].Name
%>",SqlDbType.<%=GetAllDbType(SourceTable.NonPrimaryKeyColumns[i])%>);
pa.Value=obj.<%=
GetModifyColName(SourceTable.NonPrimaryKeyColumns[i])%>;
<% } %>
try {
re=<%=GetType(SourceTable.PrimaryKey.MemberColumns[0])%>.Parse(cmd.ExecuteScalar().ToString());
} catch(Exception ex) {
conn.Close(); throw ex; }
conn.Close(); } } return
re; }
public static void Update<%= SourceTable.Name
%>(<%= SourceTable.Name %> obj)
{ using(SqlConnection
conn=MSSQLProvider.GetConnection()) {
using(SqlCommand cmd=new SqlCommand("Update<%=
SourceTable.Name %>",conn)) {
conn.Open();
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter pa;
pa=cmd.Parameters.Add("@id",SqlDbType.<%=GetDbType(SourceTable.PrimaryKey.MemberColumns[0])%>);
pa.Value=obj.<%= SourceTable.Name %>Id;
<% for (int i = 0; i <
SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
pa=cmd.Parameters.Add("@<%=
SourceTable.NonPrimaryKeyColumns[i].Name
%>",SqlDbType.<%=GetAllDbType(SourceTable.NonPrimaryKeyColumns[i])%>);
pa.Value=obj.<%=
GetModifyColName(SourceTable.NonPrimaryKeyColumns[i])%>;
<% } %>
try {
cmd.ExecuteNonQuery(); }
catch(Exception ex) {
conn.Close(); throw ex; }
conn.Close(); } }
}
public static void Del<%= SourceTable.Name
%>(<%=GetType(SourceTable.PrimaryKey.MemberColumns[0])%>
id) { using(SqlConnection
conn=MSSQLProvider.GetConnection()) {
using(SqlCommand cmd=new SqlCommand("Del<%= SourceTable.Name
%>",conn)) { conn.Open();
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter pa;
pa=cmd.Parameters.Add("@id",SqlDbType.<%=GetDbType(SourceTable.PrimaryKey.MemberColumns[0])%>);
pa.Value=id;
try {
cmd.ExecuteNonQuery(); }
catch(Exception ex) {
conn.Close(); throw ex; }
conn.Close(); } } }
} } |