62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace WebApplication37
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void B_login_Click(object sender, EventArgs e)
{
if (tb_id.Text == "" || tb_pwd.Text == "")
{
Response.Write("输入登录信息不完整,请重新输入!");
}
SqlConnection con = new SqlConnection("Server = ygu-PC\\SQLEXPRESS;User Id = YGU-PC\\ygu;DataBase = rcloud");
SqlConmmand cmd = new SqlCommand(string.Format("SELECT COUNT(*)FROM rcloud WHERE username = '{0}'AND userpassword = '{1}'", this.tb_id.Text.Trim(), this.tb_pwd.Text.Trim()), con);
}
protected void B_reg_Click(object sender, EventArgs e)
{
Response.Redirect("registe.aspx");
}
}
}
if (tb_id.Text == "" || tb_pwd.Text == "")
{
Response.Write("输入登录信息不完整,请重新输入!");
}
SqlConnection con = new SqlConnection("Server = ygu-PC\\SQLEXPRESS;User Id = YGU-PC\\ygu;DataBase = rcloud");
SqlConmmand cmd = new SqlCommand(string.Format("SELECT COUNT(*)FROM rcloud WHERE username = '{0}'AND userpassword = '{1}'", this.tb_id.Text.Trim(), this.tb_pwd.Text.Trim()), con);
int i = int.Parse(cmd.ExecuteScalar().ToString());
if (i > 0)
Response.Redirect("主页.aspx");
else
{
Response.Write("<script>alert('信息错误,登录失败!')</script>");
}
con.Close();
注册
SqlConnection con = new SqlConnection("Server = ygu-PC\\SQLEXPRESS;User Id = YGU-PC\\ygu;DataBase = rcloud");
if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "" &7 TextBox6.Text != "")
{
string sql=""; //要执行的 sql 语句
SqlConmmand cmd = new SqlCommand(sql, con);//同登录
int i = int.Parse(cmd.ExecuteNonQuery().ToString());
if (i > 0)
{
Response.Write("<script>alert('恭喜您,注册成功!')</script>");
}
}
else
{
Response.Write("<script>alert('用户名或密码不能为空!')</script>");
}
大概就是这个样子的吧,刚接触可以用这种方法来做,以后还是建议按照1楼的方法,封装一个简单的数据库访问类, public DataSet ExecuteDataSet(string sql)
{
OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection);
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
return ds;
}
}
然后就是调用逻辑了。这是注册部分。
重置按钮就更简单了,直接把几个textbox.Text="";即可。public class DataAccess
{
private OleDbConnection oleDbConnection;
public DataAccess()
{
//
oleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=se.mdb;");
if (oleDbConnection.State != ConnectionState.Open)
{
oleDbConnection.Open();
}
}
public int ExecuteSql(string sql)
{
OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection);
return cmd.ExecuteNonQuery();
}
public int ExecuteSql(string sql, ref OleDbParameter[] oleDbParameters)
{
OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection);
cmd.Parameters.AddRange(oleDbParameters);
return cmd.ExecuteNonQuery();
}
然后写sql语句,调用里面的方法执行就可以了。