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





}