using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication5
{
public partial class Defauit : System.Web.UI.Page
{
private readonly string connstr;
/// <summary>
/// 用户登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnLogin_Click(object sender, EventArgs e)
{
if (tbusername.Text=="")
{
Response.Write(@"<script>alert('用户名不能为空!');</script>");
}
if (tbpsw.Text=="")
{
Response.Write(@"<script>alert('密码不能为空!');</script>");
}
string username=tbusername.Text;
string password=tbpsw.Text;
string sql = "select * from T_user where username=@username and password=@password";
SqlParameter[] parameter = { new SqlParameter("@username",username),new SqlParameter("@password",password)};
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameter);
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
DataTable table = ds.Tables[0];
if(table != null)
Response.Write(@"<script>alert('登录成功!');</script>");
else
Response.Write(@"<script>alert('登录失败!');</script>");
}
}
}
/// <summary>
/// 用户注册
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BrnRegister_Click(object sender, EventArgs e)
{
string username = tbusername.Text;
string password = tbpsw.Text;
string sql = "Insert into T_user(username, password) values(@username,@password)";
SqlParameter[] parameters = { new SqlParameter("@username", username), new SqlParameter("@password", password) };
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
cmd.ExecuteNonQuery();
}
}
Response.Write(@"<script>alert('注册成功!');</script>");
}
}
}
里面的tbusername和tbpsw一直存在当前上下文不存在的问题!!!!
把aspx的Inherits改成“命名空间.类名”
https://zhidao.baidu.com/question/309128746224564284.html
给你一个类似问题的回答