Asp.Net登錄

//在asp.net裏頭做個登錄,用到ado.net方法和winform裏頭的一樣簡單

//拖三個控件,兩個TextBox一個button就行了,注意了初學者不要拖成html的控件

界面:

爲了養成習慣,我把連接字符串寫在了web.config文件裏頭了

代碼:

 

protected void Button1_Click(object sender, EventArgs e)
        {
            string id = TextBox1.Text.Trim();
            string pwd = TextBox2.Text.Trim();
            using (SqlConnection conn=new SqlConnection(connStr))
            {
                conn.Open();
                using(SqlCommand cmd=conn.CreateCommand())
	            {
                    cmd.CommandText="select count(*) from login where id=@id and pwd=@pwd";
                    SqlParameter pm=new SqlParameter("@id",id);
                    SqlParameter pm2=new SqlParameter("@pwd",pwd);
                    cmd.Parameters.Add(pm);
                    cmd.Parameters.Add(pm2);
		            object obj=cmd.ExecuteScalar();
                    if (Convert.ToInt32(obj)>0)
	                {
		                Response.Write("<script>alert('登錄成功!')</script>");
	                }
                    else
	                {
                        Response.Write("<script>alert('登錄失敗!')</script>");
	                }
	            }
            }
        }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章