asp.net登錄註冊頁面

在這裏插入圖片描述
數據庫:app,表:Student

db.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
///
/// db 的摘要說明
///
public class db
{
public db()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public static SqlConnection CreateConnection()
{
SqlConnection con = new SqlConnection(“Data Source=PC-201510151352\MSSQLSERVER1;Initial Catalog=app;User Id=sa;Pwd=1234”);
return con;

}  

}

Login.aspx文件

<%@ Page Language=“C#” AutoEventWireup=“true” CodeFile=“Login.aspx.cs” Inherits="_Default" %>

用戶登錄頁面

用戶名:

  密 碼:

     

Login.aspx.cs文件

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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnLogin_Click(object sender, EventArgs e)
{
    if ((tbusername.Text == "") || (tbpsw.Text == ""))
    {
        Response.Write(@"<script>alert('用戶名與密碼不能爲空!');</script>");


    }
    else
    {
        SqlConnection con = db.CreateConnection();
        con.Open();
        string strSql = "select password  from Student where username='" + tbusername.Text + "'";
        SqlCommand cmd = new SqlCommand(strSql, con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(strSql, con);
        da.Fill(ds, "Student");
        try
        {
            if (tbpsw.Text == ds.Tables[0].Rows[0].ItemArray[0].ToString().Trim())
            {
                string curuser = tbusername.Text;
                Response.Write(@"<script>alert('登錄成功,歡迎你!');</script>");
            }
            else
            {
                Response.Write(@"<script>alert('用戶名或者密碼錯誤!');</script>");
            }
        }
        catch
        {
            Response.Write(@"<script>alert('Sorry!你輸入的用戶名不存在!');</script>");


        }
        con.Close();

    } 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /*
    if (tbusername.Text == "")
    {
        Response.Write(@"<script>alert('用戶名不能爲空!');</script>");
        goto abc;
    }


    if (tbpsw.Text == "")
    {
        Response.Write(@"<script>alert('密碼不能爲空!');</script>");
        goto abc;
    }
    string username = tbusername.Text;
    string password = tbpsw.Text;
   
    String connstr = "Data Source=PC-201510151352\\MSSQLSERVER1;Initial Catalog=app;User Id=sa;Pwd=1234";
    string sql = "select * from Student where username=@username and password=@password";
    SqlParameter[] parameters = { new SqlParameter("@username", username), new SqlParameter("@password", password) };
    using (SqlConnection conn = new SqlConnection(connstr))
    {
        conn.Open();
        using (SqlCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = sql;
            cmd.Parameters.AddRange(parameters);
            DataSet ds = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds);
            DataTable table = ds.Tables[0];
            if (table == null)
                Response.Write(@"<script>alert('登錄成功!');</script>");
            else
                Response.Write(@"<script>alert('登錄失敗!');</script>");


        }
    }
abc: ;
    */

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

}


protected void btnRegister_Click(object sender, EventArgs e)
{
    if((tbusername.Text=="")||(tbpsw.Text==""))  
   {  
        Response.Write(@"<script>alert('用戶名與密碼不能爲空!');</script>");
   }  
   else  
   {  
       try  
       {  
           SqlConnection con = db.CreateConnection();  
           con.Open();  
           string strsql = "insert into  Student  values('" + tbusername.Text + "','" + tbpsw.Text + "')";  
           SqlCommand cmd=new SqlCommand(strsql,con);  
           cmd.ExecuteNonQuery();  
           con.Close();  
           tbusername.Text="";  
           tbpsw.Text="";  
           Response.Write(@"<script>alert('註冊成功!歡迎登錄!');</script>");
       }  
       catch  
       {  
           Response.Write(@"<script>alert('用戶名已存在!');</script>");
       }  
   }  

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /*
    String connstr = "Data Source=PC-201510151352\\MSSQLSERVER1;Initial Catalog=app;User Id=sa;Pwd=1234";
    SqlConnection con = new SqlConnection();
    con.ConnectionString = connstr;
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    String sql = String.Format("Insert into Student values('" + tbusername.Text + "','" + tbpsw.Text + "')");
    cmd.CommandText = sql;
    cmd.ExecuteNonQuery();
    Response.Write(@"<script>alert('註冊成功!');</script>"); 
    //   Response.Write(@"<script>alert('註冊成功!');window.location='Register.aspx';</script>");
    con.Close();
     */

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

}

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