ASP.NET 三层结构

http://www.chinaitbbs.com.cn/bbs/topic.aspx?topicid=14476  

在页面上建用户名和密码的文本框,登录按钮-----------------------Login.aspx.csCODE: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.csCODE: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 C;        //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();        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值