大家请看下面代码中的这是一个登陆界面问题是验证输入的用户名和密码是否和数据库里的一样可是查不到[color=#FF0000
两条SQL语句数据库里有东西可是查不到 登录提示用户名和密码不存在
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace School
{
public partial class Denglu : Form
{
public Denglu()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
bool isValidUser = false;//标识是否为合法用户
string message = "";//如果登录失败则显示消息提示
//如果登录成功则显示相应的窗体
if (ValidateInput())
{
isValidUser = ValidateUser(Loginid.Text, LoginPwd.Text, cbo.Text, ref message);
if (isValidUser)
{ Userhelp.txttname = Loginid.Text;//将输入的用户名保存到静态变量中
Userhelp.cbo = cbo.Text;//将输入的登录类型保存到静态变量中 ShowUserForm();//显示相应的窗体
this.Visible = false;//将当前窗体隐藏
}
else
{
MessageBox.Show(message, "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
//用户验证成功返回true 失败返回flase public void ShowUserForm()
{
switch (cbo.Text)
{
case "管理员":
AdminForm adminform = new AdminForm();
adminform.Show();
break;
default:
MessageBox.Show("抱歉,您请求的功能尚未完成");
break;
}
}
//验证用户是否进行了输入选择
private bool ValidateInput()
{
if (Loginid.Text.Trim() == "")
{
MessageBox.Show("请输入用户名", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Loginid.Focus();
return false;
}
else if (LoginPwd.Text.Trim() == "")
{
MessageBox.Show("请输入您的密码", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
LoginPwd.Focus();
return false;
}
else if (cbo.Text.Trim() == "")
{
MessageBox.Show("请选择登录类型", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
cbo.Focus();
return false;
}
else
{
return true;
}
}
public bool ValidateUser(string txttname, string txtpassword, string cbo, ref string message)
{
int count ;//数据库查询的结果
bool isValidUser = false;//返回值。是否找到该用户
if(cbo =="管理员")//判断管理员用户
{ //查询用的SQL语句
string sql = string.Format("select count (*) from Admin where Loginid='{0}'and LoginPwd='{1}'", Loginid, LoginPwd); try
{
//创建command命令
SqlCommand command =new SqlCommand (sql,DBhelp .connection );
DBhelp.connection .Open ();//打开连接
count =(int)command.ExecuteScalar();//执行查询语句
//如果找到一个验证通过否则是非法用户
if(count==1)
{
isValidUser = true;
}
else
{
message = "用户名或密码不存在";
isValidUser = false;
}
}
catch (Exception ex)
{
message=ex.Message;
Console.WriteLine(ex.Message);//出现异常打印异常消息
}
finally
{
DBhelp .connection .Close ();//关闭数据库连接
}
}
else if(cbo=="教员")
{
string sql = string.Format("select count (*) from Teacher where Loginid='{0}'and LoginPwd='{1}'",Loginid, LoginPwd);
try
{
//创建command命令
SqlCommand command =new SqlCommand (sql,DBhelp .connection );
DBhelp.connection .Open ();//打开连接 count =(int)command.ExecuteScalar();//执行查询语句
//如果找到一个验证通过否则是非法用户
if(count ==1)
{
isValidUser =true;
}
else
{
message ="用户名或密码不存在";
isValidUser =false;
}
}
catch (Exception ex)
{
message=ex.Message;
Console.WriteLine(ex.Message);//出现异常打印异常消息
}
finally
{
DBhelp .connection .Close ();//关闭数据库连接
}
}
return isValidUser;
} private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
两条SQL语句数据库里有东西可是查不到 登录提示用户名和密码不存在
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace School
{
public partial class Denglu : Form
{
public Denglu()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
bool isValidUser = false;//标识是否为合法用户
string message = "";//如果登录失败则显示消息提示
//如果登录成功则显示相应的窗体
if (ValidateInput())
{
isValidUser = ValidateUser(Loginid.Text, LoginPwd.Text, cbo.Text, ref message);
if (isValidUser)
{ Userhelp.txttname = Loginid.Text;//将输入的用户名保存到静态变量中
Userhelp.cbo = cbo.Text;//将输入的登录类型保存到静态变量中 ShowUserForm();//显示相应的窗体
this.Visible = false;//将当前窗体隐藏
}
else
{
MessageBox.Show(message, "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
//用户验证成功返回true 失败返回flase public void ShowUserForm()
{
switch (cbo.Text)
{
case "管理员":
AdminForm adminform = new AdminForm();
adminform.Show();
break;
default:
MessageBox.Show("抱歉,您请求的功能尚未完成");
break;
}
}
//验证用户是否进行了输入选择
private bool ValidateInput()
{
if (Loginid.Text.Trim() == "")
{
MessageBox.Show("请输入用户名", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Loginid.Focus();
return false;
}
else if (LoginPwd.Text.Trim() == "")
{
MessageBox.Show("请输入您的密码", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
LoginPwd.Focus();
return false;
}
else if (cbo.Text.Trim() == "")
{
MessageBox.Show("请选择登录类型", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
cbo.Focus();
return false;
}
else
{
return true;
}
}
public bool ValidateUser(string txttname, string txtpassword, string cbo, ref string message)
{
int count ;//数据库查询的结果
bool isValidUser = false;//返回值。是否找到该用户
if(cbo =="管理员")//判断管理员用户
{ //查询用的SQL语句
string sql = string.Format("select count (*) from Admin where Loginid='{0}'and LoginPwd='{1}'", Loginid, LoginPwd); try
{
//创建command命令
SqlCommand command =new SqlCommand (sql,DBhelp .connection );
DBhelp.connection .Open ();//打开连接
count =(int)command.ExecuteScalar();//执行查询语句
//如果找到一个验证通过否则是非法用户
if(count==1)
{
isValidUser = true;
}
else
{
message = "用户名或密码不存在";
isValidUser = false;
}
}
catch (Exception ex)
{
message=ex.Message;
Console.WriteLine(ex.Message);//出现异常打印异常消息
}
finally
{
DBhelp .connection .Close ();//关闭数据库连接
}
}
else if(cbo=="教员")
{
string sql = string.Format("select count (*) from Teacher where Loginid='{0}'and LoginPwd='{1}'",Loginid, LoginPwd);
try
{
//创建command命令
SqlCommand command =new SqlCommand (sql,DBhelp .connection );
DBhelp.connection .Open ();//打开连接 count =(int)command.ExecuteScalar();//执行查询语句
//如果找到一个验证通过否则是非法用户
if(count ==1)
{
isValidUser =true;
}
else
{
message ="用户名或密码不存在";
isValidUser =false;
}
}
catch (Exception ex)
{
message=ex.Message;
Console.WriteLine(ex.Message);//出现异常打印异常消息
}
finally
{
DBhelp .connection .Close ();//关闭数据库连接
}
}
return isValidUser;
} private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
改成如下:
string sql = string.Format("select count (*) from Teacher where Loginid='{0}'and LoginPwd='{1}'",txttname, txtpassword);