2017年12月13日 星期三

ASP 登入方法

  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Data.SqlClient;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Configuration;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10.   
  11. public partial class LOGIN : System.Web.UI.Page  
  12. {  
  13.     List<Account> accounts;  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.         SqlConnection con = new SqlConnection();  
  17.         SqlCommand cmd = null;  
  18.         SqlDataReader reader = null;  
  19.         try  
  20.         {  
  21.             //step1: 連接資料庫   ps:ConnectionStrings[資料庫名稱ConnectionString]  
  22.             con.ConnectionString = WebConfigurationManager.ConnectionStrings["xinConnectionString"].ConnectionString;  
  23.             //開始連接  
  24.             con.Open();  
  25.             //下SQL指令  讀取資料  
  26.             cmd = new SqlCommand("SELECT * FROM[Accounts]",con);  
  27.             //執行,並將讀取的資料放入reader  
  28.             reader = cmd.ExecuteReader();  
  29.             //使用集合 ,將所有讀取到的資料放入裡面  
  30.             accounts = new List<Account>();  
  31.             //讀取  
  32.             while (reader.Read())  
  33.             {  
  34.                 accounts.Add(new Account(reader["account"].ToString(), reader["password"].ToString()));    
  35.             }  
  36.         }  
  37.         catch (Exception)  
  38.         {  
  39.             //發生錯誤會跑來這  
  40.             Response.Write("<Script language='Javascript'>");  
  41.             Response.Write("alert('NOT FOUND')");  
  42.             Response.Write("</" + "Script>");  
  43.         }  
  44.         finally  
  45.         {  
  46.             //執行完後關閉資料庫  
  47.             con.Close();  
  48.         }  
  49.         
  50.           
  51.     }  
  52.   
  53.   
  54.     //登入按鈕  
  55.     protected void Button1_Click1(object sender, EventArgs e)  
  56.     {  
  57.         //使用者輸入的 帳號 密碼  
  58.         string account = TextBox1.Text, password = TextBox2.Text;  
  59.         //宣告布林 判斷是否登入成功  
  60.         Boolean isSuccess = false;  
  61.         //透過迴圈走訪accounts集合  
  62.         foreach (Account item in accounts)  
  63.         {  
  64.             //假如有其中一筆帳戶核對成功  
  65.             if (account.Equals(item.account)&&password.Equals(item.password))  
  66.             {  
  67.                 isSuccess = true;  
  68.                 Response.Write("<Script language='Javascript'>alert('登入成功')</" + "Script>");  
  69.                 // 之後轉頁面操作  
  70.                 break;//跳出迴圈  
  71.             }  
  72.               
  73.         }  
  74.         if (!isSuccess)//假如沒核對成功的帳戶  
  75.         {  
  76.             Response.Write("<Script language='Javascript'>alert('登入失敗')</" + "Script>");  
  77.         }  
  78.     }  
  79. }  
  80. //宣告CLASS 類別  
  81. class Account//帳戶  
  82. {  
  83.    public string account , password;  
  84.    public Account(string acc, string pass)  
  85.     {  
  86.         account = acc;  
  87.         password = pass;  
  88.     }  
  89.   
  90. }  

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。