在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
源代码:13033480群共享 一、登录界面的逻辑结构 1、基本的数据库连接代码: protected void btnConfirm_Click(object sender, EventArgs e) { bool blIsAuthenticated = false;
string strConnection = ConfigurationManager.ConnectionStrings["SuperMarketDBConnectionString"].ConnectionString.ToString();
SqlConnection conn = new SqlConnection(); conn.ConnectionString = strConnection;
SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT hy_Username FROM T_Vip WHERE hy_Username='" + txtUserName.Text +"' AND hy_Password='" + txtPassword.Text+"'";
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.Read()) { blIsAuthenticated = true; } rdr.Close(); conn.Close();
if (blIsAuthenticated == true) { Response.Redirect("Welcome.aspx"); } else { Response.Write("<script>alert('用户名或密码错误!')</script>"); } } 这个代码中使用了一个布尔变量blIsAuthenticated,来判断数据库操作的成功与否,而没有在数据库操作过程中添加过多的逻辑结构,在数据库操作结束后,再根据这个变量,进行你想要的逻辑操作。 2、try…catch…finally结构 protected void btnConfirm_Click(object sender, EventArgs e) { bool blIsAuthenticated = false;
string strConnection = ConfigurationManager.ConnectionStrings["SuperMarketDBConnectionString"].ConnectionString.ToString();
SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(strConnection); try { conn.Open();
cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT hy_Username FROM T_Vip WHERE hy_Username='" + txtUserName.Text +"' AND hy_Password='" + txtPassword.Text +"'";
SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.Read()) { blIsAuthenticated = true; } rdr.Close(); } finally { conn.Close(); }
if (blIsAuthenticated == true) { Session["UserName"] = txtUserName.Text; Response.Redirect("Welcome.aspx"); } else { Response.Write("<script>alert('用户名或密码错误!')</script>"); } } 为了说明下面的using()结构,代码中没有用使用catch关键字,注册中有采用。这个结构的主要目的,是确保SqlConnection在使用后能及时Close(),并Dispose()。 3、using()结构 public partial class Login3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
} protected void btnConfirm_Click(object sender, EventArgs e) { bool blIsAuthenticated =false;
string strConnection =ConfigurationManager.ConnectionStrings["SuperMarketDBConnectionString"].ConnectionString.ToString();
SqlCommand cmd = new SqlCommand(); using (SqlConnection conn =newSqlConnection(strConnection)) { conn.Open();
cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT hy_Username FROM T_Vip WHERE hy_Username='" + txtUserName.Text +"' AND hy_Password='" + txtPassword.Text +"'";
SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.Read()) { blIsAuthenticated = true; } rdr.Close(); }
if (blIsAuthenticated ==true) { Session["UserName"] = txtUserName.Text; Response.Redirect("Welcome.aspx"); } else { Response.Write("<script>alert('用户名或密码错误!')</script>"); } } } 简单地理解,using()结构,就是catch…finally结构的另一种形式而已。 二、登录界面的逻辑结构 protected void btnRegist_Click(object sender, EventArgs e) { if (Page.IsValid) { string strConnection =ConfigurationManager.ConnectionStrings["SuperMarketDBConnectionString"].ConnectionString; string cmdText = "insert into t_Vip(hy_Username,hy_Password,hy_Name,hy_Sex,hy_Age,hy_Mobliephone,hy_Email,hy_QQ) values(@hy_Username,@hy_Password,@hy_Name,@hy_Sex,@hy_Age,@hy_Mobliephone,@hy_Email,@hy_QQ)";
string username = txtUserName.Text; string password = txtPassword.Text; string truename = txtTrueName.Text; string sex = radlSex.SelectedValue; int age = Convert.ToInt16(txtAge.Text); string phone = txtPhone.Text; string email = txtEmail.Text; string qq = txtQQ.Text;
SqlParameter[] parms ={ new SqlParameter("@hy_Username", SqlDbType.VarChar, 20), new SqlParameter("@hy_Password", SqlDbType.VarChar, 20), new SqlParameter("@hy_Name", SqlDbType.VarChar, 20), new SqlParameter("@hy_Sex", SqlDbType.VarChar, 2), new SqlParameter("@hy_Age", SqlDbType.Int), new SqlParameter("@hy_Mobliephone", SqlDbType.VarChar,20), new SqlParameter("@hy_Email", SqlDbType.VarChar, 50), new SqlParameter("@hy_QQ", SqlDbType.VarChar, 20)};
parms[0].Value = username; parms[1].Value = password; parms[2].Value = truename; parms[3].Value = sex; parms[4].Value = age; parms[5].Value = phone; parms[6].Value = email; parms[7].Value = qq;
SqlCommand cmd = new SqlCommand(); SqlConnection conn =newSqlConnection(strConnection); try { if (conn.State !=ConnectionState.Open) conn.Open();
cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = cmdText;
foreach (SqlParameter parmin parms) cmd.Parameters.Add(parm);
cmd.ExecuteNonQuery(); } catch { Response.Redirect("ErrorMessage.aspx"); } finally { conn.Close(); }
Response.Redirect("Login.aspx"); } } } 登录界面的逻辑结构,主要多了一个SqlCommand对象要使用的SqlParmeter参数,这个变量,采用数组比较好,便于以后把数据库操作部分放在函数中,放在类中时的参数传递。 三、界面设计 1、配色方案:
2、格式设计 主要遵循了这个几个原则: (1)对于象登录、注册这样局部的布局采用表格+CSS结构,事实上ASP.NET的很多Web控件,都采用的表格。 (2)共性的样子,如配色方案的颜色都放在了CSS样式表中,个性的样式,及个别CSS不能设置的样式如表格的行高,使用了表格的属性。 (3)按照Stephen Walther(《ASP.NET2.0揭秘》一书的作者)的说法,舍弃了皮肤文件设置样式的方法,而采用了cssClass,把控件的样式也放在了CSS中。
|
请发表评论