總體思路.根據(jù)用戶輸入的用戶名和密碼,來判斷,和數(shù)據(jù)庫里面存的是不是一樣,如果一樣就表明登錄成功,否則就登錄失敗。
方案一:
1.select* from 表名 where username="用戶輸入的用戶名"
2.如果存在 reader.Read(),即用戶名存在,接著就判斷用戶輸入的密碼,和取到的密碼(reader.GetString(reader.GetOridinal("密碼字段")))是不是一樣,如果一樣就登錄成功,否則就登錄失敗。
方案二:
select * from 表名 where username="用戶輸入的用戶名" and password="用戶輸入的密碼",如果查得到數(shù)據(jù),就登錄成功。否則登錄失敗。
下面,我們來使用方案一,來做一個登錄的案例吧。
這里,為了方便,還是用控制臺應(yīng)用程序吧。
前奏:
我這次要把連接字符串寫在配置文件中,
1.首先我們要添加命名空間的引用:System.Configuration;
2.然后在我們的配置文件AppConfig中,的Configuration>節(jié)點(diǎn)下面添加連接字符串的相關(guān)節(jié)點(diǎn)信息。
configuration>
connectionStrings>
add name="ConStr" connectionString="server=.;database=DB_USERS;uid=sa;pwd=Pasword_1"/>
/connectionStrings>
startup>
supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
/startup>
/configuration>
標(biāo)紅顏色的地方,就是我們添加的連接字符串節(jié)點(diǎn)信息;
3.然后我習(xí)慣,創(chuàng)建一個DBHelper類,在里面聲明一個方法來獲取,連接字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;//在項目中添加這個的引用,并在這個類里面添加這個命名空間
namespace ADO.NET登錄案例1
{
public class DBHelper
{
public static string GetConnectionStrings()
{
//使用ConfigurationManager類,來獲取連接字符串的信息。
return ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
}
}
}
4.這次我依然使用存儲過程,創(chuàng)建一個根據(jù)用戶名查詢的存儲過程:
IF OBJECT_ID('Ins_User','P') IS NOT NULL
DROP PROCEDURE Ins_User
GO
CREATE PROCEDURE Ins_User
@name NVARCHAR(20)
AS
SELECT * FROM dbo.T_USERS WHERE T_NAME=@name
GO
存儲過程
前期的準(zhǔn)備工作,做好之后,現(xiàn)在我們來開始寫程序,編碼實(shí)現(xiàn):
思路:方案一,說了,首先我們當(dāng)然是讓用戶輸入,用戶名和密碼,然后根據(jù)用戶輸入的用戶名來查詢數(shù)據(jù)庫對應(yīng)的表中,有沒有相關(guān)數(shù)據(jù),如果沒有的話,就提示用戶名不存在,有的話,就繼續(xù)判斷用戶輸入的密碼是否正確(拿用戶輸入的密碼和數(shù)據(jù)庫對應(yīng)的密碼,進(jìn)行判斷),如果正確,就提示登錄成功,否則就提示密碼錯誤。
*這里我使用參數(shù)化查詢,來寫登錄的案例,目的是為了防止SQL注入攻擊。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace ADO.NET登錄案例1
{
class Program
{
static void Main(string[] args)
{
//提示用戶輸入用戶名
Console.WriteLine("請輸入用戶名:");
//使用Console.ReadLine()接收用戶輸入的信息
string userName = Console.ReadLine();
//提示用戶輸入密碼
Console.WriteLine("請輸入密碼:");
string password = Console.ReadLine();
//現(xiàn)在就是開始使用ADO.NET技術(shù),來查詢數(shù)據(jù)庫了
//連接方式訪問
//1.創(chuàng)建連接對象(連接字符串)
SqlConnection scon = new SqlConnection(DBHelper.GetConnectionStrings());
//2.創(chuàng)建命令對象(并為命令對象設(shè)置屬性值)
SqlCommand scmd = new SqlCommand();
scmd.CommandText = "Ins_User";
scmd.CommandType = CommandType.StoredProcedure;
scmd.Connection = scon;
//3打開連接
scon.Open();
//設(shè)置參數(shù)
scmd.Parameters.Add(new SqlParameter("@name",userName.Trim()));
//4.執(zhí)行命令
SqlDataReader reader = scmd.ExecuteReader(CommandBehavior.CloseConnection);
//5處理數(shù)據(jù)
if (reader.Read())
{
if (password.Trim().ToString() == reader["T_PWD"].ToString())
{
Console.WriteLine("登錄成功");
}
else
{
Console.WriteLine("密碼錯誤");
}
}
else
{
Console.WriteLine("用戶名不存在");
}
//讀取器用完一定要關(guān)閉
reader.Dispose();
Console.ReadKey();
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 一款經(jīng)典的ajax登錄頁面 后臺asp.net
- asp.net jQuery Ajax用戶登錄功能的實(shí)現(xiàn)
- asp.net BasePage類+Session通用用戶登錄權(quán)限控制
- 基于.Net的單點(diǎn)登錄(SSO)實(shí)現(xiàn)解決方案
- 一個簡單的asp.net 單點(diǎn)登錄實(shí)現(xiàn)
- ASP.NET 應(yīng)用程序級 驗證用戶是否登錄 一般處理程序
- asp.net DiscuzNT登錄,退出的代碼
- ASP.NET登錄注冊頁面實(shí)現(xiàn)
- asp.net利用cookie保存用戶密碼實(shí)現(xiàn)自動登錄的方法
- ASP.NET jQuery 實(shí)例11 通過使用jQuery validation插件簡單實(shí)現(xiàn)用戶登錄頁面驗證功能