温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:个人图书管理系统源码
当前文件路径:MyLibary/App_Code/DataAccessLayer/DataBase.cs

1using System; 2
using System.Data; 3
using System.Collections; 4
using System.Data.SqlClient; 5
using System.Configuration; 6
7
//该源码下载自www.51aspx.com(51aspx.com) 8
9
namespace MyLibrary.DataAccessLayer 10
{ 11
// 数据库接口类 12
public class DataBase 13
{ 14
//私有变量,数据库连接 15
protected SqlConnection Connection; 16
protected string ConnectionString; 17
18
//构造函数 19
public DataBase() 20
{ 21
ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 22
} 23
24
//保护方法,打开数据库连接 25
private void Open() 26
{ 27
//判断数据库连接是否存在 28
if (Connection == null) 29
{ 30
//不存在,新建并打开 31
Connection = new SqlConnection(ConnectionString); 32
Connection.Open(); 33
} 34
else 35
{ 36
//存在,判断是否处于关闭状态 37
if (Connection.State.Equals(ConnectionState.Closed)) 38
Connection.Open(); //连接处于关闭状态,重新打开 39
} 40
} 41
42
//公有方法,关闭数据库连接 43
public void Close() 44
{ 45
if (Connection.State.Equals(ConnectionState.Open)) 46
{ 47
Connection.Close(); //连接处于打开状态,关闭连接 48
} 49
} 50
51
/// <summary> 52
/// 析构函数,释放非托管资源 53
/// </summary> 54
~DataBase() 55
{ 56
try 57
{ 58
if (Connection != null) 59
Connection.Close(); 60
} 61
catch{} 62
try 63
{ 64
Dispose(); 65
} 66
catch{} 67
} 68
//51_a_s_p_x.c_o_m 69
70
//公有方法,释放资源 71
public void Dispose() 72
{ 73
if (Connection != null) // 确保连接被关闭 74
{ 75
Connection.Dispose(); 76
Connection = null; 77
} 78
} 79
80
//公有方法,根据Sql语句,返回是否查询到记录 81
public bool GetRecord(string XSqlString) 82
{ 83
Open(); 84
SqlDataAdapter adapter = new SqlDataAdapter(XSqlString, Connection); 85
DataSet dataset = new DataSet(); 86
adapter.Fill(dataset); 87
Close(); 88
89
if (dataset.Tables[0].Rows.Count > 0) 90
{ 91
return true; 92
} 93
else 94
{ 95
return false; 96
} 97
} 98
99
//公有方法,返回Sql语句获得的数据值 100
//SqlString的格式:select count(*) from XXX where ... 101
// select max(XXX) from YYY where ... 102
public int GetRecordCount(string XSqlString) 103
{ 104
string SCount; 105
106
Open(); 107
SqlCommand Cmd = new SqlCommand(XSqlString,Connection); 108
SCount = Cmd.ExecuteScalar().ToString().Trim(); 109
if (SCount=="") 110
SCount="0"; 111
Close(); 112
return Convert.ToInt32(SCount); 113
} 114
115
//公有方法,根据XWhere更新数据表XTableName中的某些纪录 116
//XTableName--表名 117
//XHT--哈希表,键为字段名,值为字段值 118
public DataSet AdvancedSearch(string XTableName, Hashtable XHT) 119
{ 120
int Count = 0; 121
122
string Fields = ""; 123
foreach(DictionaryEntry Item in XHT) 124
{ 125
if (Count != 0) 126
{ 127
Fields += " and "; 128
} 129
Fields += Item.Key.ToString(); 130
Fields += " like '%"; 131
Fields += Item.Value.ToString(); 132
Fields += "%'"; 133
Count++; 134
} 135
Fields += " "; 136
137
string SqlString = "select * from " + XTableName + " where " + Fields; 138
Open(); 139
SqlDataAdapter Adapter = new SqlDataAdapter(SqlString, Connection); 140
DataSet Ds = new DataSet(); 141
Adapter.Fill(Ds); 142
Close(); 143
return Ds; 144
145
} 146
147
//私有方法,获得一个用来调用存储过程的SqlCommand 148
//输入: 149
// ProcName - 存储过程名 150
// Params - 用来调用存储过程的参数表 151
private SqlCommand CreateCommand(string ProcName, SqlParameter[] Prams) 152
{ 153
Open(); 154
SqlCommand Cmd = new SqlCommand(ProcName, Connection); 155
Cmd.CommandType = CommandType.StoredProcedure; 156
157
if (Prams != null) 158
{ 159
foreach (SqlParameter Parameter in Prams) 160
Cmd.Parameters.Add(Parameter); 161
} 162
163
return Cmd; 164
} 165
166
//公有方法,实例化一个用于调用存储过程的参数 167
//输入: 168
// ParamName - 参数名称 169
// DbType - 参数类型 170
// Size - 参数大小 171
// Direction - 传递方向 172
// Value - 值 173
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value) 174
{ 175
SqlParameter Param; 176
177
if(Size > 0) 178
Param = new SqlParameter(ParamName, DbType, Size); 179
else Param = new SqlParameter(ParamName, DbType); 180
181
Param.Direction = Direction; 182
183
if (Value != null) 184
Param.Value = Value; 185
186
return Param; 187
} 188
189
//公有方法,实例化一个用于调用存储过程的输入参数 190
//输入: 191
// ParamName - 参数名称 192
// DbType - 参数类型 193
// Size - 参数大小 194
// Value - 值 195
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) 196
{ 197
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value); 198
} 199
200
//公有方法,调用存储过程(不带参数) 201
//输入: 202
// ProcName存储过程名 203
//输出: 204
// 对Update、Insert、Delete操作返回影响到的行数,其他情况为-1 205
public int RunProc(string ProcName) 206
{ 207
int Count = -1; 208
SqlCommand Cmd = CreateCommand(ProcName, null); 209
Count = Cmd.ExecuteNonQuery(); 210
Close(); 211
return Count; 212
} 213
214
//公有方法,调用存储过程(带参数) 215
//输入: 216
// ProcName - 存储过程名 217
// Params - 用来调用存储过程的参数表 218
//输出: 219
// 对Update、Insert、Delete操作返回影响到的行数,其他情况为-1 220
public int RunProc(string ProcName, SqlParameter[] Params) 221
{ 222
int Count = -1; 223
SqlCommand Cmd = CreateCommand(ProcName, Params); 224
Count = Cmd.ExecuteNonQuery(); 225
Close(); 226
return Count; 227
} 228
229
//公有方法,调用存储过程(不带参数) 230
//输入: 231
// ProcName存储过程名 232
//输出: 233
// 将执行结果以SqlDataReader返回 234
//注意:使用后主意调用SqlDataReader.Close()方法 235
public SqlDataReader RunProcGetReader(string ProcName) 236
{ 237
SqlCommand Cmd = CreateCommand(ProcName, null); 238
return Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); 239
} 240
241
//公有方法,调用存储过程(带参数) 242
//输入: 243
// ProcName - 存储过程名 244
// Params - 存储过程需要的参数 245
//输出: 246
// 将执行结果以SqlDataReader返回 247
//注意:使用后主意调用SqlDataReader.Close()方法 248
public SqlDataReader RunProcGetReader(string ProcName, SqlParameter[] Params) 249
{ 250
SqlCommand Cmd = CreateCommand(ProcName, Params); 251
return Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); 252
} 253
254
//公有方法,调用存储过程(带参数) 255
//输入: 256
// ProcName - 存储过程名 257
// Params - 存储过程需要的参数 258
//输出: 259
// 将执行结果以SqlDataReader返回 260
//注意:使用后主意调用SqlDataReader.Close()方法 261
public int RunProcGetCount(string ProcName, SqlParameter[] Params) 262
{ 263
SqlCommand Cmd = CreateCommand(ProcName, Params); 264
string SCount; 265
SCount = Cmd.ExecuteScalar().ToString().Trim(); 266
if (SCount == "") 267
SCount = "0"; 268
Close(); 269
return Convert.ToInt32(SCount); 270
} 271
272
//公有方法,调用存储过程(不带参数) 273
//输入: 274
// ProcName存储过程名 275
//输出: 276
// 将执行结果以DataSet返回 277
public DataSet GetDataSet(string ProcName) 278
{ 279
Open(); 280
SqlDataAdapter adapter = new SqlDataAdapter(ProcName, Connection); 281
DataSet dataset = new DataSet(); 282
adapter.Fill(dataset); 283
Close(); 284
return dataset; 285
} 286
287
//公有方法,调用存储过程(不带参数) 288
//输入: 289
// ProcName存储过程名 290
//输出: 291
// 将执行结果以DataSet返回 292
public DataSet GetDataSet(string ProcName, SqlParameter[] Params) 293
{ 294
Open(); 295
SqlCommand Cmd = CreateCommand(ProcName, Params); 296
SqlDataAdapter adapter = new SqlDataAdapter(Cmd); 297
DataSet dataset = new DataSet(); 298
adapter.Fill(dataset); 299
Close(); 300
return dataset; 301
} 302
303
} 304
} 305





}
}