AD0.NET一個簡單的使用者登陸控制檯程式
阿新 • • 發佈:2019-02-07
<1>一個簡單的使用者登陸
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace ConsoleApplication1 { class Program { //int errorPaw = 0; static void Main(string[] args) { string strConn = "Data Source=Fan-VAIO;InitialCatalog=sales;Integrated Security=True"; Program pr = new Program(); Console.WriteLine("請輸入使用者名稱:"); string userName = Console.ReadLine(); Console.WriteLine("請輸入密碼:"); string userPaw = Console.ReadLine(); using (SqlConnection conn = new SqlConnection(strConn)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from t_user where name='" + userName + "'"; SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { string adoPaw = reader.GetString(reader.GetOrdinal("password")); if (userPaw == adoPaw) { Console.WriteLine("登陸成功"); } else { Console.WriteLine("密碼錯誤,請輸入正確的密碼"); } // pr.errorPaw++; Console.WriteLine("請輸入密碼"); userPaw = Console.ReadLine(); if (userPaw == adoPaw) { Console.WriteLine("登陸成功"); } else { Console.WriteLine("密碼錯誤,請輸入正確的密碼"); } //pr.errorPaw++; //pr.ErrorPaw(); Console.WriteLine("請輸入密碼"); userPaw = Console.ReadLine(); if (userPaw == adoPaw) { Console.WriteLine("登陸成功"); } else { Console.WriteLine("密碼錯誤,因輸入次數過多,請稍後再試"); Console.ReadKey(); return; } } else { Console.WriteLine("使用者名稱不存在"); } } Console.ReadKey(); } } } }