连接式数据库的例子

public partial class DataReaderDemo : System.Web.UI.Page
{
    private SqlConnection conn;
    private SqlCommand cmd;
    private SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            BindDropDownList();
            BindForm();
            this.Button2.Attributes.Add("onclick", "return cofirm('delete?')");
            this.Button3.Attributes.Add("onclick", "return cofirm('insert?')");
            this.Button4.Attributes.Add("onclick", "return cofirm('update?')");
        }
    }
    private void BindDropDownList() {
        conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBuserConnectionString"].ToString());
        cmd = new SqlCommand("select id,username,userpwd from userdetails", conn);
        conn.Open();
        //也可将SqlDataReader作为数据源绑定给控件;
        this.DropDownList1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        this.DropDownList1.DataBind();
    }
    private void BindForm() {
        conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBuserConnectionString"].ToString());
        cmd = new SqlCommand("select id,username,userpwd from userdetails where id=@id ", conn);
        //用添加参数方式给变量赋值
        cmd.Parameters.Add(new SqlParameter("@id", this.DropDownList1.SelectedValue));
        conn.Open();
        //查询到的一条结果返回到SqlDataReader里
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        //前进到下一条记录
        dr.Read();
        //将记录集的每项分别绑定到控件
        this.TextBox1.Text = dr["username"].ToString();
        this.TextBox2.Text = dr["userpwd"].ToString();
        this.TextBox3.Text = dr["userpwd"].ToString();
    }
    private void DeleteRow() {
        conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBuserConnectionString"].ToString());
        cmd = new SqlCommand("delete from userdetails where id=@id ", conn);
        //用添加参数方式给变量赋值
        cmd.Parameters.Add(new SqlParameter("@id", this.DropDownList1.SelectedValue));
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

    
    }
    private void InsertRow() {
        conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBuserConnectionString"].ToString());
        cmd = new SqlCommand("insert into userdetails(username,userpwd)values(@username,@userpwd) ", conn);
        //用添加参数方式给变量赋值
      
        cmd.Parameters.Add(new SqlParameter("@username", this.TextBox1.Text));
        cmd.Parameters.Add(new SqlParameter("@userpwd", this.TextBox2.Text));
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    private void UpdateRow() {
        conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBuserConnectionString"].ToString());
        cmd = new SqlCommand("update userdetails set username=@username,userpwd=@userpwd where id=@id ", conn);
        //用添加参数方式给变量赋值
        cmd.Parameters.Add(new SqlParameter("@id", this.DropDownList1.SelectedValue));
        cmd.Parameters.Add(new SqlParameter("@username", this.TextBox1.Text));
        cmd.Parameters.Add(new SqlParameter("@userpwd", this.TextBox2.Text));
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindForm();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        DeleteRow();
        BindDropDownList();
        BindForm();
        this.RegisterStartupScript("", "<script>alert('删除成功')</script>");

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        InsertRow();
        BindDropDownList();
        BindForm();
        this.RegisterStartupScript("", "<script>alert('插入成功')</script>");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.Button1.Text == "New")
        {
            this.Button1.Text = "Cancle";
            this.DropDownList1.Enabled = false;
            this.TextBox1.Text = "";
            this.TextBox2.Text = "";
            this.TextBox3.Text = "";
        }
        else
        {
            this.DropDownList1.Enabled = true;
            this.Button1.Text = "New";
            this.TextBox1.Enabled = true;
            this.TextBox1.Enabled = true;
            this.TextBox1.Enabled = true;
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        UpdateRow();
        BindDropDownList();
        BindForm();
        this.RegisterStartupScript("", "<script>alert('修改成功')</script>");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值