您目前尚未登陆,请选择【登陆】或【注册
首页->行政办公->多功能在线考试系统源码>>App-Code/DataAccessLayer/DataBase.cs>>代码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:多功能在线考试系统源码


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

- 简单三层实现的无限级DropDo..

- 精品课管理系统源码

- Ajax入门的三个实用小例子

- 字符串操作大全(SHAI、MD5加..

- ShangduCmsNT1.5.0(集成SP1..

- 漂亮无限级分类源代码(三层..

- AspxCn无刷新整合型网站开源..

- 三层小型论坛系统源码

51Aspx.com 版权所有 CopyRight © 2000-2008. 京ICP备06046876号