您好,登錄后才能下訂單哦!
using Sqystem.Data; using System.Data.SqlClient; using System.Configuration; //引用一個庫Ststem.configuration //修改根目錄的web.config文件 <configuration>//最外層節點 ...... <connectionStrings> <add name="connString" connectionString = "Sqerver=.;DataBase=StudentManage;Uid=sa;Pwd=scale2018@"> </connectionStrings> ...... </configuration> //數據庫通用訪問類 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; } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。