using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;namespace login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == string.Empty)
{
MessageBox.Show("请输入用户名");
return;
}
else if (textBox1.Text.Length < 2)
{
MessageBox.Show("用户名不得小于两位");
return;
}
if (textBox2.Text.Trim() == string.Empty)
{
MessageBox.Show("请输入密码");
return;
}
else if (textBox2.Text.Length < 6)
{
MessageBox.Show("密码不得小于六位");
return;
}
MySqlConnection mysqlconn = null;
String MyconnectString = "Server=localhost;UserId=root;Password=qwe12356;Database=test";
mysqlconn = new MySqlConnection(MyconnectString);
mysqlconn.Open();
//MySqlCommand cmd = new MySqlCommand();
//cmd.Connection = mysqlconn;
//cmd.CommandText = "select * from login where name='" + textBox1.Text + "'and password='" + textBox2.Text + "'";
String strSql = "select * from login where name='" + textBox1.Text + "'and password='" + textBox2.Text + "'";
MySqlCommand cmd = new MySqlCommand(strSql, mysqlconn);
int number = Convert.ToInt32(cmd.ExecuteNonQuery());
if (Convert.ToInt32(cmd.ExecuteNonQuery()) > 1)
{
MessageBox.Show("登录成功");
}
/*
MySqlDataReader mysqlread = cmd.ExecuteReader();
while (mysqlread.Read())
{
Boolean boo = String.Equals((String)mysqlread["name"], textBox1.Text);
Boolean boo1 = String.Equals((String)mysqlread["password"], textBox2.Text);
if (boo == true && boo1 == true)
{
MessageBox.Show("登陆成功");
}
else
{
MessageBox.Show("登录失败");
}
}
* */
} }
}
{
MessageBox.Show("登录成功");
}
你这里加个else判断就知道了
而你是要登录 要到数据里面查询数据里面是否有你要登陆的的数据
用 阅读器接收sqldatareader
在判断是否有
if(变量名.HasRows==true)
{
messagebox.show("登陆成功")
}
if (result == 1)
{
validate = true;
}
else
{
MessageBox.Show("用户名或者密码错误", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
//弹出错误提示
}
}
查询不是用这个方法的...String strSql = "select count(*) from login where name='" + textBox1.Text + "'and password='" + textBox2.Text + "'";
MySqlCommand cmd = new MySqlCommand(strSql, mysqlconn);
int number = Convert.ToInt32(cmd.ExcuteScalar());
为什么要这样写?
ExecuteNonQuery 是返回受影响的行数吧,本来就是int. 为何多此一举 Convert?
select *from table ? 这样 你用ExecuteNonQuery 方法 ?
其次, >1 的条件是 为2 ,或者2 以上.
存在很多问题 .虽然是新手,但也不能这样啊..这个是最基本的东西.
好好去学习下ADO.NET