連接式數據庫的例子

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>");
    }
}

發佈了33 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章