Asp.net源码专业站
首页->商务办公->Asp.net简单公文流转系统(MVC)源码>>App-Code/DataAccessLayeer/Database.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Asp.net简单公文流转系统(MVC)源码
当前文件:文件类型 GongWenLiuZhuan/App_Code/DataAccessLayeer/Database.cs[7K,2009-6-12 11:43:27]打开代码结构图
普通视图
		            
1using System; 2using System.ComponentModel; 3using System.Collections; 4using System.Diagnostics; 5using System.Data; 6using System.Data.SqlClient; 7using System.Configuration; 8 9using MyOA.DataAccessHelper; 10using MyOA.CommonComponent; 11 12namespace MyOA.DataAccessLayer 13{ 14 /// <summary> 15 /// 类,用于数据访问的类。 16 /// </summary> 17 public class Database : IDisposable 18 { 19 /// <summary> 20 /// 保护变量,数据库连接。 21 /// </summary> 22 protected SqlConnection Connection; 23 24 /// <summary> 25 /// 保护变量,数据库连接串。 26 /// </summary> 27 protected String ConnectionString; 28 29 /// <summary> 30 /// 系统日志对象,日志来源为“MyOA.Database” 31 /// </summary> 32 protected MyEventsLog log = new MyEventsLog("MyOA.Database"); 33 34 /// <summary> 35 /// 构造函数。 36 /// </summary> 37 /// <param name="DatabaseConnectionString">数据库连接串</param> 38 public Database() 39 { 40 ConnectionString = ConfigurationManager.AppSettings["DBConnectionString"]; 41 } 42 43 /// <summary> 44 /// 析构函数,释放非托管资源 45 /// </summary> 46 ~Database() 47 { 48 try 49 { 50 if (Connection != null) 51 Connection.Close(); 52 } 53 catch(Exception e) 54 { 55 log.WriteLog(EventLogEntryType.Warning,"Close失败,系统异常信息:"+e.Message); 56 } 57 try 58 { 59 Dispose(); 60 } 61 catch{} 62 } 63 64 /// <summary> 65 /// 保护方法,打开数据库连接。 66 /// </summary> 67 protected void Open() 68 { 69 if (Connection == null) 70 { 71 try 72 { 73 Connection = new SqlConnection(ConnectionString); 74 } 75 catch(Exception e) 76 { 77 log.WriteLog(EventLogEntryType.Error,"创建数据库连接失败,系统异常信息:"+e.Message); 78 } 79 } 80 if (Connection.State.Equals(ConnectionState.Closed)) 81 { 82 try 83 { 84 Connection.Open(); 85 } 86 catch(Exception e) 87 { 88 log.WriteLog(EventLogEntryType.Error,"打开数据库连接失败,系统异常信息:"+e.Message); 89 } 90 } 91 } 92 93 /// <summary> 94 /// 公有方法,关闭数据库连接。 95 /// </summary> 96 public void Close() 97 { 98 try 99 { 100 if (Connection != null) 101 Connection.Close(); 102 } 103 catch(Exception e) 104 { 105 log.WriteLog(EventLogEntryType.Warning,"Close失败,系统异常信息:"+e.Message); 106 } 107 } 108 109 /// <summary> 110 /// 公有方法,释放资源。 111 /// </summary> 112 public void Dispose() 113 { 114 // 确保连接被关闭 115 try 116 { 117 if (Connection != null) 118 { 119 Connection.Dispose(); 120 Connection = null; 121 } 122 } 123 catch(Exception e) 124 { 125 log.WriteLog(EventLogEntryType.Warning,"Dispose失败,系统异常信息:"+e.Message); 126 } 127 } 128 129 /// <summary> 130 /// 公有方法,获取数据,返回一个SqlDataReader (调用后主意调用SqlDataReader.Close())。 131 /// </summary> 132 /// <param name="SqlString">Sql语句</param> 133 /// <returns>SqlDataReader</returns> 134 public SqlDataReader GetDataReader(String SqlString) 135 { 136 Open(); 137 try 138 { 139 SqlCommand cmd = new SqlCommand(SqlString,Connection); 140 return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); 141 } 142 catch(Exception e) 143 { 144 log.WriteLog(EventLogEntryType.Error,"GetDataReader失败,SqlString="+SqlString+",系统异常信息:"+e.Message); 145 return null; 146 } 147 } 148 149 /// <summary> 150 /// 公有方法,获取数据,返回一个DataSet。 151 /// </summary> 152 /// <param name="SqlString">Sql语句</param> 153 /// <returns>DataSet</returns> 154 public DataSet GetDataSet(String SqlString) 155 { 156 DataSet dataset = new DataSet(); 157 Open(); 158 try 159 { 160 SqlDataAdapter adapter = new SqlDataAdapter(SqlString,Connection); 161 adapter.Fill(dataset); 162 } 163 catch(Exception e) 164 { 165 log.WriteLog(EventLogEntryType.Warning,"GetDataSet失败,SqlString="+SqlString+",系统异常信息:"+e.Message); 166 } 167 finally 168 { 169 Close(); 170 } 171 return dataset; 172 } 173 174 /// <summary> 175 /// 公有方法,获取数据,返回一个DataTable。 176 /// </summary> 177 /// <param name="SqlString">Sql语句</param> 178 /// <returns>DataTable</returns> 179 public DataTable GetDataTable(String SqlString) 180 { 181 DataSet dataset = GetDataSet(SqlString); 182 dataset.CaseSensitive = false; 183 return dataset.Tables[0]; 184 } 185 186 /// <summary> 187 /// 公有方法,获取数据,返回一个DataRow。 188 /// </summary> 189 /// <param name="SqlString">Sql语句</param> 190 /// <returns>DataRow</returns> 191 public DataRow GetDataRow(String SqlString) 192 { 193 DataSet dataset = GetDataSet(SqlString); 194 dataset.CaseSensitive = false; 195 if (dataset.Tables[0].Rows.Count>0) 196 { 197 return dataset.Tables[0].Rows[0]; 198 } 199 else 200 { 201 return null; 202 } 203 } 204 205 /// <summary> 206 /// 公有方法,执行Sql语句。 207 /// </summary> 208 /// <param name="SqlString">Sql语句</param> 209 /// <returns>对Update、Insert、Delete为影响到的行数,其他情况为-1</returns> 210 public int ExecuteSQL(String SqlString) 211 { 212 int count = -1; 213 Open(); 214 try 215 { 216 SqlCommand cmd = new SqlCommand(SqlString,Connection); 217 count = cmd.ExecuteNonQuery(); 218 } 219 catch(Exception e) 220 { 221 log.WriteLog(EventLogEntryType.Error,"ExecuteSQL失败,SqlString="+SqlString+",系统异常信息:"+e.Message); 222 count = -1; 223 } 224 finally 225 { 226 Close(); 227 } 228 return count; 229 } 230 231 /// <summary> 232 /// 公有方法,执行一组Sql语句。 233 /// </summary> 234 /// <param name="SqlStrings">Sql语句组</param> 235 /// <returns>是否成功</returns> 236 public bool ExecuteSQL(String[] SqlStrings) 237 { 238 bool success = true; 239 Open(); 240 SqlCommand cmd = new SqlCommand(); 241 SqlTransaction trans = Connection.BeginTransaction(); 242 cmd.Connection = Connection; 243 cmd.Transaction = trans; 244 245 int i=0; 246 try 247 { 248 foreach (String str in SqlStrings) 249 { 250 cmd.CommandText = str; 251 cmd.ExecuteNonQuery(); 252 i++; 253 } 254 trans.Commit(); 255 } 256 catch(Exception e) 257 { 258 log.WriteLog(EventLogEntryType.Error,"ExecuteSQL失败,SqlString="+SqlStrings[i]+",系统异常信息:"+e.Message); 259 success = false; 260 trans.Rollback(); 261 } 262 finally 263 { 264 Close(); 265 } 266 return success; 267 } 268 269 /// <summary> 270 /// 公有方法,在一个数据表中插入一条记录。 271 /// </summary> 272 /// <param name="TableName">表名</param> 273 /// <param name="Cols">哈西表,键值为字段名,值为字段值</param> 274 /// <returns>是否成功</returns> 275 public bool Insert(String TableName,Hashtable Cols) 276 { 277 int Count = 0; 278 279 if (Cols.Count<=0) 280 { 281 return true; 282 } 283 284 String Fields = " ("; 285 String Values = " Values("; 286 foreach(DictionaryEntry item in Cols) 287 { 288 if (Count!=0) 289 { 290 Fields += ","; 291 Values += ","; 292 } 293 Fields += "["+item.Key.ToString()+"]"; 294 Values += item.Value.ToString(); 295 Count ++; 296 } 297 Fields += ")"; 298 Values += ")"; 299 300 String SqlString = "Insert into "+TableName+Fields+Values; 301 302 String[] Sqls = {SqlString}; 303 return ExecuteSQL(Sqls); 304 } 305 306 307 /// <summary> 308 /// 公有方法,更新一个数据表。 309 /// </summary> 310 /// <param name="TableName">表名</param> 311 /// <param name="Cols">哈西表,键值为字段名,值为字段值</param> 312 /// <param name="Where">Where子句</param> 313 /// <returns>是否成功</returns> 314 public bool Update(String TableName,Hashtable Cols,String Where) 315 { 316 int Count = 0; 317 if (Cols.Count<=0) 318 { 319 return true; 320 } 321 String Fields = " "; 322 foreach(DictionaryEntry item in Cols) 323 { 324 if (Count!=0) 325 { 326 Fields += ","; 327 } 328 Fields += "["+item.Key.ToString()+"]"; 329 Fields += "="; 330 Fields += item.Value.ToString(); 331 Count ++; 332 } 333 Fields += " "; 334 335 String SqlString = "Update "+TableName+" Set "+Fields+Where; 336 337 String[] Sqls = {SqlString}; 338 return ExecuteSQL(Sqls); 339 } 340 } 341}
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:Asp.net简单公文流转系统(MVC)源码
51Aspx.com 版权所有 CopyRight © 2006-2010. 京ICP备06046876号 本站法律顾问:ITlaw-庄毅雄律师
返回顶部
客户服务:点击这里进行客户咨询 业务合作:点击这里洽谈业务合作 合作热线:010-68880146