C# 数据库操作公用类
1

http://www.cnblogs.com/zwq 2009 版权所有#region http://www.cnblogs.com/zwq 2009 版权所有2

3

/**//** 4
* 5
* 用户电脑名:zwq 6
* 7
* 作者:郑文权 8
* 9
* 创建时间:2009-5-17 22:00 10
* 11
* 描述:数据库公用操作类(用于ACCESS)12
* 13
* 版本:V1.0 14
* 15
**************************************************** 16
* 17
* 修改时间: 18
*19
* 作者: 20
*21
* 描述: 22
* 23
* 版本: 24
* 25
*/26

27
#endregion28

29

using#region using30

31
using System;32
using System.Configuration;33
using System.Data.OleDb;34
using System.Data;35
using System.Web.Caching;36

37
#endregion38

39
namespace zwq.lib.Utils40


{41
public class OleDbUtils42

{43

/**//// <summary>44
/// 私有的构造方法45
/// </summary>46

private OleDbUtils()
{ }47

48

/**//// <summary>49
/// 获取单例的数据库操作类对象50
/// </summary>51
/// <returns></returns>52
public static OleDbUtils GetInstance()53

{54
OleDbUtils instance = new OleDbUtils();55
return instance;56
}57

58

自定义私有变量#region 自定义私有变量59

60
private string oledbConnString = null; //数据库连接字符串 61
private OleDbConnection oledbCon = null; //数据库连接对象 62
private OleDbCommand oledbCmd = null; //数据库操作对象 63
private OleDbTransaction oledbTran = null; //数据库事务对象 64
private int oledbOperationColumns = 0; //数据库操作的行数 65
private object oledbScalarResult = null; //数据库ExecuteScalar操作时返回的第一行第一列记录 66
private OleDbDataReader oledbRdr = null; //数据库读取的数据 67
#endregion68

69

数据库操作#region 数据库操作70

71

/**//// <summary>72
/// 创建ACCESS连接字符串 73
/// </summary>74
/// <returns></returns>75
public OleDbUtils CreateConnectionString()76

{77
object oledbConectionString = CacheUtils.GetCache("OleDbConString");78
if (oledbConectionString == null)79

{80
string configAccessPath = ConfigurationManager.ConnectionStrings["access.path"].ConnectionString;81
string accessFilePath = FileUtils.GetFileFullPath(configAccessPath);82
string configAccessCon = ConfigurationManager.ConnectionStrings["access.con"].ConnectionString;83
oledbConectionString = configAccessCon + accessFilePath;84
}85
this.oledbConnString = (string)oledbConectionString;86
return this;87
}88

89

/**//// <summary>90
/// 获取数据库连接字符串91
/// </summary>92
/// <returns></returns>93
public string GetConnectionString()94

{95
if (string.IsNullOrEmpty(this.oledbConnString))96

{97
throw new Exception("未执行CreateConnectionString()方法!");98
}99
return this.oledbConnString;100
}101

102

/**//// <summary>103
/// 创建OleDbConnection连接对象 104
/// </summary>105
/// <returns></returns>106
public OleDbUtils CreateConnection()107

{108
if (string.IsNullOrEmpty(this.oledbConnString))109

{110
throw new Exception("请先设置数据库连接字符串!");111
}112
this.oledbCon = new OleDbConnection(this.oledbConnString);113
return this;114
}115

116

/**//// <summary>117
/// 关闭OleDbConnection连接对象 118
/// </summary>119
/// <returns></returns>120
public OleDbUtils CloseConnection()121

{122
if (this.oledbCon == null)123

{124
throw new Exception("请先设置数据库连接对象!");125
}126
if (oledbCon.State == ConnectionState.Open)127

{128
oledbCon.Close();129
}130
return this;131
}132

133

/**//// <summary>134
/// 获取数据库连接对象135
/// </summary>136
/// <returns></returns>137
public OleDbConnection GetConnection()138

{139
if (this.oledbCon == null)140

{141
throw new Exception("未执行CreateConnection()方法!");142
} return this.oledbCon;143
}144

145

/**//// <summary>146
/// 开始一个事务对象147
/// </summary>148
/// <returns></returns>149
public OleDbUtils BeginTransaction()150

{151
if (this.oledbCon == null)152

{153
throw new Exception("请先设置数据库连接对象!");154
} this.oledbTran = this.oledbCon.BeginTransaction();155
return this;156
}157

158

/**//// <summary>159
/// 获取数据库事务对象160
/// </summary>161
/// <returns></returns>162
public OleDbTransaction GetTransaction()163

{164
if (this.oledbTran == null)165

{166
throw new Exception("未执行BeginTransaction()方法!");167
} return this.oledbTran;168
}169

170

/**//// <summary>171
/// 提交数据库事务172
/// </summary>173
/// <returns></returns>174
public OleDbUtils Commit()175

{176
this.oledbTran.Commit();177
return this;178
}179

180

/**//// <summary>181
/// 回滚数据库事务182
/// </summary>183
/// <returns></returns>184
public OleDbUtils Rollback()185

{186
this.oledbTran.Rollback();187
return this;188
}189

190

/**//// <summary>191
/// 新建OleDbCommand对象192
/// </summary>193
/// <param name="sql"></param>194
/// <returns></returns>195
public OleDbUtils CreateCommand(string sql)196

{197
if (this.oledbCon == null)198

{199
throw new Exception("请先设置数据库连接对象!");200
}201
this.oledbCmd = oledbCon.CreateCommand(); this.oledbCmd.CommandText = sql;202
if (oledbCon.State == ConnectionState.Closed)203

{204
oledbCon.Open();205
}206
//为当前数据库操作对象绑定事务 207
if (this.oledbTran != null)208

{209
this.oledbCmd.Transaction = this.oledbTran;210
}211
return this;212
}213

214

/**//// <summary>215
/// 新建传入参数的OleDbCommand对象216
/// </summary>217
/// <param name="sql"></param>218
/// <param name="oledbParameters"></param>219
/// <returns></returns>220
public OleDbUtils CreateCommand(string sql, params OleDbParameter[] oledbParameters)221

{222
this.CreateCommand(sql);223
if (oledbParameters != null)224

{225
foreach (OleDbParameter oledbParameter in oledbParameters)226

{227
this.oledbCmd.Parameters.Add(oledbParameter);228
}229
}230
return this;231
}232

233

/**//// <summary>234
/// 获取数据库操作对象235
/// </summary>236
/// <returns></returns>237
public OleDbCommand GetCommand()238

{239
if (this.oledbCmd == null)240

{241
throw new Exception("未执行CreateCommand()方法!");242
}243
return this.oledbCmd;244
}245

246

/**//// <summary>247
/// 新建OleDbParameter参数对象并添加进入SqlCommand对象中248
/// </summary>249
/// <param name="parameterName"></param>250
/// <param name="parameterValue"></param>251
/// <returns></returns>252
public OleDbUtils CreateParameter(string parameterName, object parameterValue)253

{254
this.oledbCmd.Parameters.Add(new OleDbParameter(parameterName, parameterValue));255
return this;256
}257

258

/**//// <summary>259
/// 执行Insert,Update,Delete操作260
/// </summary>261
/// <returns></returns>262
public OleDbUtils ExecuteNonQuery()263

{264
if (oledbCmd == null)265

{266
throw new Exception("请先设置数据库操作对象!");267
}268
this.oledbOperationColumns = oledbCmd.ExecuteNonQuery();269
return this;270
}271

272

/**//// <summary>273
/// 获取数据库操作影响的行数274
/// </summary>275
/// <returns></returns>276
public int GetOperationColumns()277

{278
return this.oledbOperationColumns;279
}280

281

/**//// <summary>282
/// 执行Select操作,获取第一行第一列记录283
/// </summary>284
/// <returns></returns>285
public OleDbUtils ExecuteScalar()286

{287
if (oledbCmd == null)288

{289
throw new Exception("请先设置数据库操作对象!");290
}291
this.oledbScalarResult = oledbCmd.ExecuteScalar();292
return this;293
}294

295

/**//// <summary>296
/// 获取数据库操作中记录第一行第一列的数据297
/// </summary>298
/// <returns></returns>299
public object GetScalarResult()300

{301
return this.oledbScalarResult;302
}303

304

305

/**//// <summary>306
/// 执行Select操作307
/// </summary>308
/// <returns></returns>309
public OleDbUtils ExecuteReader()310

{311
if (oledbCmd == null)312

{313
throw new Exception("请先设置数据库操作对象!");314
}315
this.oledbRdr = oledbCmd.ExecuteReader();316
return this;317
}318

319

/**//// <summary>320
/// 获取数据库操作时查询出来的记录集321
/// </summary>322
/// <returns></returns>323
public OleDbDataReader GetDataReader()324

{325
return this.oledbRdr;326
}327
#endregion328
}329
}330

作者:郑文权(Xiangyue's Blog)
出处:http://zwq.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://zwq.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号