ASP.net三层架构开发模式实例

Login.aspx
在页面上建用户名和密码的文本框,登录按钮
-----------------------
Login.aspx.cs
 
CODE:

protected void Submit_Click(object sender, EventArgs e)
{
DataAccess.Class1 dac = new DataAccess.Class1();
if (dac.CheckLogin(this.UName.Text, this.pwd.Text))
//Response.Write("Success!");
//Response.Redirect("Default.aspx?");
Server.Transfer("Default.aspx");
else
Response.Write("Failed!");
//Server.Transfer("Login.aspx");
}


----------------------------------------------------
Default.aspx

在页面上建GridView,要写内容到数据库的文本框和按钮
-------------------------
Default.aspx.cs
 
CODE:

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
public void BindData()
{//51aspx.com
DataAccess.Class1 dac = new DataAccess.Class1();
DataSet ds = dac.getUsers();
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
DataAccess.Class1 dac = new DataAccess.Class1();
DataSet ds = dac.getUsers();
DataRow dr = ds.Tables[0].NewRow();
dr["UsersName"] = this.TextBox1.Text;
dr["Passwords"] = this.TextBox2.Text;
dr["BlogName"] = this.TextBox3.Text;
ds.Tables[0].Rows.Add(dr);
dac.updataBystoredprocedure(ds);
BindData();//51aspx.com
}


-------------------------------------------------------------
Common类库:
 
CODE:

//cn
public const string ConnectString = "server=localhost;database=DBxBlog;UID=xBlog;PassWord=123456";
//Sql
public const string Sql_getUser = "select * from Users";
//Table
public const string table_Users = "Users";
//StoredProcedure
public const string StoredProcedure_insert = "insertUsers";


-------------------------------------------------------------------
DataAccess类库(数据层):

 
CODE:


SqlConnection cn = new SqlConnection(Common.Class1.ConnectString);
SqlDataAdapter da;
DataSet ds=new DataSet();

//gettable
public DataSet getUsers()
{
SqlCommand cmd = new SqlCommand(Common.Class1.Sql_getUser,cn);
da = new SqlDataAdapter(cmd);
da.Fill(ds,Common.Class1.table_Users);
return ds;
}

//Login
public bool CheckLogin(string name, string pwd)
{
try
{
SqlCommand cmd = new SqlCommand("select count(*) from Users where UsersName='" + name + "'and Passwords='" + pwd + "'", cn);
cn.Open();
int count = -1;
count = Convert.ToInt32(cmd.ExecuteScalar());
if (count < 1)
return false;
return true;
}
catch (Exception ex)
{
return false;
}
}
//textbox updata
public void updata(DataSet ds)
{
SqlCommand cmd = new SqlCommand("insert into Users(UsersName,Passwords,BlogName)valuse(@UsersName,@Passwords,@BlogName)", cn);
cmd.Parameters.Add("@UsersName",SqlDbType.VarChar,20,"UsersName");
cmd.Parameters.Add("@Passwords", SqlDbType.VarChar, 50, "Passwords");
cmd.Parameters.Add("@BlogName", SqlDbType.VarChar, 20, "BlogName");

da=new SqlDataAdapter ();
da.InsertCommand=cmd;
cn.Open();
da.Update(ds,Common.Class1.table_Users);
cn.Close();
}
//stroedprocedure updata
public void updataBystoredprocedure(DataSet ds)
{
SqlCommand cmd = new SqlCommand(Common.Class1.StoredProcedure_insert,cn);
cmd.Parameters.Add("@UsersName",SqlDbType.VarChar,20,"UsersName");
cmd.Parameters.Add("@Passwords", SqlDbType.VarChar, 50, "Passwords");
cmd.Parameters.Add("@BlogName", SqlDbType.VarChar, 20, "BlogName");
cmd.CommandType = CommandType.StoredProcedure;
da=new SqlDataAdapter();
da.InsertCommand=cmd;
cn.Open();
da.Update(ds,Common.Class1.table_Users);
cn.Close();
}


 

你可能感兴趣的:(职场,休闲,三层架构)