CodeSmith 以前只是听说,用了一下还不错。适合我这种每天写垃圾代码的人
<%
@ CodeTemplate Language
=
"
C#
"
TargetLanguage
=
"
T-SQL
"
Description
=
"
Create a procedure which have delete function base on a table.Must use PrimaryKey to delete a record.
"
%>
<% @ Assembly Name = " SchemaExplorer " %>
<% @ Import Namespace = " SchemaExplorer " %>
<% @ Property Name = " SourceTable " Type = " SchemaExplorer.TableSchema " Category = " DataTable " Description = " Table that the stored procedures should be based on. " %>
<% @ Property Name = " Author " Type = " String " Category = " Context " Description = " The author for this procedure. " Optional = " true " %>
<% @ Property Name = " Description " Type = " String " Category = " Context " Description = " The description for this procedure. " Optional = " true " %>
< script runat ="template" >
public string GetSqlParameterStatement(ColumnSchema column)
{
string param = " @ " + column.Name + " " + column.NativeType;
switch (column.DataType)
{
case DbType.Decimal:
{
param += " ( " + column.Precision + " , " + column.Scale + " ) " ;
break ;
}
default :
{
if (column.Size > 0 )
{
param += " ( " + column.Size + " ) " ;
}
break ;
}
}
return param;
}
public string GetUpdateParameter()
{ string temp = null ;
for ( int i = 0 ;i < SourceTable.Columns.Count;i ++ )
{
if ( ! SourceTable.Columns[i].IsPrimaryKeyMember)
{
temp = temp + SourceTable.Columns[i].Name + " =@ " + SourceTable.Columns[i].Name + " , " + " \n " ;
}
}
if (temp.Length > 0 )
return temp.Substring ( 0 ,temp.Length - 1 );
else
return "" ;
}
</ script >
CREATE PROCEDURE dbo. <% = SourceTable.Name %> Delete
/*
==================================================
??: <% = Author %>
??????: <% = System.DateTime.Now.ToShortDateString() %>
??: <% = Description %>
==================================================
*/
<% for ( int i = 0 ; i < SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) { %>
<% = GetSqlParameterStatement(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < SourceTable.PrimaryKey.MemberColumns.Count - 1 ) { %> , <% } %> <% if (SourceTable.Columns[i].Description ! = "" ) { %> -- <% = SourceTable.Columns[i].Description %><% } %>
<% } %>
AS
Delete From [ <% = SourceTable.Name %> ]
Where
<% for ( int i = 0 ; i < SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) { %>
<% if (i > 0 ) { %> AND <% } %> [ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %> ] = @ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %>
<% } %>
CREATE PROCEDURE dbo. <% = SourceTable.Name %> SelectByID
/*
==================================================
Author: <% = Author %>
CreatedTime: <% = System.DateTime.Now.ToShortDateString() %>
Description: <% = Description %>
==================================================
*/
<% for ( int i = 0 ; i < SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) { %>
<% = GetSqlParameterStatement(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < SourceTable.PrimaryKey.MemberColumns.Count - 1 ) { %> , <% } %> <% if (SourceTable.Columns[i].Description ! = "" ) { %> -- <% = SourceTable.Columns[i].Description %><% } %>
<% } %>
AS
select
<% for ( int i = 0 ; i < SourceTable.Columns.Count; i ++ ) { %>
<% = SourceTable.Columns[i].Name %>
<% } %>
From [ <% = SourceTable.Name %> ]
Where
<% for ( int i = 0 ; i < SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) { %>
<% if (i > 0 ) { %> AND <% } %> [ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %> ] = @ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %>
<% } %>
CREATE PROCEDURE dbo. <% = SourceTable.Name %> SelectAll
/*
==================================================
Author: <% = Author %>
CreatedTime: <% = System.DateTime.Now.ToShortDateString() %>
Description: <% = Description %>
==================================================
*/
AS
select
<% for ( int i = 0 ; i < SourceTable.Columns.Count; i ++ ) { %>
<% = SourceTable.Columns[i].Name %>
<% } %>
From [ <% = SourceTable.Name %> ]
CREATE PROCEDURE dbo. <% = SourceTable.Name %> Update
/*
==================================================
Author: <% = Author %>
CreatedTime: <% = System.DateTime.Now.ToShortDateString() %>
Description: <% = Description %>
==================================================
*/
<% for ( int i = 0 ; i < SourceTable.Columns.Count; i ++ ) { %>
<% = GetSqlParameterStatement(SourceTable.Columns[i]) %>
<% } %>
AS
Update [ <% = SourceTable.Name %> ] set
<% = GetUpdateParameter() %>
Where
<% for ( int i = 0 ; i < SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) { %>
<% if (i > 0 ) { %> AND <% } %> [ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %> ] = @ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %>
<% } %>
<% @ Assembly Name = " SchemaExplorer " %>
<% @ Import Namespace = " SchemaExplorer " %>
<% @ Property Name = " SourceTable " Type = " SchemaExplorer.TableSchema " Category = " DataTable " Description = " Table that the stored procedures should be based on. " %>
<% @ Property Name = " Author " Type = " String " Category = " Context " Description = " The author for this procedure. " Optional = " true " %>
<% @ Property Name = " Description " Type = " String " Category = " Context " Description = " The description for this procedure. " Optional = " true " %>
< script runat ="template" >
public string GetSqlParameterStatement(ColumnSchema column)
{
string param = " @ " + column.Name + " " + column.NativeType;
switch (column.DataType)
{
case DbType.Decimal:
{
param += " ( " + column.Precision + " , " + column.Scale + " ) " ;
break ;
}
default :
{
if (column.Size > 0 )
{
param += " ( " + column.Size + " ) " ;
}
break ;
}
}
return param;
}
public string GetUpdateParameter()
{ string temp = null ;
for ( int i = 0 ;i < SourceTable.Columns.Count;i ++ )
{
if ( ! SourceTable.Columns[i].IsPrimaryKeyMember)
{
temp = temp + SourceTable.Columns[i].Name + " =@ " + SourceTable.Columns[i].Name + " , " + " \n " ;
}
}
if (temp.Length > 0 )
return temp.Substring ( 0 ,temp.Length - 1 );
else
return "" ;
}
</ script >
CREATE PROCEDURE dbo. <% = SourceTable.Name %> Delete
/*
==================================================
??: <% = Author %>
??????: <% = System.DateTime.Now.ToShortDateString() %>
??: <% = Description %>
==================================================
*/
<% for ( int i = 0 ; i < SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) { %>
<% = GetSqlParameterStatement(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < SourceTable.PrimaryKey.MemberColumns.Count - 1 ) { %> , <% } %> <% if (SourceTable.Columns[i].Description ! = "" ) { %> -- <% = SourceTable.Columns[i].Description %><% } %>
<% } %>
AS
Delete From [ <% = SourceTable.Name %> ]
Where
<% for ( int i = 0 ; i < SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) { %>
<% if (i > 0 ) { %> AND <% } %> [ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %> ] = @ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %>
<% } %>
CREATE PROCEDURE dbo. <% = SourceTable.Name %> SelectByID
/*
==================================================
Author: <% = Author %>
CreatedTime: <% = System.DateTime.Now.ToShortDateString() %>
Description: <% = Description %>
==================================================
*/
<% for ( int i = 0 ; i < SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) { %>
<% = GetSqlParameterStatement(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < SourceTable.PrimaryKey.MemberColumns.Count - 1 ) { %> , <% } %> <% if (SourceTable.Columns[i].Description ! = "" ) { %> -- <% = SourceTable.Columns[i].Description %><% } %>
<% } %>
AS
select
<% for ( int i = 0 ; i < SourceTable.Columns.Count; i ++ ) { %>
<% = SourceTable.Columns[i].Name %>
<% } %>
From [ <% = SourceTable.Name %> ]
Where
<% for ( int i = 0 ; i < SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) { %>
<% if (i > 0 ) { %> AND <% } %> [ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %> ] = @ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %>
<% } %>
CREATE PROCEDURE dbo. <% = SourceTable.Name %> SelectAll
/*
==================================================
Author: <% = Author %>
CreatedTime: <% = System.DateTime.Now.ToShortDateString() %>
Description: <% = Description %>
==================================================
*/
AS
select
<% for ( int i = 0 ; i < SourceTable.Columns.Count; i ++ ) { %>
<% = SourceTable.Columns[i].Name %>
<% } %>
From [ <% = SourceTable.Name %> ]
CREATE PROCEDURE dbo. <% = SourceTable.Name %> Update
/*
==================================================
Author: <% = Author %>
CreatedTime: <% = System.DateTime.Now.ToShortDateString() %>
Description: <% = Description %>
==================================================
*/
<% for ( int i = 0 ; i < SourceTable.Columns.Count; i ++ ) { %>
<% = GetSqlParameterStatement(SourceTable.Columns[i]) %>
<% } %>
AS
Update [ <% = SourceTable.Name %> ] set
<% = GetUpdateParameter() %>
Where
<% for ( int i = 0 ; i < SourceTable.PrimaryKey.MemberColumns.Count; i ++ ) { %>
<% if (i > 0 ) { %> AND <% } %> [ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %> ] = @ <% = SourceTable.PrimaryKey.MemberColumns[i].Name %>
<% } %>