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
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
29
namespace <%= NameSpace%>.Providers
30

{
31
/**//// <summary>
32
/// 数据提供者具体实现类
33
/// </summary>
34
public partial class Sql<%= ModuleName%>Provider : <%= ModuleName%>Provider
35
{
36
系统设置#region 系统设置
37
private string applicationName;
38
private string connectionString;
39
private string connectionStringName;
40
41
42
/**//// <summary>
43
/// 设置或获取数据提供者的应用程序名称。
44
/// </summary>
45
public override string ApplicationName
46
{
47
get
{ return applicationName; }
48
set
{ applicationName = value; }
49
}
50
51
/**//// <summary>
52
/// 设置或获取数据库连接字符串名称。
53
/// </summary>
54
public string ConnectionStringName
55
{
56
get
{ return connectionStringName; }
57
set
{ connectionStringName = value; }
58
}
59
60
/**//// <summary>
61
/// 获取数据库连接字符串
62
/// </summary>
63
public string ConnectionString
64
{
65
//TODO测试临时改动
66
get
67
{
68
return connectionString;
69
}
70
}
71
/**//// <summary>
72
/// =======================
73
/// </summary>
74
public string conn
75
{
76
set
{ connectionString = value; }
77
}
78
79
80
81
82
/**//// <summary>
83
/// 初始化数据提供者。
84
/// </summary>
85
/// <param name="name">数据提供者名称。</param>
86
/// <param name="config">数据提供者配置信息。</param>
87
public override void Initialize(string name, NameValueCollection config)
88
{
89
// Verify that config isn't null
90
if (config == null)
91
throw new ArgumentNullException("config");
92
93
// Assign the provider a default name if it doesn't have one
94
if (String.IsNullOrEmpty(name))
95
name = "<%= ModuleName%>Provider";
96
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
{
101
config.Remove("description");
102
config.Add("description",
103
"SQL application user provider");
104
}
105
106
// Call the base class's Initialize method
107
base.Initialize(name, config);
108
109
// Initialize _applicationName
110
applicationName = config["applicationName"];
111
112
if (string.IsNullOrEmpty(applicationName))
113
applicationName = "/";
114
115
config.Remove("applicationName");
116
117
// Initialize _connectionString
118
string connect = config["connectionStringName"];
119
120
if (String.IsNullOrEmpty(connect))
121
throw new ProviderException
122
("Empty or missing connectionStringName");
123
124
config.Remove("connectionStringName");
125
126
if (WebConfigurationManager.ConnectionStrings[connect] == null)
127
throw new ProviderException("Missing connection string");
128
129
connectionString = WebConfigurationManager.ConnectionStrings
130
[connect].ConnectionString;
131
132
if (String.IsNullOrEmpty(connectionString))
133
throw new ProviderException("Empty connection string");
134
135
// Throw an exception if unrecognized attributes remain
136
if (config.Count > 0)
137
{
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
{
148
%>
149
150
%#region <%
151
152
if(Table.ExtendedProperties["Rmark"] != null)
153
{
154
Response.WriteLine(Table.ExtendedProperties["Rmark"].Value.ToString());
155
}
156
else
157
{
158
Response.WriteLine("生成时请在数据库中的扩展属性中设置key=Rmark,Value=''");
159
}%>
160
161
public override <%
162
if(!IsMorePrimary(Table))
163
{%><%= GetCSharpVariableType(Table.PrimaryKey.MemberColumns[0])%> <%
164
165
}
166
else
167
{%>void <%
168
}
169
%>Create<%= GetClassName(Table)%>(<%= GetClassName(Table)%> <%= GetparameteryName(Table)%>)
170
{
171
<%
172
if(!IsMorePrimary(Table))
173
{%> <%= 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
{
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
{
188
foreach(ColumnSchema column in Table.PrimaryKey.MemberColumns)
189
{
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
{
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
{
206
try
207
{
208
conn.Open();
209
cmd.ExecuteNonQuery();
210
<%
211
if(!IsMorePrimary(Table))
212
{
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
{
221
Debug.WriteLine(ex.Message);
222
throw;
223
}
224
}
225
<%
226
if(!IsMorePrimary(Table))
227
{%>return <%= Table.PrimaryKey.MemberColumns[0].Name%>;
228
<%
229
}
230
%>
231
232
}
233
public override void Update<%= GetClassName(Table)%>(<%= GetClassName(Table)%> <%= GetparameteryName(Table)%>)
234
{
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
{
240
%>
241
SqlHelper.MakeInParam(cmd, "@<%= column.Name%>", SqlDbType.<%=GetSqlDbType(column)%>, <%=GetParamSize(column)%>, <%= GetparameteryName(Table)%>.<%= GetPropertyName(column)%>);
242
<%
243
}
244
%>
245
using (conn)
246
{
247
try
248
{
249
conn.Open();
250
cmd.ExecuteNonQuery();
251
}
252
catch (Exception ex)
253
{
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
{
262
%><%= GetCSharpVariableType(column)%> <%=GetFiledName(column)%><%= GetComma(column,Table.PrimaryKey.MemberColumns)%><%}%>)
263
{
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
{
269
%>
270
SqlHelper.MakeInParam(cmd, "@<%= column.Name%>", SqlDbType.<%=GetSqlDbType(column)%>, <%=GetParamSize(column)%>, <%=GetFiledName(column)%>);<%}%>
271
using (conn)
272
{
273
try
274
{
275
conn.Open();
276
cmd.ExecuteNonQuery();
277
}
278
catch (Exception ex)
279
{
280
Debug.WriteLine(ex.Message);
281
throw;
282
}
283
}
284
285
}
286
public override <%= GetClassName(Table)%> Get<%= GetClassName(Table)%>(<%
287
foreach(ColumnSchema column in Table.PrimaryKey.MemberColumns)
288
{
289
%><%= GetCSharpVariableType(column)%> <%=GetFiledName(column)%><%= GetComma(column,Table.PrimaryKey.MemberColumns)%><%}%>)
290
{
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
{
296
%>
297
SqlHelper.MakeInParam(cmd, "@<%= column.Name%>", SqlDbType.<%=GetSqlDbType(column)%>, <%=GetParamSize(column)%>, <%=GetFiledName(column)%>);<%}%>
298
<%= GetClassName(Table)%> item = null;
299
300
using (conn)
301
{
302
try
303
{
304
conn.Open();
305
using (SqlDataReader reader = cmd.ExecuteReader())
306
{
307
if (reader.Read())
308
item = Populate<%= GetTableName(Table)%>(reader);
309
}
310
}
311
catch (Exception ex)
312
{
313
Debug.WriteLine(ex.Message);
314
throw;
315
}
316
}
317
318
return item;
319
}
320
private <%= GetClassName(Table)%> Populate<%= GetTableName(Table)%>(IDataReader reader)
321
{
322
<%= GetClassName(Table)%> <%= GetparameteryName(Table)%> = new <%= GetClassName(Table)%>();
323
<%
324
foreach(ColumnSchema column in Table.Columns)
325
{
326
%>
327
if (reader["<%= column.Name%>"] != DBNull.Value)
328
<%= GetparameteryName(Table)%>.<%= GetPropertyName(column)%> = (<%= GetCSharpVariableType(column)%>)reader["CustomerId"];
329
330
<%}%>
331
return <%= GetparameteryName(Table)%>;
332
}
333
#endregion
334
<%
335
}
336
%>
337
}
338
339
340
341
}
342
343
<script runat="template">
344
public string GetDBNullValue(ColumnSchema column)
345

{
346
return "";
347
}
348
</script>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30



31


32

33

34

35



36


37

38

39

40

41

42


43

44

45

46



47



48



49

50

51


52

53

54

55



56



57



58

59

60


61

62

63

64



65

66

67



68

69

70

71


72

73

74

75



76



77

78

79

80

81

82


83

84

85

86

87

88



89

90

91

92

93

94

95

96

97

98

99

100



101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137



138

139

140

141

142

143

144

145

146

147



148

149

150


151

152

153



154

155

156

157



158

159

160

161

162

163



164

165

166

167



168

169

170



171

172

173



174

175

176

177

178

179

180

181



182

183

184

185

186

187



188

189



190

191

192

193

194

195

196



197

198

199

200

201

202

203

204

205



206

207



208

209

210

211

212



213

214

215

216

217

218

219

220



221

222

223

224

225

226

227



228

229

230

231

232

233

234



235

236

237

238

239



240

241

242

243

244

245

246



247

248



249

250

251

252

253



254

255

256

257

258

259

260

261



262

263



264

265

266

267

268



269

270

271

272



273

274



275

276

277

278

279



280

281

282

283

284

285

286

287

288



289

290



291

292

293

294

295



296

297

298

299

300

301



302

303



304

305

306



307

308

309

310

311

312



313

314

315

316

317

318

319

320

321



322

323

324

325



326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345



346

347

348
