[code=asp.net]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table><tr><td>用户名: </td><td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td><td>
<asp:Label ID="Label1" runat="server"></asp:Label></td>
</tr><tr><td>密 码:</td><td>
<asp:TextBox ID="TextBox2" runat="server" EnableViewState="False" TextMode="Password"></asp:TextBox></td><td>
<asp:Label ID="Label2" runat="server"></asp:Label></td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" Text="登陆" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="注册"
PostBackUrl="~/zhuche.aspx" />
<p align="center"><asp:Label ID="Label3" runat="server"></asp:Label></p>
</div>
</form>
</body>
</html>[/code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;using MySql.Data;
using MySql.Data.MySqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Equals(String.Empty))
{
Label1.Text = "请输入用户名!";
return;
}
else if (TextBox2.Text.Equals(String.Empty))
{
Label1.Text = "";
Label2.Text = "请输密码!";
return;
}
else
{
Label1.Text = "";
Label2.Text = "";
//Label3.Text = "登陆成功!";
//跟数据库作比较
///*
String connectionString = "server=localhost;user id=jhonny;password=jhonny_74513210;database=jhonny;pooling=false";
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();
String sql = "select * from user where userName='" + TextBox1.Text + "'" + " and password='" + TextBox2.Text + "';";
MySqlCommand command = new MySqlCommand(sql, connection);
if (command.ExecuteNonQuery() > 0)
{
Label3.Text = "登陆成功!";
}
connection.Close();
// */ }
}
}
Label3.Text = "登陆成功!";
为什么上面这句话放在数据库执行后就不能显示了呢:if (command.ExecuteNonQuery() > 0)
{
Label3.Text = "登陆成功!";
}
而我在数据库里直接查询都有返回受影响的条数,为什么到这里都不能执行了呢
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table><tr><td>用户名: </td><td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td><td>
<asp:Label ID="Label1" runat="server"></asp:Label></td>
</tr><tr><td>密 码:</td><td>
<asp:TextBox ID="TextBox2" runat="server" EnableViewState="False" TextMode="Password"></asp:TextBox></td><td>
<asp:Label ID="Label2" runat="server"></asp:Label></td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" Text="登陆" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="注册"
PostBackUrl="~/zhuche.aspx" />
<p align="center"><asp:Label ID="Label3" runat="server"></asp:Label></p>
</div>
</form>
</body>
</html>[/code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;using MySql.Data;
using MySql.Data.MySqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Equals(String.Empty))
{
Label1.Text = "请输入用户名!";
return;
}
else if (TextBox2.Text.Equals(String.Empty))
{
Label1.Text = "";
Label2.Text = "请输密码!";
return;
}
else
{
Label1.Text = "";
Label2.Text = "";
//Label3.Text = "登陆成功!";
//跟数据库作比较
///*
String connectionString = "server=localhost;user id=jhonny;password=jhonny_74513210;database=jhonny;pooling=false";
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();
String sql = "select * from user where userName='" + TextBox1.Text + "'" + " and password='" + TextBox2.Text + "';";
MySqlCommand command = new MySqlCommand(sql, connection);
if (command.ExecuteNonQuery() > 0)
{
Label3.Text = "登陆成功!";
}
connection.Close();
// */ }
}
}
Label3.Text = "登陆成功!";
为什么上面这句话放在数据库执行后就不能显示了呢:if (command.ExecuteNonQuery() > 0)
{
Label3.Text = "登陆成功!";
}
而我在数据库里直接查询都有返回受影响的条数,为什么到这里都不能执行了呢
把command.ExecuteNonQuery()赋给int型
然后再判断
返回结果集的可以用 ExecuteReader() or DataTable or DataSet
那如果是select 要用到哪个呀
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();
String sql = "select * from user where userName='" + TextBox1.Text + "'" + " and password='" + TextBox2.Text + "';";
MySqlDataAdapter adpt= new MySqlDataAdapter (sql, connection);
DataTable dt=new DataTable();
adpt.Fill(dt);
if (dt.Rows.Count>0)
{
Label3.Text = "登陆成功!";
}
最好能把你的操作数据库的一些操作封装一下
String connectionString = "server=localhost;user id=jhonny;password=jhonny_74513210;database=jhonny;pooling=false";
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();
String sql = "select * from user where userName='" + TextBox1.Text + "'" + " and password='" + TextBox2.Text + "';";
MySqlCommand command = new MySqlCommand(sql, connection);
conn.Open(); //打开连接
MySqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read()) //如果用户名输入正确
{ {
Label3.Text = "登陆成功!";
}
connection.Close();
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();
String sql = "select * from user where userName='" + TextBox1.Text + "'" + " and password='" + TextBox2.Text + "';";
MySqlCommand command = new MySqlCommand(sql, connection);
conn.Open(); //打开连接
MySqlDataReader sdr = command .ExecuteReader();
if (sdr.Read()) //如果用户名输入正确
{
Session["username"] = txtUserName.Text.Trim();//存储用户名
Label3.Text = "登陆成功!";
}
sdr.Close();
connection.Close();