CodeSmith从4.1版直接支持Oracle Schema Provider了,最近在做一个SQL Server向Oracle迁移的项目,于是写了一个模板来导数据,感觉比较方便,放出来让大家评评,有不足的地方,请高手之处以便改进。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Export sql" Debug="true" %>
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Assembly Name="SchemaExplorer" %>
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Import Namespace="SchemaExplorer" %>
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="" %>
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Property Name="SortBy" Type="String" Default="" Optional="True" Category="SQL" Description="" OnChanged="" Editor="" EditorBase="" Serializer="" %>
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Property Name="Filter" Type="String" Default="" Optional="True" Category="" Description="" OnChanged="" Editor="" EditorBase="" Serializer="" %>
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Property Name="IsOracle" Type="Boolean" Default="False" Optional="False" Category="" Description="Script target database is oracle" OnChanged="" Editor="" EditorBase="" Serializer="" %>
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
--Created <%=DateTime.Now %>
10
-- written by wilson.fu
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
for(int intRow=0;intRow<SourceTableData.Rows.Count;intRow++){ %>
12
insert into <%=GetTableOwner() %><%=SourceTable.Name %>(<%=AllColumns %>)
13
values (<%=AllDate(intRow) %>)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
if(!IsOracle){%>
15
go
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
}else{%>
17
;
18
<%}%>
19
<%}%>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
<script runat="template">
2
private DataTable _sourceTableData;
3
private string _allColumns;
4
private string _allData;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
SourceTableData#region SourceTableData
7
private DataTable SourceTableData
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
9
get
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
if (_sourceTableData == null)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
_sourceTableData = SourceTable.GetTableData();
14
}
15
16
return _sourceTableData;
17
}
18
}
19
#endregion
20
21![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
AllColumns#region AllColumns
22
private string AllColumns
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
get
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
if(_allColumns ==null)
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
string strCols = "";
29
for(int i=0;i<SourceTable.Columns.Count-1;i++)
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
if(IsOracle)
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
strCols += SourceTable.Columns[i].Name + ",";
34
}
35
else if(SourceTable.Columns[i].Name != "fid")
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37
strCols += SourceTable.Columns[i].Name + ",";
38
}
39
}
40
strCols+=SourceTable.Columns[SourceTable.Columns.Count-1].Name;
41
_allColumns = strCols;
42
}
43
return _allColumns;
44
45
}
46
}
47
#endregion
48
49![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
AllData#region AllData
50
private string AllDate(int intRow)
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
string strVal = "";
53
int intCol=0;
54
DataTable dt = SourceTable.GetTableData();
55
DataView dv = new DataView(dt);
56
if(SortBy!=string.Empty)
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
dv.Sort = SortBy;
59
}
60
61
if(Filter!=string.Empty)
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
64
dv.RowFilter = Filter;
65
}
66
dt = dv.ToTable();
67
for(;intCol<dt.Columns.Count-1;intCol++)
68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
if(IsOracle)
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
strVal += Formated(dt.Rows[intRow][intCol],intCol,intRow) + ",";
72
}
73
else if(dt.Columns[intCol].ColumnName != "fid")
74![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
75
strVal += Formated(dt.Rows[intRow][intCol],intCol,intRow) + ",";
76
}
77
}
78
strVal += Formated(dt.Rows[intRow][intCol],intCol,intRow);
79
return strVal;
80
}
81
#endregion
82
83![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
FormatData#region FormatData
84
private string Formated(object obj,int intCol,int intRow)
85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
string strVal = "";
87
switch(SourceTable.Columns[intCol].DataType)
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
case DbType.String:
90
case DbType.Guid:
91
strVal = "'" + obj + "'";
92
break;
93
case DbType.Int16:
94
case DbType.Int32:
95
case DbType.Int64:
96
case DbType.Byte:
97
98
strVal = obj.ToString();
99
break;
100
case DbType.DateTime:
101
case DbType.Date:
102
if(!IsOracle)
103![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104
strVal = "'" + obj + "'";
105
}
106
else
107![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108
strVal = string.Format("to_date('{0}','yyyy-mm-dd hh24:mi:ss')"
109
,Convert.ToDateTime(obj).ToString("yyyy-MM-dd HH:mm:ss"));
110
}
111
break;
112
113
case DbType.Boolean:
114
if(obj == Convert.DBNull)
115![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
116
strVal = "0";
117
}
118
else
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
120
if(Convert.ToBoolean(obj))
121![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
strVal = "1";
123
}
124
else
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
126
strVal = "0";
127
}
128
}
129
break;
130![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* case DbType.DateTime:
131
strVal = "('" + obj + "','yyyy-mm-dd HH24:MI:SS')";
132
break;*/
133
default:
134
strVal = "'" + obj + "'";
135
break;
136
}
137
if (SourceTableData.Rows[intRow][intCol]==DBNull.Value)
138
strVal = "null";
139
return strVal;
140
}
141
#endregion
142
143
public string GetTableOwner()
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
145
return GetTableOwner(true);
146
}
147
148
public string GetTableOwner(bool includeDot)
149![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
150
if (SourceTable.Owner.Length > 0&&!IsOracle)
151![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
152
return SourceTable.Owner + ".";
153
}
154
else
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
156
return "";
157
}
158
}
159
160
</script>
漂泊雪狼,转载请注明出处,谢谢