1
<%--
2
Name:
3
Author:
4
Description:
5
--%>
6
<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" TargetLanguage="Text" Src="Helper.cs" Inherits="Helper" Debug="False" Description="Template description here." %>
7
<%@ Property Name="NameSpace" Type="System.String" Default="" Optional="false" Category="1.命名空间" Description="当前生成命名空间" %>
8
<%@ Property Name="DataBase" Type="SchemaExplorer.DatabaseSchema" Category="2.数据库对象"
9
Description="数据库对象" %>
10
<%@ Property Name="ModuleName" Type="System.String" Default="" Optional="false" Category="" Description="项目名称" %>
11![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
<%@ Assembly Name="System.Data" %>
13
<%@ Assembly Name="SchemaExplorer" %>
14
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
15
<%@ Import Namespace="System.Data" %>
16
<%@ Import Namespace="SchemaExplorer" %>
17
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
18
using System;
19
using System.Collections.Generic;
20
using System.Text;
21
using System.Collections.Specialized;
22
using System.Data;
23
using System.Data.SqlClient;
24
using System.Configuration.Provider;
25
using System.Web.Configuration;
26
using System.Diagnostics;
27
using System.Reflection;
28![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
namespace <%= NameSpace%>.Providers
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
32
/// 数据提供者具体实现类
33
/// </summary>
34
public partial class Sql<%= ModuleName%>Provider : <%= ModuleName%>Provider
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
系统设置#region 系统设置
37
private string applicationName;
38
private string connectionString;
39
private string connectionStringName;
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
43
/// 设置或获取数据提供者的应用程序名称。
44
/// </summary>
45
public override string ApplicationName
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return applicationName; }
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ applicationName = value; }
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
52
/// 设置或获取数据库连接字符串名称。
53
/// </summary>
54
public string ConnectionStringName
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return connectionStringName; }
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ connectionStringName = value; }
58
}
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
61
/// 获取数据库连接字符串
62
/// </summary>
63
public string ConnectionString
64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
65
//TODO测试临时改动
66
get
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
return connectionString;
69
}
70
}
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
72
/// =======================
73
/// </summary>
74
public string conn
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ connectionString = value; }
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
83
/// 初始化数据提供者。
84
/// </summary>
85
/// <param name="name">数据提供者名称。</param>
86
/// <param name="config">数据提供者配置信息。</param>
87
public override void Initialize(string name, NameValueCollection config)
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
// Verify that config isn't null
90
if (config == null)
91
throw new ArgumentNullException("config");
92![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
// Assign the provider a default name if it doesn't have one
94
if (String.IsNullOrEmpty(name))
95
name = "<%= ModuleName%>Provider";
96![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
// Add a default "description" attribute to config if the
98
// attribute doesn't exist or is empty
99
if (string.IsNullOrEmpty(config["description"]))
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
config.Remove("description");
102
config.Add("description",
103
"SQL application user provider");
104
}
105![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
// Call the base class's Initialize method
107
base.Initialize(name, config);
108![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109
// Initialize _applicationName
110
applicationName = config["applicationName"];
111![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
if (string.IsNullOrEmpty(applicationName))
113
applicationName = "/";
114![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
115
config.Remove("applicationName");
116![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
// Initialize _connectionString
118
string connect = config["connectionStringName"];
119![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120
if (String.IsNullOrEmpty(connect))
121
throw new ProviderException
122
("Empty or missing connectionStringName");
123![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
124
config.Remove("connectionStringName");
125![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
126
if (WebConfigurationManager.ConnectionStrings[connect] == null)
127
throw new ProviderException("Missing connection string");
128![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
connectionString = WebConfigurationManager.ConnectionStrings
130
[connect].ConnectionString;
131![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
132
if (String.IsNullOrEmpty(connectionString))
133
throw new ProviderException("Empty connection string");
134![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
135
// Throw an exception if unrecognized attributes remain
136
if (config.Count > 0)
137![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
138
string attr = config.GetKey(0);
139
if (!String.IsNullOrEmpty(attr))
140
throw new ProviderException
141
("Unrecognized attribute: " + attr);
142
}
143
}
144
#endregion
145
<%
146
foreach(TableSchema Table in DataBase.Tables)
147![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
148
%>
149![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
150![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
%#region <%
151
152
if(Table.ExtendedProperties["Rmark"] != null)
153![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
154
Response.WriteLine(Table.ExtendedProperties["Rmark"].Value.ToString());
155
}
156
else
157![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
158
Response.WriteLine("生成时请在数据库中的扩展属性中设置key=Rmark,Value=''");
159
}%>
160![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
161
public override <%
162
if(!IsMorePrimary(Table))
163![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{%><%= GetCSharpVariableType(Table.PrimaryKey.MemberColumns[0])%> <%
164
165
}
166
else
167![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{%>void <%
168
}
169
%>Create<%= GetClassName(Table)%>(<%= GetClassName(Table)%> <%= GetparameteryName(Table)%>)
170![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
171
<%
172
if(!IsMorePrimary(Table))
173![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{%> <%= GetCSharpVariableType(Table.PrimaryKey.MemberColumns[0])%> <%= Table.PrimaryKey.MemberColumns[0].Name%>;
174
<%
175
}
176
%>
177
SqlConnection conn = new SqlConnection(ConnectionString);
178
SqlCommand cmd = SqlHelper.PrepareCommand("<%= GetTableName(Table)%>_Create", conn, CommandType.StoredProcedure);
179
<%
180
if(!IsMorePrimary(Table))
181![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
182
%>
183
SqlHelper.MakeOutParam(cmd, "@<%= Table.PrimaryKey.MemberColumns[0].Name%>", SqlDbType.<%= GetSqlDbType(Table.PrimaryKey.MemberColumns[0])%>, <%= GetParamSize(Table.PrimaryKey.MemberColumns[0])%>);
184
<%
185
}
186
else
187![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
188
foreach(ColumnSchema column in Table.PrimaryKey.MemberColumns)
189![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
190
%>
191
SqlHelper.MakeInParam(cmd, "@<%= column.Name%>", SqlDbType.<%=GetSqlDbType(column)%>, <%=GetParamSize(column)%>, <%= GetparameteryName(Table)%>.<%= GetPropertyName(column)%>);
192
<%
193
}
194
}
195
foreach(ColumnSchema column in Table.NonPrimaryKeyColumns)
196![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
197
%>
198
SqlHelper.MakeInParam(cmd, "@<%= column.Name%>", SqlDbType.<%=GetSqlDbType(column)%>, <%=GetParamSize(column)%>, <%= GetparameteryName(Table)%>.<%= GetPropertyName(column)%>);
199
<%
200
201
}
202
203
%>
204
using (conn)
205![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
206
try
207![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
208
conn.Open();
209
cmd.ExecuteNonQuery();
210
<%
211
if(!IsMorePrimary(Table))
212![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
213
%>
214
<%= Table.PrimaryKey.MemberColumns[0].Name%> = (<%= GetCSharpVariableType(Table.PrimaryKey.MemberColumns[0])%>)cmd.Parameters["@<%= Table.PrimaryKey.MemberColumns[0].Name%>"].Value;
215
<%
216
}
217
%>
218
}
219
catch (Exception ex)
220![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
221
Debug.WriteLine(ex.Message);
222
throw;
223
}
224
}
225
<%
226
if(!IsMorePrimary(Table))
227![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{%>return <%= Table.PrimaryKey.MemberColumns[0].Name%>;
228
<%
229
}
230
%>
231
232
}
233
public override void Update<%= GetClassName(Table)%>(<%= GetClassName(Table)%> <%= GetparameteryName(Table)%>)
234![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
235
SqlConnection conn = new SqlConnection(ConnectionString);
236
SqlCommand cmd = SqlHelper.PrepareCommand("<%= GetTableName(Table)%>_Update", conn, CommandType.StoredProcedure);
237
<%
238
foreach(ColumnSchema column in Table.Columns)
239![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
240
%>
241
SqlHelper.MakeInParam(cmd, "@<%= column.Name%>", SqlDbType.<%=GetSqlDbType(column)%>, <%=GetParamSize(column)%>, <%= GetparameteryName(Table)%>.<%= GetPropertyName(column)%>);
242
<%
243
}
244
%>
245
using (conn)
246![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
247
try
248![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
249
conn.Open();
250
cmd.ExecuteNonQuery();
251
}
252
catch (Exception ex)
253![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
254
Debug.WriteLine(ex.Message);
255
throw;
256
}
257
}
258
}
259
public override void Delete<%= GetClassName(Table)%>(<%
260
foreach(ColumnSchema column in Table.PrimaryKey.MemberColumns)
261![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
262
%><%= GetCSharpVariableType(column)%> <%=GetFiledName(column)%><%= GetComma(column,Table.PrimaryKey.MemberColumns)%><%}%>)
263![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
264
SqlConnection conn = new SqlConnection(ConnectionString);
265
SqlCommand cmd = SqlHelper.PrepareCommand("<%= GetTableName(Table)%>_Delete", conn, CommandType.StoredProcedure);
266
<%
267
foreach(ColumnSchema column in Table.PrimaryKey.MemberColumns)
268![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
269
%>
270
SqlHelper.MakeInParam(cmd, "@<%= column.Name%>", SqlDbType.<%=GetSqlDbType(column)%>, <%=GetParamSize(column)%>, <%=GetFiledName(column)%>);<%}%>
271
using (conn)
272![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
273
try
274![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
275
conn.Open();
276
cmd.ExecuteNonQuery();
277
}
278
catch (Exception ex)
279![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
280
Debug.WriteLine(ex.Message);
281
throw;
282
}
283
}
284![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
285
}
286
public override <%= GetClassName(Table)%> Get<%= GetClassName(Table)%>(<%
287
foreach(ColumnSchema column in Table.PrimaryKey.MemberColumns)
288![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
289
%><%= GetCSharpVariableType(column)%> <%=GetFiledName(column)%><%= GetComma(column,Table.PrimaryKey.MemberColumns)%><%}%>)
290![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
291
SqlConnection conn = new SqlConnection(ConnectionString);
292
SqlCommand cmd = SqlHelper.PrepareCommand("<%= GetTableName(Table)%>_Get<%= GetClassName(Table)%>", conn, CommandType.StoredProcedure);
293
<%
294
foreach(ColumnSchema column in Table.PrimaryKey.MemberColumns)
295![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
296
%>
297
SqlHelper.MakeInParam(cmd, "@<%= column.Name%>", SqlDbType.<%=GetSqlDbType(column)%>, <%=GetParamSize(column)%>, <%=GetFiledName(column)%>);<%}%>
298
<%= GetClassName(Table)%> item = null;
299![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
300
using (conn)
301![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
302
try
303![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
304
conn.Open();
305
using (SqlDataReader reader = cmd.ExecuteReader())
306![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
307
if (reader.Read())
308
item = Populate<%= GetTableName(Table)%>(reader);
309
}
310
}
311
catch (Exception ex)
312![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
313
Debug.WriteLine(ex.Message);
314
throw;
315
}
316
}
317![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
318
return item;
319
}
320
private <%= GetClassName(Table)%> Populate<%= GetTableName(Table)%>(IDataReader reader)
321![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
322
<%= GetClassName(Table)%> <%= GetparameteryName(Table)%> = new <%= GetClassName(Table)%>();
323
<%
324
foreach(ColumnSchema column in Table.Columns)
325![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
326
%>
327
if (reader["<%= column.Name%>"] != DBNull.Value)
328
<%= GetparameteryName(Table)%>.<%= GetPropertyName(column)%> = (<%= GetCSharpVariableType(column)%>)reader["CustomerId"];
329![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
330
<%}%>
331
return <%= GetparameteryName(Table)%>;
332
}
333
#endregion
334
<%
335
}
336
%>
337
}
338![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
339![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
340![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
341
}
342![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
343
<script runat="template">
344
public string GetDBNullValue(ColumnSchema column)
345![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
346
return "";
347
}
348
</script>
posted @
2007-07-05 20:02
吴碧宇
阅读(
502)
评论()
编辑
收藏
举报