1.注册用户向数据库中插入数据
点击注册后的操作
protected void Button1_Click(object sender, EventArgs e)
{
DClass db = new DClass();
try
{
//判断用户名是否已经注册
SqlDataReader read = db.getRecods("select * from users where uname='"
+ this.TextBox1.Text + "'");
read.Read();
if (read.HasRows)
{
if (this.TextBox1.Text == read["uname"].ToString())
{
Response.Write("<script>alert('对不起,该用户已经注册!');location='Register.aspx'</script>");
return;
}
}
read.Close();
//把注册的信息插入到数据库中
String cmdText = "insert into users(uname,img,bir,upass,uapass,rname,sex,phone,tel,QQ,email,hobby)";
cmdText += "values('" + this.TextBox1.Text + "','"+DropDownList1.SelectedValue+"','" +DropDownList2.SelectedValue+DropDownList3.SelectedValue+DropDownList4.SelectedValue+"','"+ TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text
+ "','" + DropDownList5.SelectedValue + "','" + TextBox5.Text + "','" + TextBox6.Text + "'," + TextBox7.Text + ",'"
+ TextBox8.Text + "',null)";
//Response.Write(cmdText);
int count = db.ExecuteSql(cmdText);
if (count > 0)
{
Response.Write("<script>alert('注册成功!');</script>");
Response.Redirect("welcome.aspx");
}
else
{
Response.Write("<script>alert('注册失败!');history.back();</script>");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally {
}
}
public class DClass
{
public SqlConnection myConn = null;
public DClass()
{
myConn = new SqlConnection("Server=(local); Database = db_user; uid = sa; password = sa123");
myConn.Open();//打开连接
}
//select选择查询操作
public SqlDataReader getRecods(string sqlText)
{
SqlCommand myCmd = new SqlCommand(sqlText, myConn);//查询对象
SqlDataReader dr = myCmd.ExecuteReader();//创建逐行数据读取器对象
return dr;
}
//Insert/delete/Update操作查询
public int ExecuteSql(String Sql_Command)
{
SqlCommand comm = new SqlCommand(Sql_Command, myConn);
int x = comm.ExecuteNonQuery();//对数据库的增、删、改,操作记录的数目
myConn.Close();
return x;
}
}
2.用户登录时,从数据库中取出数据进行验证。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
DBClass db1 = new DBClass();
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Register.aspx");
}
protected void Button2_Click(object sender, EventArgs e)
{
try {
string cmdText = "select * from users where uname='"
+ TextBox1.Text.Trim() + "'and upass='" + TextBox2.Text.Trim() + "'";
SqlDataReader dr = db1.getRecods(cmdText);
dr.Read();
if (dr.HasRows)
{
Response.Write("<script>alert('登录成功!');</script>");
Response.Redirect("welcome.aspx");
}
else
{
Response.Write("<script>alert('用户名或密码错误!');</script>");
}
} catch (Exception ex) {
Response.Write(ex.Message);
}
}
}
public class DBClass
{
public SqlConnection myConn = null;
public DBClass()
{
myConn = new SqlConnection("Server=(local); Database = db_user; uid = sa; password = sa123");
}
//select选择查询操作
public SqlDataReader getRecods(string sqlText)
{
myConn.Open();//打开连接
SqlCommand myCmd = new SqlCommand(sqlText, myConn);//查询对象
SqlDataReader dr = myCmd.ExecuteReader();//创建逐行数据读取器对象
return dr;
}
//Insert/delete/Update操作查询
public int ExecuteSql(String Sql_Command)
{
myConn.Open();
SqlCommand comm = new SqlCommand(Sql_Command, myConn);
int x = comm.ExecuteNonQuery();//对数据库的增、删、改,操作记录的数目
myConn.Close();
return x;
}
}