上次我们说了怎样访问数据库,现在我们接下来说怎样根据访问的表写模板,首先:我们来看看他声明的SourceTable的属性:
SourceTable.Columns[i]:指的是数据库的第I列。
SourceTable.Columns[i].Name:第I列的字段名。
SourceTable.Columns[i].NativeType:第I列的字段类型。
SourceTable.Columns[i].Size:第I列字段的大小。
SourceTable.Columns.Count:表的行数。
SourceTable.Columns[i].Description:第I列的描述信息
根据以上,我们可以写出以下的模板了:
运行之后的结果为:
SourceTable.Columns[i]:指的是数据库的第I列。
SourceTable.Columns[i].Name:第I列的字段名。
SourceTable.Columns[i].NativeType:第I列的字段类型。
SourceTable.Columns[i].Size:第I列字段的大小。
SourceTable.Columns.Count:表的行数。
SourceTable.Columns[i].Description:第I列的描述信息
根据以上,我们可以写出以下的模板了:
<%
@ CodeTemplate Language
=
"
C#
"
TargetLanguage
=
"
T-SQL
"
Description = " Generates a update stored procedure. " %>
<% @ Property Name = " SourceTable " Type = " SchemaExplorer.TableSchema "
Category = " Context "
Description = " Table that the stored procedures should be based on. " %>
<% @ Assembly Name = " SchemaExplorer " %>
<% @ Import Namespace = " SchemaExplorer " %>
-----------------------------------------------------------------
-- Date Created: <%= DateTime.Now.ToLongDateString() %>
-- Created By: guojiang
-----------------------------------------------------------------
< script runat = " template " >
public string GetSqlParameterStatement(ColumnSchema column)
{
string param =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;
}
</ script >
<% for ( int i = 0 ; i < SourceTable.Columns.Count; i ++ ) { %>
//<%=SourceTable.Columns[i].Description%>
//类型 <%=GetSqlParameterStatement(SourceTable.Columns[i])%>
this.txt<%= SourceTable.Columns[i].Name %>.Text=model.<%= SourceTable.Columns[i].Name %>
<% } %>
<% for ( int i = 0 ; i < SourceTable.Columns.Count; i ++ ) { %>
//<%=SourceTable.Columns[i].Description%>
//类型 <%=GetSqlParameterStatement(SourceTable.Columns[i])%>
model.<%= SourceTable.Columns[i].Name %>=this.txt<%= SourceTable.Columns[i].Name %>.Text.Trim()
<% } %>
Description = " Generates a update stored procedure. " %>
<% @ Property Name = " SourceTable " Type = " SchemaExplorer.TableSchema "
Category = " Context "
Description = " Table that the stored procedures should be based on. " %>
<% @ Assembly Name = " SchemaExplorer " %>
<% @ Import Namespace = " SchemaExplorer " %>
-----------------------------------------------------------------
-- Date Created: <%= DateTime.Now.ToLongDateString() %>
-- Created By: guojiang
-----------------------------------------------------------------
< script runat = " template " >
public string GetSqlParameterStatement(ColumnSchema column)
{
string param =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;
}
</ script >
<% for ( int i = 0 ; i < SourceTable.Columns.Count; i ++ ) { %>
//<%=SourceTable.Columns[i].Description%>
//类型 <%=GetSqlParameterStatement(SourceTable.Columns[i])%>
this.txt<%= SourceTable.Columns[i].Name %>.Text=model.<%= SourceTable.Columns[i].Name %>
<% } %>
<% for ( int i = 0 ; i < SourceTable.Columns.Count; i ++ ) { %>
//<%=SourceTable.Columns[i].Description%>
//类型 <%=GetSqlParameterStatement(SourceTable.Columns[i])%>
model.<%= SourceTable.Columns[i].Name %>=this.txt<%= SourceTable.Columns[i].Name %>.Text.Trim()
<% } %>
运行之后的结果为:
-----------------------------------------------------------------
-- Date Created: 2007年4月25日
-- Created By: guojiang
-----------------------------------------------------------------
// 离职部门管理号
// 类型 varchar(2)
this .txtManageNo.Text = model.ManageNo
// 一二级部门管理号
// 类型 varchar(3)
this .txtDeptCode.Text = model.DeptCode
// 一二级部门名称
// 类型 varchar(50)
this .txtDeptName.Text = model.DeptName
//
// 类型 datetime
this .txtRecordCreateDate.Text = model.RecordCreateDate
//
// 类型 varchar(32)
this .txtRecordCreator.Text = model.RecordCreator
//
// 类型 datetime
this .txtRecordUpdateDate.Text = model.RecordUpdateDate
//
// 类型 varchar(32)
this .txtRecordUpdator.Text = model.RecordUpdator
// 离职部门管理号
// 类型 varchar(2)
model.ManageNo = this .txtManageNo.Text.Trim()
// 一二级部门管理号
// 类型 varchar(3)
model.DeptCode = this .txtDeptCode.Text.Trim()
// 一二级部门名称
// 类型 varchar(50)
model.DeptName = this .txtDeptName.Text.Trim()
//
// 类型 datetime
model.RecordCreateDate = this .txtRecordCreateDate.Text.Trim()
//
// 类型 varchar(32)
model.RecordCreator = this .txtRecordCreator.Text.Trim()
//
// 类型 datetime
model.RecordUpdateDate = this .txtRecordUpdateDate.Text.Trim()
//
// 类型 varchar(32)
model.RecordUpdator = this .txtRecordUpdator.Text.Trim()
-- Date Created: 2007年4月25日
-- Created By: guojiang
-----------------------------------------------------------------
// 离职部门管理号
// 类型 varchar(2)
this .txtManageNo.Text = model.ManageNo
// 一二级部门管理号
// 类型 varchar(3)
this .txtDeptCode.Text = model.DeptCode
// 一二级部门名称
// 类型 varchar(50)
this .txtDeptName.Text = model.DeptName
//
// 类型 datetime
this .txtRecordCreateDate.Text = model.RecordCreateDate
//
// 类型 varchar(32)
this .txtRecordCreator.Text = model.RecordCreator
//
// 类型 datetime
this .txtRecordUpdateDate.Text = model.RecordUpdateDate
//
// 类型 varchar(32)
this .txtRecordUpdator.Text = model.RecordUpdator
// 离职部门管理号
// 类型 varchar(2)
model.ManageNo = this .txtManageNo.Text.Trim()
// 一二级部门管理号
// 类型 varchar(3)
model.DeptCode = this .txtDeptCode.Text.Trim()
// 一二级部门名称
// 类型 varchar(50)
model.DeptName = this .txtDeptName.Text.Trim()
//
// 类型 datetime
model.RecordCreateDate = this .txtRecordCreateDate.Text.Trim()
//
// 类型 varchar(32)
model.RecordCreator = this .txtRecordCreator.Text.Trim()
//
// 类型 datetime
model.RecordUpdateDate = this .txtRecordUpdateDate.Text.Trim()
//
// 类型 varchar(32)
model.RecordUpdator = this .txtRecordUpdator.Text.Trim()
更多,请访问:::