断开式数据库的例子

public partial class Register : System.Web.UI.Page
{
    private SqlConnection conn;
    private SqlDataAdapter dad;
    private DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBuserConnectionString"].ToString());
        this.dad = new SqlDataAdapter("select id,username,userpwd,sex,address,birthday from userdetails", conn);
        this.ds=new DataSet();
        this.dad.Fill(ds);


        if (!this.IsPostBack)
        {
        
            //绑定下拉列表框
            this.BindDropDowLlist();
            //绑定表单
            this.BindForm();

            this.Button2.Attributes.Add("onclick", "return confirm('Delete?')");
            this.Button3.Attributes.Add("onclick", "return confirm('insert?')");
            this.Button4.Attributes.Add("onclick", "return confirm('update?')");
        }

    }
    private void BindDropDowLlist()
    {
        //为下拉列项提供数据源字段
        this.DropDownList5.DataTextField = "username";
        this.DropDownList5.DataValueField = "id";
        //指定数据源
        this.DropDownList5.DataSource = ds.Tables[0];
        //将数据绑定到控件中
        this.DropDownList5.DataBind();

        this.DropDownList1.DataTextField = "address";
        this.DropDownList1.DataSource = ds.Tables[0];
        this.DropDownList1.DataBind();
    }

    private void BindForm() {
        //是一条记录的集合,所以要用数组;
        DataRow[] dr = ds.Tables[0].Select("id = "+ this.DropDownList5.SelectedValue);
        //这里访问的也是当前根据id筛选的数据;
        this.TextBox1.Text = dr[0]["username"].ToString();
        this.TextBox2.Text = dr[0]["userpwd"].ToString();
        this.TextBox3.Text = dr[0]["userpwd"].ToString();
        //if (dr[0]["sex"].ToString().Equals("boy")) {
        //    this.RadioButton1.Checked=true;
        //    this.RadioButton2.Checked = false;
        //}

        //else if (dr[0]["sex"].ToString().Equals("girl"))
        //{

        //    this.RadioButton2.Checked = true;
        //    this.RadioButton1.Checked = false;
        //}
        //else {
        //    this.RadioButton2.Checked = false;
        //    this.RadioButton1.Checked = false;
        //}
        //for (int index = 0; index < this.DropDownList1.Items.Count; index++)
        //{
        //    if (dr[0]["address"].ToString().Equals(this.DropDownList1.Items[index].Text))
        //    {
        //        this.DropDownList1.Items[0].Text = dr[0]["address"].ToString();
        //    }
        //}
    }

    private void DeleteRow() {
        //是一条记录的集合,所以要用数组;
        DataRow[] dr = ds.Tables[0].Select("id = " + this.DropDownList5.SelectedValue);
        //删除当前行;
        dr[0].Delete();


    }

    private void UpdateSQL() {
        //利用此对象自动生成insert,update,delete三种SQL语句
        SqlCommandBuilder cb = new SqlCommandBuilder(this.dad);
        //利用SqlDataAdapter对象的Update方法修改数据库的记录;
        this.dad.Update(this.ds);

        //重新绑定列表和表单;
        this.BindDropDowLlist();
        this.BindForm();
    }

    private void InsertRow() {
        //添加一行数据
        DataRow dr = ds.Tables[0].NewRow();
        dr["username"] = this.TextBox1.Text;
        dr["userpwd"] = this.TextBox2.Text;
        dr["userpwd"] = this.TextBox3.Text;
        ds.Tables[0].Rows.Add(dr);
    }
    private void updateRow() {
        //是一条记录的集合,所以要用数组;
        DataRow[] dr = ds.Tables[0].Select("id = " + this.DropDownList5.SelectedValue);
        dr[0]["username"] = this.TextBox1.Text;
        dr[0]["userpwd"] = this.TextBox2.Text;
        dr[0]["userpwd"] = this.TextBox3.Text;
    }

    protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindForm();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.Button1.Text == "New")
        {
            this.Button1.Text = "cancel";
            this.DropDownList5.Enabled = false;
            this.TextBox1.Text = " ";
            this.TextBox2.Text = " ";
            this.TextBox3.Text = " ";

        }
        else {
            this.Button1.Text = "New";
            this.DropDownList5.Enabled = true;
            this.TextBox1.Enabled = true;
            this.TextBox2.Enabled = true;
            this.TextBox3.Enabled = true;
            this.BindDropDowLlist();
            this.BindForm();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.DeleteRow();
        this.UpdateSQL();
        this.RegisterStartupScript("", "<script>alert('删除成功')</script>");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {

        this.InsertRow();
        this.UpdateSQL();
        this.RegisterStartupScript("", "<script>alert('插入成功')</script>");

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        this.updateRow();
        this.UpdateSQL();
        this.RegisterStartupScript("", "<script>alert('修改成功')</script>");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值