先根据用户输入的用户名查找数据中是否有这条数据,如果没有,提示该用户不存在,如果有就直接将这条数据取出,将用户输入的密码跟取出的数据进行比较,若相同就登陆该软件,不相同就提示密码错误。。。。
建个DBHelper类用于数据库的连接:
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemData;
using SystemDataSqlClient;
using SystemWindowsForms;
namespace Load
{
class DBHelper
{
private static SqlCommand cmd = null;
private static SqlDataReader dr = null;
public int RowCount { get; private set; }
SqlConnection sqlCnn = new SqlConnection();
SqlCommand sqlCmd = new SqlCommand();
//数据库连接字符串
private static string connectionString = "Server = 127001; Database = dianzi; Integrated Security =SSPI";
//数据库连接Connection对象
public static SqlConnection connection = new SqlConnection(connectionString);
public DBHelper()
{ }
#region 返回结果集
public static SqlDataReader GetResult(string sql)
{
try
{
cmd = new SqlCommand();
cmdCommandText = sql;
cmdConnection = connection;
cmdConnectionOpen();
dr = cmdExecuteReader();
return dr;
}
catch (Exception ex)
{
MessageBoxShow(exMessage);
return null;
}
finally
{
//drClose();
//cmdConnectionClose();
}
}
#endregion
#region 对Select语句,返回int型结果集
public static int GetSqlResult(string sql)
{
try
{
cmd = new SqlCommand();
cmdCommandText = sql;
cmdConnection = connection;
cmdConnectionOpen();
int a = (int)cmdExecuteScalar();
return a;
}
catch (Exception ex)
{
MessageBoxShow(exMessage);
return -1;
}
finally
{
cmdConnectionClose();
}
}
#endregion
#region 对Update,Insert和Delete语句,返回该命令所影响的行数
public static int GetDsqlResult(string sql)
{
try
{
cmd = new SqlCommand();
cmdCommandText = sql;
cmdConnection = connection;
cmdConnectionOpen();
cmdExecuteNonQuery();
return 1;
}
catch (Exception ex)
{
MessageBoxShow(exMessage);
return -1;
}
finally
{
cmdConnectionClose();
}
}
#endregion
}
}
Load窗体中的函数:
1、 #region 验证用户的输入,成功返回true,失败返回false
private bool IsValidataInput()
{
if (txtbox1TextTrim() == "")
{
MessageBoxShow("请输入用户名!", "登陆提示", MessageBoxButtonsOK, MessageBoxIconInformation);
txtbox1Focus();
return false;
}
else if (txtbox2Text == "")
{
MessageBoxShow("请输入密码!", "登陆提示", MessageBoxButtonsOK, MessageBoxIconInformation);
txtbox2Focus();
return false;
}
return true;
}
#endregion
2、 #region 验证用户是否合法
//传递用户账号、密码、登陆类型,合法返回true,不合法返回false
//message参数用来记录验证失败的原因
private bool IsValidataUser(string UserName, string UserrPwd, ref string message)
{
string sql = StringFormat("select count() from User where UserName = '{0}' and UserPwd = '{1}'", UserName, UserPwd);
int a = DBHelperGetSqlResult(sql);
if (a < 1)
{
message = "该用户名不存在或密码错误;并检查用户类型是否选择正确!";
return false;
}
else
{
return true;
}
}
#endregion
3、按钮事件
private void button_Click(object sender, EventArgs e)
{
//标识是否为合法用户
bool isValidUser = false;
string message = "登陆失败!";
if (IsValidataInput())
{
//验证用户是否为合法用户
isValidUser = IsValidataUser(txtbox1TextTrim(),txtbox2Text, ref message);
if (isValidUser)
{
Manager ma = new Manager(); // Manager为你登陆后要显示的窗体
maShow();
thisHide();
}
else
{
MessageBoxShow(message, "登陆提示", MessageBoxButtonsOK, MessageBoxIconAsterisk);
}
}
主窗体
Load
事件里丢入以下代码
(适当改动
连接字符串
和
Query
语句
)
string
strcon
=
"Data
Source
=
127001;Initial
Catalog
=
YouDbName
;Password
=
your
password;Integrated
Security
=
true";
SqlConnection
con
=
null;
SqlCommand
cmd
=
null;
SqlDataReader
sdr
=
null;
bool
isLogin
=
false;
try
{
con
=
new
SqlCommand
(strCon);
cmd
=
conCreateCommand();
cmdCommandText
=stringFormat(
"select
from
yourTableName
where
u_Name=
'{0}'
and
u_password
=
'{1}'",textBoxUserNameText,textBoxPasswordText);
conopen();
sdr
=
cmdExecuteReader();
while(sdrRead())
{
isLogin=true;
}
if(isLogin)
{
MessageBoxShow("login
sucess!!!");
}
else
{
MessageBoxShow("login
fail!!!");
}
}
catch(Exception
ex)
{
MessageBoxShow(exMessage);
}
finally
{
sdrClose();
cmdClose();
conClose();
}
$db=mysql_connect(连接你的数据库);
mysql_select_db('login', $db);
$u=$_POST['u'];
$p=$_POST['p'];
if($u && $p){
$query=mysql_query('select from exam where name=\''$u'\'',$db);
if($re=mysql_fetch_array($query,MYSQL_ASSOC)&&$re['password']==$p){
exit('<script>alert(\'登陆成功\');historyback();</script>');
}else{
exit('<script>alert(\'用户名不存在,或密码错误\');historyback();</script>');
}
}
以上就是关于数据库问题:登录时,用户名和密码与数据库中的记录进行校验,如何实现全部的内容,包括:数据库问题:登录时,用户名和密码与数据库中的记录进行校验,如何实现、求一个用户登录验证c#代码,包括连接数据库,验证等功能,要求最简单、C#写的一个登录窗口,需要连接数据库进行登录验证,应该怎么做等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)