您目前尚未登陆,请选择【登陆】或【注册
首页->留言本类->51aspx修正版简单三层留言板源码>>MVC/DataBaseControlLayer/DBControl.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:51aspx修正版简单三层留言板源码
当前文件:文件类型 LeaveMessageMVC/MVC/DataBaseControlLayer/DBControl.cs打开代码结构图
普通视图
		            
1using System; 2using System.Data; 3using System.Configuration; 4using System.Web; 5using System.Web.Security; 6using System.Web.UI; 7using System.Web.UI.WebControls; 8using System.Web.UI.WebControls.WebParts; 9using System.Web.UI.HtmlControls; 10using System.Data.SqlClient; 11using System.Data.OleDb; 12using LeaveMessageMVC.MVC.BusinessLogicLayer.ParameterInformation; 13using LeaveMessageMVC.MVC.BusinessLogicLayer; 14using LeaveMessageMVC.MVC.BusinessLogicLayer.SomeFunctions; 15 16namespace LeaveMessageMVC.MVC.DataBaseControlLayer 17{ 18 /// <summary> 19 /// DBControl 的摘要说明 20 /// </summary> 21 public static class DBControl 22 { 23 /// <summary> 24 /// 数据库 连接字符串 25 /// </summary> 26 /// 备注:如果 ConfigurationManager.AppSettings["xxx"] 指定的“xxx”在web.config中不存在, 27 /// 则 ConnectionString = null 28 static private string ConnectionString = ConfigurationManager.AppSettings["DBCONNECTIONSTRING"]; 29 30 /// <summary> 31 /// 数据库连接 对象(for SQL Server) 32 /// </summary> 33 static private SqlConnection LMDBConnection; 34 35 /// <summary> 36 /// 数据库连接 对象(for Access) 37 /// </summary> 38 static private OleDbConnection DBConnection; 39 40 /// <summary> 41 /// 判断是否为 Access数据库 连接 42 /// </summary> 43 static private bool AccessCnt = false; 44 45 /// <summary> 46 /// 用以保存 异常 信息的字符串 47 /// </summary> 48 static private Exception SomeExceptions = new Exception(); 49 50 /// <summary> 51 /// 获取异常信息 52 /// </summary> 53 public static Exception DBControlError 54 { 55 get 56 { 57 return SomeExceptions; 58 } 59 } 60 61 /// <summary> 62 /// 判断是否为Access连接的对外接口 63 /// </summary> 64 public static bool AccessConnection 65 { 66 get 67 { 68 return AccessCnt; 69 } 70 } 71 72 /// <summary> 73 /// 链接并打开数据库 74 /// </summary> 75 /// <returns>返回是否成功</returns> 76 public static bool DBOpen() 77 { 78 bool yesorno = true; 79 80 string JudgeAccessDB = "Microsoft.Jet.OLEDB.4.0"; 81 82 //如果连接字符串里含有"Microsoft.Jet.OLEDB.4.0"字样,则为Access连接 83 if (ConnectionString.Contains(JudgeAccessDB)) 84 { 85 AccessCnt = true; 86 try 87 { 88 if (LMDBConnection == null) 89 { 90 DBConnection = new OleDbConnection(ConnectionString); 91 } 92 if (DBConnection.State.Equals(ConnectionState.Closed)) 93 { 94 DBConnection.Open(); 95 } 96 } 97 catch (Exception DBOpenEx) 98 { 99 SomeExceptions = DBOpenEx; 100 yesorno = false; 101 } 102 } 103 else 104 { 105 //否则判定为SQL Server连接 106 try 107 { 108 if (LMDBConnection == null) 109 { 110 LMDBConnection = new SqlConnection(ConnectionString); 111 } 112 if (LMDBConnection.State.Equals(ConnectionState.Closed)) 113 { 114 LMDBConnection.Open(); 115 } 116 } 117 catch (Exception DBOpenEx) 118 { 119 SomeExceptions = DBOpenEx; 120 yesorno = false; 121 } 122 } 123 124 return yesorno; 125 126 } 127 128 /// <summary> 129 /// 关闭数据库 130 /// </summary> 131 public static void DBClose() 132 { 133 if (!AccessCnt) 134 { 135 if (LMDBConnection != null) 136 LMDBConnection.Close(); 137 } 138 else 139 { 140 if (DBConnection != null) 141 DBConnection.Close(); 142 } 143 } 144 145 /// <summary> 146 /// 获取 数据库 数据 for SQL Server(DataReader获取数据) 147 /// </summary> 148 /// <returns>返回查询到的数据</returns> 149 public static SqlDataReader DBSelect(string ReadCode) 150 { 151 SqlDataReader dr = null; 152 try 153 { 154 DBOpen(); 155 SqlCommand cmd = new SqlCommand(ReadCode, LMDBConnection); 156 dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 157 } 158 catch (Exception DBReaderEx) 159 { 160 SomeExceptions = DBReaderEx; 161 dr = null; 162 } 163 //finally 164 //{ 165 // DBClose(); 166 //} 167 168 return dr; 169 } 170 171 /// <summary> 172 /// 获取 数据库 数据 for Access(DataAdapter读取数据并填充到DataSet) 173 /// </summary> 174 /// <returns>返回查询到的数据</returns> 175 public static DataSet aDBSelect(string ReadCode) 176 { 177 OleDbDataAdapter oda = new OleDbDataAdapter(); 178 try 179 { 180 DBOpen(); 181 oda.SelectCommand = new OleDbCommand(ReadCode, DBConnection); 182 } 183 catch (Exception DBReaderEx) 184 { 185 SomeExceptions = DBReaderEx; 186 oda = null; 187 } 188 189 DataSet ds = new DataSet(); 190 oda.Fill(ds); 191 192 return ds; 193 } 194 195 /// <summary> 196 /// 执行插入、更新、删除 197 /// </summary> 198 /// <param name="IUDCode">插入、修改、删除语句</param> 199 /// <returns>返回执行插入/更新/删除操作后影响的行数。0 为没有受影响函数;失败返回 -1</returns> 200 public static int DBExcuteIUD(string IUDCode) 201 { 202 int TriggerRows = -1; 203 try 204 { 205 DBOpen(); 206 if (!AccessCnt) 207 { 208 SqlCommand cmd = new SqlCommand(IUDCode, LMDBConnection); 209 TriggerRows = cmd.ExecuteNonQuery(); 210 } 211 else 212 { 213 OleDbCommand cmd = new OleDbCommand(IUDCode, DBConnection); 214 TriggerRows = cmd.ExecuteNonQuery(); 215 } 216 } 217 catch (Exception DBExcuteEx) 218 { 219 SomeExceptions = DBExcuteEx; 220 TriggerRows = -1; 221 } 222 finally 223 { 224 DBClose(); 225 } 226 227 return TriggerRows; 228 } 229 230 /// <summary> 231 /// 获取数据库中 表 的行数 232 /// </summary> 233 /// <returns></returns> 234 public static int TableCount() 235 { 236 int count = 0; 237 string SQL_SelectCount = "SELECT COUNT(*) FROM " + LMManage.Tables; 238 try 239 { 240 DBOpen(); 241 if (!AccessCnt) 242 { 243 SqlCommand cmd = new SqlCommand(SQL_SelectCount, LMDBConnection); 244 SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection); 245 while (rd.Read()) 246 count = rd.GetSqlInt32(0).Value; //这就是你要的表的总行数了! 247 rd.Close(); 248 } 249 else 250 { 251 OleDbCommand cmd = new OleDbCommand(SQL_SelectCount, DBConnection); 252 OleDbDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection); 253 while (rd.Read()) 254 count = rd.GetInt32(0); //这就是你要的表的总行数了! 255 rd.Close(); 256 } 257 } 258 catch (Exception DBExcuteEx) 259 { 260 SomeExceptions = DBExcuteEx; 261 count = -1; 262 } 263 finally 264 { 265 DBClose(); 266 } 267 268 return count; 269 } 270 271 /// <summary> 272 /// 获取数据库操作相关的异常信息,供管理员查看 273 /// </summary> 274 /// <param name="ex"></param> 275 /// <returns></returns> 276 public static string DBExceptionEvent() 277 { 278 return DBControlError.ToString(); 279 } 280 } 281} 282//该源码下载自www.51aspx.com(51aspx.com) 283//edit by 51-aspx
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:51aspx修正版简单三层留言板源码