日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

[C#] 连接数据库并验证用户名和密码

發(fā)布時(shí)間:2025/3/20 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [C#] 连接数据库并验证用户名和密码 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

連接數(shù)據(jù)庫, 請(qǐng)根據(jù)用用戶的輸入和數(shù)據(jù)庫里的信息進(jìn)行比較, 判斷用戶名和密碼是否正確

數(shù)據(jù)庫設(shè)計(jì):

數(shù)據(jù)庫名: MyDataBase1.mdf

表名: T_Users

表數(shù)據(jù):

?

程序代碼

[csharp] view plaincopyprint?
  • using?System;??
  • using?System.Collections.Generic;??
  • using?System.Text;??
  • using?System.Data.SqlClient;??
  • ??
  • namespace?登錄??
  • {??
  • ????class?Program??
  • ????{??
  • ????????static?void?Main(string[]?args)??
  • ????????{??
  • ????????????//下面這段文字的作用已經(jīng)在其它博文中解釋! ??
  • ????????????string?dataDir?=?AppDomain.CurrentDomain.BaseDirectory;??
  • ????????????if?(dataDir.EndsWith(@"\bin\Debug\")??
  • ????????????????||?dataDir.EndsWith(@"\bin\Release\"))??
  • ????????????{??
  • ????????????????dataDir?=?System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;??
  • ????????????????AppDomain.CurrentDomain.SetData("DataDirectory",?dataDir);??
  • ????????????}??
  • ??
  • ????????????//定義用戶名和密碼變量 ??
  • ????????????string?username,?password;??
  • ??
  • ????????????//提示用戶輸入用戶名和密碼 ??
  • ????????????Console.WriteLine("請(qǐng)輸入用戶名:");??
  • ????????????username?=?Console.ReadLine();??
  • ????????????Console.WriteLine("請(qǐng)輸入密碼:");??
  • ????????????password?=?Console.ReadLine();??
  • ??
  • ????????????//使用創(chuàng)建數(shù)據(jù)庫連接 ??
  • ????????????using(SqlConnection?conn?=?new?SqlConnection(@"Data?Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDataBase1.mdf;Integrated?Security=True;User?Instance=True"))??
  • ????????????{??
  • ????????????????conn.Open();//打開數(shù)據(jù)庫 ??
  • ??????????????????
  • ????????????????//創(chuàng)建數(shù)據(jù)庫查詢命令 ??
  • ????????????????using(SqlCommand?cmd?=?conn.CreateCommand())??
  • ????????????????{??
  • ????????????????????//查詢命令為:查詢UserName等于輸入的用戶名 ??
  • ????????????????????cmd.CommandText?=?"select?*?from?T_Users?where?UserName='"?+?username?+?"'";??
  • ??
  • ????????????????????//將查詢到的數(shù)據(jù)保存在reader這個(gè)變量里 ??
  • ????????????????????using(SqlDataReader?reader?=?cmd.ExecuteReader())??
  • ????????????????????{??
  • ????????????????????????//如果reader.Read()的結(jié)果不為空,?則說明輸入的用戶名存在 ??
  • ????????????????????????if(reader.Read())??
  • ????????????????????????{??
  • ????????????????????????????/*從數(shù)據(jù)庫里查詢出和用戶相對(duì)應(yīng)的PassWorld的值?
  • ?????????????????????????????*reader.GetOrdinal("PassWord")的作用是得到PassWord的為這行數(shù)據(jù)中的第幾列,返回回值是int?
  • ?????????????????????????????*reader.GetString()的作用是得到第幾列的值,返回類型為String.?
  • ?????????????????????????????*/??
  • ????????????????????????????string?dbpassword?=?reader.GetString(reader.GetOrdinal("PassWord"));??
  • ??????????????????????????????
  • ????????????????????????????//比較用戶輸入的密碼與從數(shù)據(jù)庫中查詢到的密碼是否一至 ??
  • ????????????????????????????if(password==dbpassword)??
  • ????????????????????????????{??
  • ????????????????????????????????//如果相等,就登錄成功 ??
  • ????????????????????????????????Console.WriteLine("登錄成功!");??
  • ????????????????????????????}??
  • ????????????????????????????else??
  • ????????????????????????????{??
  • ????????????????????????????????//如果不相等,說明密碼不對(duì) ??
  • ????????????????????????????????Console.WriteLine("輸入的密碼有誤!");??
  • ????????????????????????????}??
  • ??
  • ????????????????????????}??
  • ????????????????????????else??
  • ????????????????????????{??
  • ????????????????????????????//說明輸入的用戶名不存在 ??
  • ????????????????????????????Console.WriteLine("輸入的用戶名不存在!");??
  • ????????????????????????}??
  • ????????????????????}??
  • ??
  • ????????????????}??
  • ??
  • ????????????}????????
  • ????????????Console.ReadKey();??
  • ????????}??
  • ????}??
  • }??
  • 轉(zhuǎn)載于:https://www.cnblogs.com/zyj-keyen/archive/2012/07/26/2610065.html

    總結(jié)

    以上是生活随笔為你收集整理的[C#] 连接数据库并验证用户名和密码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。