数据库函数..........
数据库函数..........
1
using System;
2
using System.Data;
3
using System.Data.SqlClient;
4
5
using CommonClass;
6
7
namespace SqlConnClass
8

{
9
/**//// <summary>
10
/// SqlConn 的摘要说明。
11
/// </summary>
12
public class SqlConn
13
{
14
private SqlConnection _SConn;
15
16
构造函数#region 构造函数
17
public SqlConn(string sqlconn)
18
{
19
this._SConn=new SqlConnection(sqlconn);
20
21
}
22
#endregion
23
24
public SqlConnection SConn
25
{
26
get
{return _SConn;}
27
}
28
29
30
31
public enum DBOperate
32
{
33
Insert=1,
34
Update,
35
Delete,
36
Pause,
37
Select,
38
Order,
39
Other
40
}
41
42
打开关闭连接 Open(),Close()#region 打开关闭连接 Open(),Close()
43
public void Open()
44
{_SConn.Open();}
45
46
public void Close()
47
{_SConn.Close();}
48
#endregion
49
50
根据语句查询返回DataSet SelectBySql(string sqlstr)#region 根据语句查询返回DataSet SelectBySql(string sqlstr)
51
public DataSet SelectBySql(string sqlstr)
52
{
53
SqlDataAdapter da;
54
DataSet ds=new DataSet();
55
56
da = new SqlDataAdapter( "SelectBySql", _SConn );
57
da.SelectCommand.CommandType = CommandType.StoredProcedure;
58
da.SelectCommand.Parameters.Add( new SqlParameter( "@SqlStr", sqlstr ) );
59
da.Fill(ds);
60
da.Dispose();
61
62
return ds;
63
}
64
#endregion
65
66
3参数 根据 ID 返回一行SqlDataReader SelectByKey(int KeyValue,string TableName,string KeyName)#region 3参数 根据 ID 返回一行SqlDataReader SelectByKey(int KeyValue,string TableName,string KeyName)
67
public SqlDataReader SelectByKey(int KeyValue,string TableName,string KeyName)
68
{
69
//一般为前台页面使用该函数,不涉及到数据更新
70
string Sqlstr="";
71
Sqlstr="select * from "+TableName+" where "+KeyName+"="+KeyValue;
72
SqlDataReader r;
73
SqlCommand com=new SqlCommand(Sqlstr,_SConn);
74
Open();
75
r = com.ExecuteReader();
76
77
return r;
78
79
}
80
#endregion
81
82
3参数 根据 字符串 返回一行SqlDataReader SelectByKey(int KeyValue,string TableName,string KeyName)#region 3参数 根据 字符串 返回一行SqlDataReader SelectByKey(int KeyValue,string TableName,string KeyName)
83
public SqlDataReader SelectByKey(string KeyValue,string TableName,string KeyName)
84
{
85
//一般为前台页面使用该函数,不涉及到数据更新
86
string Sqlstr="";
87
Sqlstr="select * from "+TableName+" where "+KeyName+"='"+KeyValue+"'";
88
SqlDataReader r;
89
SqlCommand com=new SqlCommand(Sqlstr,_SConn);
90
Open();
91
r = com.ExecuteReader();
92
com.Dispose();
93
return r;
94
95
}
96
#endregion
97
98
1参数 根据语句返回一行SqlDataReader SelectByKey(string sqlstr)#region 1参数 根据语句返回一行SqlDataReader SelectByKey(string sqlstr)
99
public SqlDataReader SelectByKey(string sqlstr)
100
{
101
SqlDataReader r=null;
102
103
SqlCommand com=new SqlCommand(sqlstr,_SConn);
104
Open();
105
r = com.ExecuteReader();
106
com.Dispose();
107
return r;
108
}
109
#endregion
110
111
根据关键字删除一行bool DelectByKey(object KeyValue,string TableName,string KeyName)#region 根据关键字删除一行bool DelectByKey(object KeyValue,string TableName,string KeyName)
112
public bool DelectByKey(object KeyValue,string TableName,string KeyName)
113
{
114
try
115
{
116
string Sqlstr="";
117
118
if(Common.IsNum(KeyValue))
119
Sqlstr="Delete from "+TableName+" where "+KeyName+"="+Convert.ToInt32(KeyValue.ToString());
120
else
121
Sqlstr="delete from "+TableName+" where "+KeyName+"='"+KeyValue.ToString()+"'";
122
123
Open();
124
SqlCommand com=new SqlCommand(Sqlstr,_SConn);
125
com.ExecuteNonQuery();
126
Close();
127
com.Dispose();
128
return true;
129
}
130
catch
131
{
132
Close();
133
return false;
134
}
135
}
136
#endregion
137
138
操作函数 bool Operate(string SqlStr)#region 操作函数 bool Operate(string SqlStr)
139
public bool Operate(string SqlStr)
140
{
141
142
try
143
{
144
145
Open();
146
SqlCommand com=new SqlCommand( SqlStr, _SConn );
147
com.ExecuteNonQuery();
148
Close();
149
com.Dispose();
150
return true;
151
}
152
catch
153
{
154
Close();
155
return false;
156
}
157
}
158
#endregion
159
160
统计记录数 GetCount(string sqlstr)#region 统计记录数 GetCount(string sqlstr)
161
public string GetCount(string sqlstr)
162
{
163
string str="";
164
SqlDataReader r;
165
166
SqlCommand com=new SqlCommand(sqlstr,this._SConn);
167
Open();
168
r = com.ExecuteReader();
169
if(r.Read())
170
str=r["sum"].ToString();
171
else
172
str="";
173
r.Close();
174
Close();
175
176
return str;
177
}
178
#endregion
179
180
分页#region 分页
181
public DataSet CutPage(int operation,string TableName,string FieldLists,string KeyField,
182
string Critical,string SortType,int PageSize,int ShowPageNo)
183
{
184
return CutPage(operation,TableName,FieldLists,KeyField,
185
Critical,SortType,PageSize,ShowPageNo,"");
186
}
187
188
public DataSet CutPage(int operation,string TableName,string FieldLists,string KeyField,
189
string Critical,string SortType,int PageSize,int ShowPageNo,string Group)
190
{
191
// operation int, --操作類型,0為取得當前頁記錄集,1為取得記錄總數(RecordCount)
192
// @TableName varchar(100), --要操作的表名
193
// @FieldLists varchar(8000), --字段列表,可以為'*",也可為空
194
// @KeyField varchar(100), --關鍵字段名
195
// @Critical varchar(200), --查詢條件
196
// @SortType varchar(4), --排序類型,為'DESC'或者'ASC',為空是默認為'ASC'
197
// @PageSize int, --每頁大小,大於0的整數
198
// @ShowPageNo int --當前要顯示的頁碼,大於0的整數,頁數從1開始
199
SqlDataAdapter da;
200
DataSet ds=new DataSet();
201
da = new SqlDataAdapter( "CutPage", _SConn );
202
da.SelectCommand.CommandType = CommandType.StoredProcedure;
203
da.SelectCommand.Parameters.Add( new SqlParameter( "@operation", operation ) );
204
da.SelectCommand.Parameters.Add( new SqlParameter( "@TableName", TableName ) );
205
da.SelectCommand.Parameters.Add( new SqlParameter( "@FieldLists", FieldLists ) );
206
da.SelectCommand.Parameters.Add( new SqlParameter( "@KeyField", KeyField ) );
207
da.SelectCommand.Parameters.Add( new SqlParameter( "@Critical", Critical ) );
208
da.SelectCommand.Parameters.Add( new SqlParameter( "@SortType", SortType ) );
209
da.SelectCommand.Parameters.Add( new SqlParameter( "@PageSize", PageSize ) );
210
da.SelectCommand.Parameters.Add( new SqlParameter( "@ShowPageNo", ShowPageNo ) );
211
da.SelectCommand.Parameters.Add( new SqlParameter( "@Group", Group ) );
212
da.Fill(ds);
213
214
return ds;
215
}
216
#endregion
217
218
219
/**//////////////////////////////////////////////////////////////////////////////////////////////////////////////
220
221
基础函数#region 基础函数
222
private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
223
{
224
foreach (SqlParameter p in commandParameters)
225
{
226
//check for derived output value with no value assigned
227
if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
228
{
229
p.Value = DBNull.Value;
230
}
231
232
command.Parameters.Add(p);
233
}
234
}
235
236
private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)
237
{
238
//if the provided connection is not open, we will open it
239
if (connection.State != ConnectionState.Open)
240
{
241
connection.Open();
242
}
243
244
//associate the connection with the command
245
command.Connection = connection;
246
247
//set the command text (stored procedure name or SQL statement)
248
command.CommandText = commandText;
249
250
//if we were provided a transaction, assign it.
251
if (transaction != null)
252
{
253
command.Transaction = transaction;
254
}
255
256
//set the command type
257
command.CommandType = commandType;
258
259
//attach the command parameters if they are provided
260
if (commandParameters != null)
261
{
262
AttachParameters(command, commandParameters);
263
}
264
265
return;
266
}
267
268
#endregion
269
270
执行SQL语句#region 执行SQL语句
271
272
/**//// <summary>
273
/// 执行SQL语句,返回受影响的行数
274
/// </summary>
275
/// <param name="strSql">SQL语句</param>
276
public int ExecNonSql(string strSql)
277
{
278
return ExecNonSql(strSql,null);
279
}
280
public int ExecNonSql(string strSql,SqlParameter[] commandParameters)
281
{
282
SqlCommand cmd = new SqlCommand();
283
PrepareCommand(cmd,_SConn,null,CommandType.Text,strSql,commandParameters);
284
int retval = cmd.ExecuteNonQuery();
285
286
this.Close();
287
return retval;
288
}
289
290
/**//// <summary>
291
/// 执行SQL语句,返回第一行第一列字符串
292
/// </summary>
293
/// <param name="strSql">SQL语句</param>
294
public string ExecScalarSql(string strSql)
295
{
296
return ExecScalarSql(strSql,null);
297
}
298
public string ExecScalarSql(string strSql,SqlParameter[] commandParameters)
299
{
300
SqlCommand cmd = new SqlCommand();
301
PrepareCommand(cmd,_SConn,null,CommandType.Text,strSql,commandParameters);
302
object retval = cmd.ExecuteScalar();
303
if(retval == null)
304
retval = "";
305
306
this.Close();
307
return retval.ToString();
308
}
309
310
/**//// <summary>
311
/// 执行SQL语句,返回一个SqlDataReader对象
312
/// </summary>
313
/// <param name="strSql">SQL语句</param>
314
public SqlDataReader ExecReaderSql(string strSql)
315
{
316
return ExecReaderSql(strSql,null);
317
}
318
319
public SqlDataReader ExecReaderSql(string strSql,SqlParameter[] commandParameters)
320
{
321
SqlCommand cmd = new SqlCommand();
322
PrepareCommand(cmd,_SConn,null,CommandType.Text,strSql,commandParameters);
323
324
SqlDataReader dr;
325
dr = cmd.ExecuteReader();
326
327
return dr;
328
}
329
330
/**//// <summary>
331
/// 执行SQL语句,返回一个DataSet对象
332
/// </summary>
333
/// <param name="strSql">SQL语句</param>
334
public DataSet ExecDatasetSql(string strSql)
335
{
336
return ExecDatasetSql(strSql,null);
337
}
338
public DataSet ExecDatasetSql(string strSql,SqlParameter[] commandParameters)
339
{
340
SqlCommand cmd = new SqlCommand();
341
PrepareCommand(cmd,_SConn,null,CommandType.Text,strSql,commandParameters);
342
343
//create the DataAdapter & DataSet
344
SqlDataAdapter da = new SqlDataAdapter(cmd);
345
DataSet ds = new DataSet();
346
347
da.Fill(ds);
348
349
cmd.Parameters.Clear();
350
351
this.Close();
352
return ds;
353
}
354
355
#endregion
356
357
执行存储过程#region 执行存储过程
358
359
/**//// <summary>
360
/// 执行SQL存储过程,返回受影响的行数
361
/// </summary>
362
/// <param name="spSql">存储过程名</param>
363
/// <param name="parameterValues">存储过程参数</param>
364
public int ExecNonSP(string spSql)
365
{
366
return ExecNonSP(spSql,null);
367
}
368
public int ExecNonSP(string spSql,SqlParameter[] commandParameters)
369
{
370
SqlCommand cmd = new SqlCommand();
371
372
PrepareCommand(cmd,_SConn,null,CommandType.StoredProcedure,spSql,commandParameters);
373
374
int retval = cmd.ExecuteNonQuery();
375
cmd.Parameters.Clear();
376
377
this.Close();
378
return retval;
379
}
380
/**//// <summary>
381
/// 执行SQL存储过程,返回第一行第一列字符串
382
/// </summary>
383
/// <param name="spSql">存储过程名</param>
384
/// <param name="parameterValues">存储过程参数</param>
385
public string ExecScalarSP(string spSql)
386
{
387
return ExecScalarSP(spSql,null);
388
}
389
public string ExecScalarSP(string spSql,SqlParameter[] commandParameters)
390
{
391
SqlCommand cmd = new SqlCommand();
392
PrepareCommand(cmd,_SConn,null,CommandType.StoredProcedure,spSql,commandParameters);
393
object retval = cmd.ExecuteScalar();
394
cmd.Parameters.Clear();
395
396
if(retval == null)
397
retval = "";
398
399
this.Close();
400
return retval.ToString();
401
}
402
403
/**//// <summary>
404
/// 执行SQL存储过程,返回一个SqlDataReader对象
405
/// </summary>
406
/// <param name="strSql">SQL语句</param>
407
public SqlDataReader ExecReaderSP(string spSql)
408
{
409
return ExecReaderSP(spSql,null);
410
}
411
public SqlDataReader ExecReaderSP(string spSql,SqlParameter[] commandParameters)
412
{
413
SqlCommand cmd = new SqlCommand();
414
PrepareCommand(cmd,_SConn,null,CommandType.StoredProcedure,spSql,commandParameters);
415
416
SqlDataReader dr;
417
dr = cmd.ExecuteReader();
418
cmd.Parameters.Clear();
419
420
return dr;
421
}
422
423
/**//// <summary>
424
/// 执行SQL语句,返回一个DataSet对象
425
/// </summary>
426
/// <param name="strSql">SQL语句</param>
427
public DataSet ExecDatasetSP(string spSql)
428
{
429
return ExecDatasetSP(spSql,null);
430
}
431
public DataSet ExecDatasetSP(string spSql,SqlParameter[] commandParameters)
432
{
433
SqlCommand cmd = new SqlCommand();
434
PrepareCommand(cmd,_SConn,null,CommandType.StoredProcedure,spSql,commandParameters);
435
436
//create the DataAdapter & DataSet
437
SqlDataAdapter da = new SqlDataAdapter(cmd);
438
DataSet ds = new DataSet();
439
440
da.Fill(ds);
441
442
cmd.Parameters.Clear();
443
444
this.Close();
445
return ds;
446
}
447
448
#endregion
449
450
执行XML#region 执行XML
451
#endregion
452
453
/**//////////////////////////////////////////////////////////////////////////////////////////////////////////////
454
}
455
}
456

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

349

350

351

352

353

354

355

356

357


358

359


360

361

362

363

364

365



366

367

368

369



370

371

372

373

374

375

376

377

378

379

380


381

382

383

384

385

386



387

388

389

390



391

392

393

394

395

396

397

398

399

400

401

402

403


404

405

406

407

408



409

410

411

412



413

414

415

416

417

418

419

420

421

422

423


424

425

426

427

428



429

430

431

432



433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450


451

452

453


454

455

456
