html:
帳號: Textbox1
密碼: Textbox2
Lable1
Button1
SqlDataSource1
單純的兩個Textbox的輸入帳號密碼
Textbox1 id =username
Textbox1 id =password
Textbox1 id =username
Textbox1 id =password
Lable負責顯示輸入帳號密碼錯誤
屬性Text不設 Visible設為True讓他不顯示
Button登入
SqlDataSource1連接資料庫產生連接字串到web.config
aspx.cs:
public partial class login : System.Web.UI.Page
{
//連接web.config 資料庫
//連接web.config 資料庫
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["web.config Add資料庫的name"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//ADO.NET基本的用法
SqlCommand comm = new SqlCommand();
comm.CommandText = "select * from login where name=@name and password=@password";
comm.Connection = conn;
comm.Parameters.AddWithValue("@name", username.Text);
comm.Parameters.AddWithValue("@password", password.Text);
conn.Open();
//ADO.net 方法
SqlDataReader rd = comm.ExecuteReader();
if (rd.HasRows)
{
comm.Cancel();
rd.Close();
conn.Close();
conn.Dispose();
Session["Login"] = "OK";
Response.Redirect("Family.aspx");
}
else
{
Label1.Text = "帳號或密碼錯誤";
comm.Cancel();
rd.Close();
conn.Close();
conn.Dispose();
}
}
}
沒有留言:
張貼留言