上次的谢谢各位! 是代码错了。
private void button1_Click(object sender, EventArgs e)
{ string name = textBox1.Text;
string pwd = textBox2.Text;
string name1= "select count(*) from admine where admin="+textBox1 .Text ;
string pwd1 = "select count(*) from admine where password="+textBox2 .Text ;
if (name == "" && pwd == "")
{
MessageBox.Show("用户名和密码不能为空!");
}
else if(name .Equals (name1 ) && pwd.Equals (pwd1))
{
Form AdminForm = new Admin();
AdminForm.Show();
}
else
{
MessageBox.Show("用户名或密码错误!请重新输入!");
textBox1.Text = "";
textBox2.Text = "";
}
}
大家帮忙看看这段代码,只要输入用户名和密码直接执行
else
{
MessageBox.Show("用户名或密码错误!请重新输入!");
textBox1.Text = "";
textBox2.Text = "";
}
这段代码,帮忙看看,先谢谢大家了!
private void button1_Click(object sender, EventArgs e)
{ string name = textBox1.Text;
string pwd = textBox2.Text;
string name1= "select count(*) from admine where admin="+textBox1 .Text ;
string pwd1 = "select count(*) from admine where password="+textBox2 .Text ;
if (name == "" && pwd == "")
{
MessageBox.Show("用户名和密码不能为空!");
}
else if(name .Equals (name1 ) && pwd.Equals (pwd1))
{
Form AdminForm = new Admin();
AdminForm.Show();
}
else
{
MessageBox.Show("用户名或密码错误!请重新输入!");
textBox1.Text = "";
textBox2.Text = "";
}
}
大家帮忙看看这段代码,只要输入用户名和密码直接执行
else
{
MessageBox.Show("用户名或密码错误!请重新输入!");
textBox1.Text = "";
textBox2.Text = "";
}
这段代码,帮忙看看,先谢谢大家了!
如果需要實現用戶一離開TextBox就觸發事件,就在TextChanged或者LostFocus事件里面做
string name1= "select count(*) from admine where admin="+textBox1 .Text ;
string pwd1 = "select count(*) from admine where password="+textBox2 .Text ; else if(name .Equals (name1 ) && pwd.Equals (pwd1))
{
Form AdminForm = new Admin();
AdminForm.Show();
} 用户名和一个sql语句进行比较??
即便是你做了查询,
用户名和用户名的数量比较,密码和密码的数量比较。
有可比性吗?
这样能判断用户能否登录吗?
每次都是判断失败当然直接执行else了。
你用户名和密码在数据库中是字符串类型的吗?
string name1= "select count(*) from admine where admin=‘"+textBox1 .Text +“’”;
string pwd1 = "select count(*) from admine where password='"+textBox2 .Text+"'" ;
只要输入用户名和密码直接执行
else
{
MessageBox.Show("用户名或密码错误!请重新输入!");
textBox1.Text = "";
textBox2.Text = "";
}
string name1= "select count(*) from admine where admin="+textBox1 .Text ; name .Equals (name1 ) 都没执行查询 name1值还是 "select count(*) from admine where admin="+textBox1 .Text
string name1= "select count(*) from admine where admin=‘"+textBox1 .Text +“’”;
string pwd1 = "select count(*) from admine where password='"+textBox2 .Text+"'" ;
这两条sql字符串和textbox中的内容比较
结果肯定是
else
{
MessageBox.Show("用户名或密码错误!请重新输入!");
textBox1.Text = "";
textBox2.Text = "";
}
还不如第一次登陆后,记住Cookie呢。下次UName一出现自个儿跟上密码!
{
MessageBox.Show("用户名和密码不能为空!");
}
首先第一个判断就有问题,应该是
if (name == "" || pwd == "")
吧...随便一个空都不能过哇
{
string strSql="select * from Tb where admin='"+textBox1.Text+"'";
SqlCommand cmd=new SqlCommand(strSql,conn) ;
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
if(dr["password"].ToString().Equals(""))
{}
}
conn.Close();
}
//连接数据库略 private void button1_Click(object sender, EventArgs e)
{//登录
loginTime++; string userName = this.textBox1.Text.Trim();
string passWord = this.textBox2.Text.Trim();
StringBuilder sql = new StringBuilder();
sql.Append("select count(*) from [User] where Username='"+userName+"' and Password='"+passWord+"'");
bool checkUser=DBUtility.DbHelperOleDb.CheckUser(sql.ToString());
if (checkUser == true)
{//检验合法用户成功,进入主界面
Close();
//MessageBox.Show(this, "存在该用户", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (loginTime == 3)
{
MessageBox.Show("登陆失败,超过三次,退出");
Application.Exit();
}
else
{
MessageBox.Show("登陆失败");
this.textBox1.Clear();
this.textBox2.Clear();
}
}
}
public static bool CheckUser(String sql)
{//查询用户是否存在
Object obj = GetScalar(sql.ToString());
int cmdResult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdResult = 0;
}
else
{
cmdResult = int.Parse(obj.ToString());
}
if (cmdResult == 0)
{
return false;
}
else
{
return true;
}
}
public static Object GetScalar(String sql)
{//执行查询语句,返回查询结果(object)
if (connection != null)
{
if (connection.State == ConnectionState.Open)
{
using (OleDbCommand cmd = new OleDbCommand(sql, connection))
{
try
{ object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.OleDb.OleDbException e)
{
connection.Close();
throw new Exception(e.Message);
}
}
}
}
throw new Exception("未连接数据库");
}
string name = textBox1.Text;
string pwd = textBox2.Text;
string name1= "select count(*) from admine where admin="+textBox1 .Text ;
string pwd1 = "select count(*) from admine where password="+textBox2 .Text ;string name1= "select count(*) from admine where admin="+textBox1 .Text ;
string pwd1 = "select count(*) from admine where password="+textBox2 .Text ;
上述两条SQL语句赋值的结果是为空还是不为空
这样赋值,name和name1肯定不相等的嘛
1.sql代码没有使用数据控件去查询(使用SqlCommand.ExecuteScalar)
2.两个sql换成 where 中用 and 连接的两个条件的一个sql
3.使用参数或把用户名和密码 ,替换一个单引号'为两个单引号''
http://www.mybuffet.cn
{ string name = textBox1.Text;
string pwd = textBox2.Text;
int name1= "select count(*) from admine where admin= '"+name +"'" and password = '" +pwd ;
if (name1>0 && name1!=null)
{
Form AdminForm = new Admin();
AdminForm.Show();
}
else if(name = ""||
pwd = "")
{
MessageBox.Show("用户名和密码不能为空!");
}
else
{
MessageBox.Show("用户名或密码错误!请重新输入!");
textBox1.Text = "";
textBox2.Text = "";
}
}
这样试试
{ string name = textBox1.Text;
string pwd = textBox2.Text;
int name1= "select count(*) from admine where admin= '"+name +"'" and password = '" +pwd ;
if (name1>0 && name1!=null)
{
Form AdminForm = new Admin();
AdminForm.Show();
}
else if(name == ""||pwd == "")
{
MessageBox.Show("用户名和密码不能为空!");
}
else
{
MessageBox.Show("用户名或密码错误!请重新输入!");
textBox1.Text = "";
textBox2.Text = "";
}
}
试试这个怎么样?
string name1= "select count(*) from admine where admin="+textBox1 .Text ;
string pwd1 = "select count(*) from admine where password="+textBox2 .Text ; 你这比较又是什么就算你上面执行了 你返回的结果是字符串行的数字(count(*));
你用返回的行数和你输入的用户名比?
else if(name .Equals (name1 ) && pwd.Equals (pwd1))
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnLogin_Click(object sender, EventArgs e)
{
string str = "server=liu;database=myTest;uid=sa;pwd=123456";
SqlConnection sqlcon = new SqlConnection(str); string uname=this.txtName.Text.Trim();//用户名
string pwd=this.txtPwd.Text.Trim();//密码
string sql=string.Format("select count(*) from login where name='{0}' and pwd='{1}'",
uname,pwd); SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = sql;
sqlcmd.CommandType = CommandType.Text;
sqlcmd.Connection = sqlcon; try
{
sqlcon.Open();
object obj= sqlcmd.ExecuteScalar();
if (Convert.ToInt32(obj) > 0)
{
MessageBox.Show("恭喜,登录成功!");
}
else
{
MessageBox.Show("失败");
}
}
catch (Exception ex)
{
}
finally
{
sqlcon.Close();
}
}
}
}
自己改下数据库连接就行了