這篇文章主要為大家介紹了ADO.NET制做一個(gè)登錄案例的詳細(xì)過程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
總體思路.根據(jù)用戶輸入的用戶名和密碼,來判斷,和數(shù)據(jù)庫(kù)里面存的是不是一樣,如果一樣就表明登錄成功,否則就登錄失敗。
方案一:
1.select* from 表名 where username="用戶輸入的用戶名"
2.如果存在 reader.Read(),即用戶名存在,接著就判斷用戶輸入的密碼,和取到的密碼(reader.GetString(reader.GetOridinal("密碼字段")))是不是一樣,如果一樣就登錄成功,否則就登錄失敗。
方案二:
select * from 表名 where username="用戶輸入的用戶名" and password="用戶輸入的密碼",如果查得到數(shù)據(jù),就登錄成功。否則登錄失敗。
下面,我們來使用方案一,來做一個(gè)登錄的案例吧。
這里,為了方便,還是用控制臺(tái)應(yīng)用程序吧。
前奏:
我這次要把連接字符串寫在配置文件中,
1.首先我們要添加命名空間的引用:System.Configuration;
2.然后在我們的配置文件AppConfig中,的<Configuration>節(jié)點(diǎn)下面添加連接字符串的相關(guān)節(jié)點(diǎn)信息。
<configuration>
<span style="color: #800000"><connectionStrings>
<add name="ConStr" connectionString="server=.;database=DB_USERS;uid=sa;pwd=Pasword_1"/>
</connectionStrings>
</span> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
標(biāo)紅顏色的地方,就是我們添加的連接字符串節(jié)點(diǎn)信息;
3.然后我習(xí)慣,創(chuàng)建一個(gè)DBHelper類,在里面聲明一個(gè)方法來獲取,連接字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;//在項(xiàng)目中添加這個(gè)的引用,并在這個(gè)類里面添加這個(gè)命名空間
namespace ADO.NET登錄案例1
{
public class DBHelper
{
public static string GetConnectionStrings()
{
//使用ConfigurationManager類,來獲取連接字符串的信息。
return ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
}
}
}
4.這次我依然使用存儲(chǔ)過程,創(chuàng)建一個(gè)根據(jù)用戶名查詢的存儲(chǔ)過程:
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
存儲(chǔ)過程
前期的準(zhǔn)備工作,做好之后,現(xiàn)在我們來開始寫程序,編碼實(shí)現(xiàn):
思路:方案一,說了,首先我們當(dāng)然是讓用戶輸入,用戶名和密碼,然后根據(jù)用戶輸入的用戶名來查詢數(shù)據(jù)庫(kù)對(duì)應(yīng)的表中,有沒有相關(guān)數(shù)據(jù),如果沒有的話,就提示用戶名不存在,有的話,就繼續(xù)判斷用戶輸入的密碼是否正確(拿用戶輸入的密碼和數(shù)據(jù)庫(kù)對(duì)應(yīng)的密碼,進(jìn)行判斷),如果正確,就提示登錄成功,否則就提示密碼錯(cuò)誤。
*這里我使用參數(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("請(qǐng)輸入用戶名:");
//使用Console.ReadLine()接收用戶輸入的信息
string userName = Console.ReadLine();
//提示用戶輸入密碼
Console.WriteLine("請(qǐng)輸入密碼:");
string password = Console.ReadLine();
//現(xiàn)在就是開始使用ADO.NET技術(shù),來查詢數(shù)據(jù)庫(kù)了
//連接方式訪問
//1.創(chuàng)建連接對(duì)象(連接字符串)
SqlConnection scon = new SqlConnection(DBHelper.GetConnectionStrings());
//2.創(chuàng)建命令對(duì)象(并為命令對(duì)象設(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("密碼錯(cuò)誤");
}
}
else
{
Console.WriteLine("用戶名不存在");
}
//讀取器用完一定要關(guān)閉
reader.Dispose();
Console.ReadKey();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助