protected void ButtonLogin_Click(object sender, EventArgs e)
{
if (this.TextBoxName.Text != "")
{
if (this.TextBoxPsd.Text != "")
{
string sql;
sql = string.Format("select * from Users where user='{0}' and password='{1}'", this.TextBoxName.Text.Trim(), this.TextBoxPsd.Text.Trim());
try
{
MySqlConnection conn = new MySqlConnection("server=localhost;user id=root;password=nycg;database=dlxj");
conn.Open();
MySqlCommand myCommand = new MySqlCommand(sql, conn);
myCommand.Connection = conn;
int state=Convert.ToInt32(myCommand.ExecuteScalar());
if(state==0||state>1)
{
this.LabelMessage.Text="用户不存在,请检测用户名和密码是否正确!";
}
else
{
this.LabelMessage.Text="登录成功!";
Page.Response.Redirect("Default.aspx");
}
conn.Close();
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
else
{
this.LabelMessage.Text = "请输入密码";
}
}
else
{
this.LabelMessage.Text = "请输入用户名";
}
}
采用以上代码连接MYSQL数据库正常
但是运行后,我登录时为什么只有数据库中的第一行数据可以被通过呢?
输入第一行之外的用户名和密码都是提示"用户不存在,请检测用户名和密码是否正确!
请帮忙。
{
if (this.TextBoxName.Text != "")
{
if (this.TextBoxPsd.Text != "")
{
string sql;
sql = string.Format("select * from Users where user='{0}' and password='{1}'", this.TextBoxName.Text.Trim(), this.TextBoxPsd.Text.Trim());
try
{
MySqlConnection conn = new MySqlConnection("server=localhost;user id=root;password=nycg;database=dlxj");
conn.Open();
MySqlCommand myCommand = new MySqlCommand(sql, conn);
myCommand.Connection = conn;
int state=Convert.ToInt32(myCommand.ExecuteScalar());
if(state==0||state>1)
{
this.LabelMessage.Text="用户不存在,请检测用户名和密码是否正确!";
}
else
{
this.LabelMessage.Text="登录成功!";
Page.Response.Redirect("Default.aspx");
}
conn.Close();
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
else
{
this.LabelMessage.Text = "请输入密码";
}
}
else
{
this.LabelMessage.Text = "请输入用户名";
}
}
采用以上代码连接MYSQL数据库正常
但是运行后,我登录时为什么只有数据库中的第一行数据可以被通过呢?
输入第一行之外的用户名和密码都是提示"用户不存在,请检测用户名和密码是否正确!
请帮忙。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货