【ADO.NET】2、各種版本的 簡單登入驗證

來源:互聯網
上載者:User

標籤:主程式   ase   執行   .text   test   add   username   沒有   防止   

一、簡單登入驗證(防SQL注入)

GetString(序號) 返回某一列的值(當使用者不記得列名序號時,可使用GetOrdinal()擷取到序號)
GetInt32(序號)針對的是 int 欄位,返回int欄位的值
GetOrdinal("列名")根據列名得到序號

Console.WriteLine("請輸入使用者名稱:");string Uname=Console.ReadLine();Console.WriteLine("請輸入密碼:");string Pwd=Console.ReadLine();using (SqlConnection conn = new SqlConnection("Data Source=.;database=mytest;uid=sa;pwd=gao;")){  conn.Open();  using (SqlCommand cmd = conn.CreateCommand())  {    //User為關鍵字,用[]解決,表名T_開頭,欄位F開頭    cmd.CommandText = "select * from mydo where username=‘" + Uname + "‘";    using (SqlDataReader dr = cmd.ExecuteReader())    {      if (dr.Read())      {        //防止SQL注入,GetString(序號)返回某一列的值,GetOrdinal()根據列名得到序號        string dbpasswd = dr.GetString(dr.GetOrdinal("passwd"));        if (Pwd == dbpasswd) //判斷使用者輸入的與資料庫查詢到的是否一致        {          Console.WriteLine("登入成功!");        }        else        {           Console.WriteLine("密碼錯誤!");        }      }      else      {        Console.WriteLine("使用者名稱錯誤!");      }    }  }}

 

二、簡單登入驗證 (通過參數賦值)(防SQL注入)
Console.WriteLine("請輸入使用者名稱:");string username=Console.ReadLine();Console.WriteLine("請輸入密碼:");string passwd=Console.ReadLine();using (SqlConnection conn = new SqlConnection("Data Source=.;database=mytest;uid=sa;pwd=gao;")){  conn.Open();  using (SqlCommand cmd = conn.CreateCommand())  {
    cmd.CommandText = "select * from mydo where [email protected] and [email protected]";    cmd.Parmeters.Add(new SqlParmeter("Uname",username));    cmd.Parmeters.Add(new SqlParmeter("Pwd",passwd));    using (SqlDataReader dr = cmd.ExecuteReader())    {      if (dr.Read())      {        Console.WriteLine("登陸成功!");      }      else      {        Console.WriteLine("登陸失敗!");      }    }  }}

 

三、簡單登入驗證(含有錯誤次數的判斷)


首先在資料庫中加入 error 欄位,類型為 int 預設值為 0
注意:在同一個串連中,如果SqlDataReader沒有關閉,那麼是不能執行Update之類的語句的
所以下面對於 error 次數的修改,都是通過 函數 的方式來實現

private void IncErrorTimes()        //用於為error欄位自增值的 函數(方法)
{ using (SqlConnection conn = new SqlConnection("server=.;database=mytest;uid=sa;pwd=gao;")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "update mydo set error=error+1 where [email protected]"; cmd.Parameters.Add(new SqlParameter("@U", txtLogin.Text.Trim())); cmd.ExecuteNonQuery(); } } }private void ResetErrorTimes() //用於為error欄位複位(設定為0)的 函數
{ using (SqlConnection conn = new SqlConnection("server=.;database=mytest;uid=sa;pwd=gao;")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "update mydo set error=0 where [email protected]"; cmd.Parameters.Add(new SqlParameter("@U", txtLogin.Text.Trim())); cmd.ExecuteNonQuery(); } } }private void button1_Click(object sender, EventArgs e) //主程式{ using (SqlConnection conn = new SqlConnection("server=.;database=mytest;uid=sa;pwd=gao;")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from mydo where [email protected]"; cmd.Parameters.Add(new SqlParameter("U", txtLogin.Text.Trim())); using (SqlDataReader dr = cmd.ExecuteReader()) { if (dr.Read()) { int errorTimes = dr.GetInt32(dr.GetOrdinal("error")); if (errorTimes >= 3) { MessageBox.Show("錯誤次數過多,禁止登陸!"); return; } string dbname = dr.GetString(dr.GetOrdinal("username")); if (dbname == txtLogin.Text.Trim()) { string dbpasswd = dr.GetString(dr.GetOrdinal("passwd")); if (dbpasswd == txtPasswd.Text.Trim()) { ResetErrorTimes(); MessageBox.Show("登陸成功!"); picBox1.Visible = true; } else { IncErrorTimes(); MessageBox.Show("登陸失敗!"); return; } } } else { MessageBox.Show("使用者名稱錯誤!"); } } } }

 

【ADO.NET】2、各種版本的 簡單登入驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.