簡單的登入註冊介面測試(asp.net)
阿新 • • 發佈:2019-01-01
asp.net編寫登入註冊程式碼
win7
asp.net+ms sql 2005+vs2010(開發平臺)
資料庫名userinfo
username varchar(50)
login.aspx程式碼:
win7
asp.net+ms sql 2005+vs2010(開發平臺)
資料庫名userinfo
username varchar(50)
userpassword varchar(50)
連線資料庫,寫在類db.cs裡面,其程式碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; /// <summary> ///db 的摘要說明 /// </summary> public class db { public db() { // //TODO: 在此處新增建構函式邏輯 // } public static SqlConnection CreateConnection() { SqlConnection con = new SqlConnection("server=localhost\\SQL2005;database=userinfo;uid=sa;pwd=1111qq;"); return con; } }
login.aspx程式碼:
login.aspx.cs程式碼:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>登入介面</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="使用者"></asp:Label> <asp:TextBox ID="labuser" runat="server"></asp:TextBox><br /> <asp:Label ID="Label2" runat="server" Text="密碼"></asp:Label> <asp:TextBox ID="labpwd" runat="server" TextMode="Password" AutoCompleteType="BusinessZipCode" Width="145px"></asp:TextBox><br /> <asp:Button ID="allow" runat="server" OnClick="allow_Click" Text="確定" /> <asp:Button ID="abov" runat="server" Text="註冊" OnClick="abov_Click" /><br /> <br /> <asp:Label ID="Label3" runat="server"></asp:Label><br /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void allow_Click(object sender, EventArgs e) { if((labuser.Text=="")||(labpwd.Text=="")) { Label3.Text="使用者名稱與密碼不能為空!"; } else { SqlConnection con = db.CreateConnection(); con.Open(); string strSql = "select password from userinfo where username='"+labuser.Text+"'"; SqlCommand cmd = new SqlCommand(strSql, con); DataSet ds =new DataSet(); SqlDataAdapter da =new SqlDataAdapter(strSql,con); da.Fill(ds,"mytable"); try { if(labpwd.Text==ds.Tables[0].Rows[0].ItemArray[0].ToString().Trim()) { string curuser=labuser.Text; Label3.Text="登入成功,歡迎你!"; } else { Label3.Text="使用者名稱或者密碼錯誤!"; } } catch { Label3.Text="Sorry!你輸入的使用者名稱不存在!"; } con.Close(); } } protected void abov_Click(object sender, EventArgs e) { if((labuser.Text=="")||(labpwd.Text=="")) { Label3.Text="使用者名稱為或密碼不能為空"; } else { try { SqlConnection con = db.CreateConnection(); con.Open(); string strsql = "insert into userinfo values('" + labuser.Text + "','" + labpwd.Text + "')"; SqlCommand cmd=new SqlCommand(strsql,con); cmd.ExecuteNonQuery(); con.Close(); labuser.Text=""; labpwd.Text=""; Label3.Text="註冊成功!歡迎登入!"; } catch { Label3.Text="使用者名稱已存在!"; } } } }