分布式网络开发:
一 目录:
在网站下,新建App_Code文件,在该目录下创建三个文件夹,通常情况为Business(
业务服务,例如UserBS),Comman(封装服务,例如UserInfo)或称Entities,然后是
DataAccess(数据服务,例如DataBaseInfo(固定的数据库连接类)和UserData),最后是UI服
务(用于数据的接收和显示)
二 顺序:
1 先写DataBaseInfo建立数据库连接,注意默认数据库不要写错。
2 封装字段,用get,set方法,分别把数据库表中的相关字段封装。
为BS类中UserData下自定义的方法实例化,同时包含到新的方法中
例:
public void User(UserInfo ui)//User为UserBS类中的新的自定义方法
{
UserData = user = new UserData();
user.InsertUser(ui);//为插入方法赋值
}
public DataSet SelectUser()//最后
{
UserData obj = ne UserData();
DataSet ds = obj.SelectUserData();
return ds;
}
3 回到UserData类中,首先写下sql语句,根据需要实现的功能,比如插入一条数据(写前先导包,using.System.Data.SqlClient):
const string insertsql = "insert into reg
(username,password,sex,birthday,address,userlike) values
(@Name,@PassWord,@Sex,@Birthday,@Address,@Like)";//这里用Login数据库做示范,其reg表中包含username,password,sex,birthday,address,userlike字段
开始写包括增,删,改,查的方法代码,在这里拿更新举例
例:
public bool InsertUser(UserInfo ui)
{
using (SqlConnection con = new SqlConnection(Data.conster))
{
using (SqlCommand cmd = new SqlCommand(insertsql,con))
{
cmd.Parameters.Add(new SqlParameter
("@Name",SqlDbType.VarChar,50));
cmd.Parameters[0].Value = ui.Name;
cmd.Parameters.Add(new SqlParameter
("@PassWord",SqlDbType.VarChar,50));
cmd.Parameters[1].Value = ui.Password;
cmd.Parameters.Add(new SqlParameter
("@Sex",SqlDbType.VarChar,50));
cmd.Parameters[2].Value = ui.Sex;
cmd.Parameters.Add(new SqlParameter
("@Birthday",SqlDbType.DateTime));
cmd.Parameters[3].Value = ui.Birthday;
cmd.Parameters.Add(new SqlParameter
("@Address",SqlDbType.VarChar,50));
cmd.Parameters[4].Value = ui.Address;
cmd.Parameters.Add(new SqlParameter
("@Like",SqlDbType.VarChar,50));
cmd.Parameters[5].Value = ui.Like;
con.Open();
int count = cmd.ExecuteNonQuery();
con.Close();
}
}
return true;
}
三 页面:
返回页面login.aspx和reg.aspx进行判定
1 判断用户名和密码是否与数据库一致(Button下):
例:
protected void Button1_Click(object sender, EventArgs e)
{
//if (this.TextBox1.Text == "admin" && this.TextBox2.Text == "admin")
//{
// Response.Redirect("Reg.aspx?userid=" + this.TextBox1.Text);
//}
EmployeeBS obj = new EmployeeBS();
for (int i = 0; i < obj.SelectUser().Tables[0].Rows.Count; i++)
{
if (this.TextBox1.Text == obj.SelectUser().Tables[0].Rows[i][0].ToString())
{
if (this.TextBox2.Text == obj.SelectUser().Tables[0].Rows[i][1].ToString())
{
Response.Redirect("Reg.aspx?userid=" + this.TextBox1.Text);
}
else
{
this.Label1.Text = "密码不正确";
return;
}
}
}
this.Label1.Text = "用户名不存在";
return;
}
2 后判断用户是否已登陆(注册/修改)Page_Load下装载
例:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
object userid = Request.Params["userid"];
if (userid!=null)
{
this.LinkButton1.Text = "修改";
this.TextBox1.Text = userid.ToString();
this.Label1.Text = "修改信息";
EmployeeBS obj = new EmployeeBS();
for (int i = 0; i < obj.SelectUser().Tables[0].Rows.Count; i++)
{
if (this.TextBox1.Text == obj.SelectUser().Tables[0].Rows[i][0].ToString())
{
//if (obj.SelectUser().Tables[0].Rows[i][3].ToString() == "男")
//{
// RadioButton1.Checked;
//}
//else
//{
// RadioButton2.Checked;
//}
string mes = obj.SelectUser().Tables[0].Rows[i][3].ToString();
string[] strs = mes.Split('-');
this.DropDownList1.Text = strs[0];
this.DropDownList2.Text = strs[1];
this.DropDownList3.Text = strs[2];
this.TextBox4.Text = obj.SelectUser().Tables[0].Rows[i][4].ToString();
}
}
}
else
{
this.LinkButton1.Text = "注册";
this.Label1.Text = "注册信息";
}
}
}
3 判断注册的相关信息是否合法(密码确认,用户名是否存在等)
例:
protected void LinkButton1_Click(object sender, EventArgs e)
{
if (this.LinkButton1.Text == "修改")
{
UserInfo into = new UserInfo();
into.Name = this.TextBox1.Text;
if (this.TextBox2.Text == this.TextBox3.Text)
{
into.Password = this.TextBox2.Text;
}
else
{
this.Label2.Text = "密码不一致,请重新输入";
return;
}
if (this.RadioButton1.Checked)
{
into.Sex = this.RadioButton1.Text;
}
else
{
into.Sex = this.RadioButton2.Text;
}
into.Birthday = Convert.ToDateTime(this.DropDownList1.Text + "-" + this.DropDownList2.Text + "-" + this.DropDownList3.Text);
into.Address = this.TextBox4.Text;
into.Like = this.CheckBox1.Text;
//if (this.CheckBox1.Checked)
//{
// Like[0] = this.CheckBox1.Text;
//}
EmployeeBS employee = new EmployeeBS();
employee.UpdateUser(into);
this.Label2.Text = "修改成功";
}
if (this.LinkButton1.Text == "注册")
{
UserInfo into = new UserInfo();
EmployeeBS obj = new EmployeeBS();
for (int i = 0; i < obj.SelectUser().Tables[0].Rows.Count; i++)
{
if (this.TextBox1.Text == obj.SelectUser().Tables[0].Rows[i][0].ToString())
{
this.Label2.Text = "用户名已存在,请重新输入";
return;
}
}
into.Name = this.TextBox1.Text;
if (this.TextBox2.Text == this.TextBox3.Text)
{
into.Password = this.TextBox2.Text;
}
else
{
this.Label2.Text = "密码不一致,请重新输入";
return;
}
if (this.RadioButton1.Checked)
{
into.Sex = this.RadioButton1.Text;
}
else
{
into.Sex = this.RadioButton2.Text;
}
into.Birthday = Convert.ToDateTime(this.DropDownList1.Text + "-" + this.DropDownList2.Text + "-" + this.DropDownList3.Text);
into.Address = this.TextBox4.Text;
into.Like = this.CheckBox1.Text;
EmployeeBS employee = new EmployeeBS();
employee.Employee(into);
this.Label2.Text = "注册成功";
}
}
浙公网安备 33010602011771号