- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.Configuration;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class LOGIN : System.Web.UI.Page
- {
- List<Account> accounts;
- protected void Page_Load(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection();
- SqlCommand cmd = null;
- SqlDataReader reader = null;
- try
- {
- //step1: 連接資料庫 ps:ConnectionStrings[資料庫名稱ConnectionString]
- con.ConnectionString = WebConfigurationManager.ConnectionStrings["xinConnectionString"].ConnectionString;
- //開始連接
- con.Open();
- //下SQL指令 讀取資料
- cmd = new SqlCommand("SELECT * FROM[Accounts]",con);
- //執行,並將讀取的資料放入reader
- reader = cmd.ExecuteReader();
- //使用集合 ,將所有讀取到的資料放入裡面
- accounts = new List<Account>();
- //讀取
- while (reader.Read())
- {
- accounts.Add(new Account(reader["account"].ToString(), reader["password"].ToString()));
- }
- }
- catch (Exception)
- {
- //發生錯誤會跑來這
- Response.Write("<Script language='Javascript'>");
- Response.Write("alert('NOT FOUND')");
- Response.Write("</" + "Script>");
- }
- finally
- {
- //執行完後關閉資料庫
- con.Close();
- }
- }
- //登入按鈕
- protected void Button1_Click1(object sender, EventArgs e)
- {
- //使用者輸入的 帳號 密碼
- string account = TextBox1.Text, password = TextBox2.Text;
- //宣告布林 判斷是否登入成功
- Boolean isSuccess = false;
- //透過迴圈走訪accounts集合
- foreach (Account item in accounts)
- {
- //假如有其中一筆帳戶核對成功
- if (account.Equals(item.account)&&password.Equals(item.password))
- {
- isSuccess = true;
- Response.Write("<Script language='Javascript'>alert('登入成功')</" + "Script>");
- // 之後轉頁面操作
- break;//跳出迴圈
- }
- }
- if (!isSuccess)//假如沒核對成功的帳戶
- {
- Response.Write("<Script language='Javascript'>alert('登入失敗')</" + "Script>");
- }
- }
- }
- //宣告CLASS 類別
- class Account//帳戶
- {
- public string account , password;
- public Account(string acc, string pass)
- {
- account = acc;
- password = pass;
- }
- }
2017年12月13日 星期三
ASP 登入方法
訂閱:
張貼留言 (Atom)

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