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