十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
using Sqystem.Data; using System.Data.SqlClient; using System.Configuration; //引用一个库Ststem.configuration //修改根目录的web.config文件//最外层节点 ...... //数据库通用访问类 class SQLHelper { //数据库连接字符串 private static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString(); //写入日志文件 public void WriteLog(string msg) { FileStream fs = FileStream("1.log",fileMode.Append); StreamWriter sw = new StreamWriter(fs); sw.writeLine(DateTime.Now.ToString()+" "+ msg); sw.Close(); fs.Close(); } //数据库更新 public static int Update(string sql) { SqlConnection conn = new SqlConnection(connString); SqlConnection cmd = new SqlCommand(sql,conn); try { conn.Open(); return cmd.ExecuteNoQuery(); } catch(Exception ex) { WriteLog("执行更新时发生异常"+ex.Message); throw ex; } finally { conn.Close(); } } public static object GetSingleResult(string sql) { Sqlconnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql,conn); try { conn.Open(); return cmd.ExecuteScalar(); } catch(Exception ex) { WriteLog("执行单一结果查询"+ex.message); throw ex; } finally { conn.Close(); } } //返回结果集 public static SqlDataReader GetReader(string sql) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql,conn); try { conn.Open(); retrun cmd.ExecuteReader(CommandBehavior.CloseConnection);//不能在此关闭连接 } catch(Exception ex) { WriteLog("读取结果集发生异常"+ex.message); throw ex; } } //带参数的存储过程 public static int Update(string sqlOrProcedureName,SqlParameter[]param,bool isProcedure) { SqlConnection conn = new SqlConnection(connString); SqlConnection cmd = new SqlCommand(sqlOrProcedureName,conn); if(isProcedure) { cmd.CommandType = CommmandType.StoredProcedure; } try { conn.Open(); cmd.Parameters.AddRang(param); return cmd.ExecuteNoQuery(); } catch(Exception ex) { WriteLog("执行更新时发生异常"+ex.Message); throw ex; } finally { conn.Close(); } } }......
//DAL调用数据访问模块
专业成都网站建设公司,做排名好的好网站,排在同行前面,为您带来客户和效益!创新互联建站为您提供成都网站建设,五站合一网站设计制作,服务好的网站设计公司,网站设计、做网站负责任的成都网站制作公司!
class AdminService { public UseInfo Login(UseInfo objUser) { string sql = "select UserName from UserTable where loginid=@LoginId and LoginPwn=@LoginPwn"; SqlParameter[] param = new SqlParameter[]; { new SqlPrameter("@LoginId",UseInfo.LoginId), new SqlParameter("@LoginPwd",UseInfo.LoginPwd) }; try { SqlDataReader objReader = SQLHelper.GetReader(sql,param,false); if(objReader.Read()) { objUser.UserName = objReader["UserName"].ToString(); } else { objUser = null; } objReader.Close(); } catch(Exception ex) { throw new Exception("用户登录异常"+ex.message); } return objUser; } }