日期:2014-05-17 浏览次数:20509 次
UI层: protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) //两参按钮 { string username = TextBox1.Text.Trim(); string userpwd = TextBox2.Text.Trim(); DataTable dt = new P_AdminManager().SelectAdmin(username, userpwd); int i = dt.Rows.Count; if (i > 0) { TextBox1.Text = Convert.ToString(i); TextBox2.Text = "登录成功"; } else { TextBox1.Text = Convert.ToString(i); TextBox2.Text = "登录失败"; } protected void Button2_Click(object sender, EventArgs e) //一参按钮 { string username = TextBox1.Text.Trim(); string userpwd = TextBox2.Text.Trim(); DataTable dt = new P_AdminManager().SelectByUser(username);// int i = dt.Rows.Count; if (i > 0) { TextBox1.Text = Convert.ToString(i); TextBox2.Text = "登录成功"; } else { TextBox1.Text = Convert.ToString(i); TextBox2.Text = "登录失败"; } } } BLL层: public class P_AdminManager { private P_AdminDAO P_dao = null; public P_AdminManager() { P_dao = new P_AdminDAO(); } public DataTable SelectByUser(string un) //单参 { return P_dao.SelectByUser(un); } public DataTable SelectAdmin(string username, string userpwd)//双参 { return P_dao.SelectAdmin(username,userpwd); } } DLL层: public class P_AdminDAO { private SQLHelper sqlhelper; public P_AdminDAO() { sqlhelper = new SQLHelper(); } //单参―――――――――――――――――――――――― public DataTable SelectByUser(string un) { DataTable dt = new DataTable(); string cmdText = "select * from P_Admin where P_adminame=@P_adminame"; SqlParameter[] para = new SqlParameter[] { new SqlParameter("@P_adminame", un) }; dt = sqlhelper.ExecuteQuery(cmdText, para, CommandType.Text); return dt; } //双参――――――――――――――――――――――――――― public DataTable SelectAdmin(string username, string userpwd) { DataTable dt = new DataTable(); string cmdText = "select * from P_Admin where P_adminame=@P_adminame and P_adminpwd=@P_adminpwd";// SqlParameter[] paras = new SqlParameter[]{ new SqlParameter("@P_adminame",username), new SqlParameter("@P_adminpwd",userpwd)};// dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.Text); return dt; } } Sqlhelper类: public class SQLHelper { private SqlConnection conn = null; private SqlCommand cmd = null; private SqlDataReader sdr = null; public SQLHelper() { string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; conn = new SqlConnection(connStr); } public SqlConnection GetConn() { if (conn.State == ConnectionState.Closed) { conn.Open(); } return conn; } //带参查询 public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct) { DataTable dt = new DataTable(); cmd = new SqlCommand(cmdText, GetConn()); cmd.CommandType = ct; cmd.Parameters.AddRange(paras); using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { dt.Load(sdr); } re