首页 > 数据库技术 > 详细

asp.net对数据库增删改操作

时间:2021-05-26 23:21:39      阅读:20      评论:0      收藏:0      [点我收藏+]

有三种
protected void Button1_Click(object sender, EventArgs e)
{
string id = TextBox1.Text.Trim();
string name = TextBox2.Text.Trim();
string sex = DropDownList1.Text;
string department = DropDownList2.Text;
string constr = ConfigurationManager.ConnectionStrings["xby"].ToString();
MySqlConnection conn = new MySqlConnection(constr);
conn.Open();
string selstr = "select * from student";
MySqlDataAdapter da = new MySqlDataAdapter(selstr, conn);
MySqlCommandBuilder builder = new MySqlCommandBuilder(da);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow mr = dt.NewRow();
mr[0] = id;
mr[1] = name;
mr[2] = sex;
mr[3] = department;
dt.Rows.Add(mr);
da.Update(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
conn.Close();
}

    protected void Button2_Click(object sender, EventArgs e)
    {
        string id = TextBox3.Text.Trim();
        string tname = TextBox4.Text.Trim();
        string tclass = TextBox5.Text.Trim();
        string constr1 = ConfigurationManager.ConnectionStrings["xby"].ToString();
        MySqlConnection conn1 = new MySqlConnection(constr1);
        conn1.Open();
        string selstr1 = "select * from teacher";
        MySqlDataAdapter da1 = new MySqlDataAdapter();
        da1.SelectCommand = new MySqlCommand(selstr1, conn1);
        MySqlCommandBuilder builder = new MySqlCommandBuilder(da1);
        DataSet ds = new DataSet();
        da1.Fill(ds);
        DataRow NewRow = ds.Tables[0].NewRow();
        NewRow[0] = id;
        NewRow[1] = tname;
        NewRow[2] = tclass;
        ds.Tables[0].Rows.Add(NewRow);
        da1.Update(ds);
        Response.Write("<script language=javascript>alert(‘新纪录添加成功‘)");
        GridView2.DataSource = ds.Tables[0];
        GridView2.DataBind();
        conn1.Close();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        string id = TextBox6.Text.Trim();
        string cname = TextBox7.Text.Trim();
        string tname = TextBox8.Text.Trim();
        string constr2 = ConfigurationManager.ConnectionStrings["xby"].ToString();
        MySqlConnection conn2 = new MySqlConnection(constr2);
        conn2.Open();
        string selstr2 = "select * from course";
        MySqlCommand command = new MySqlCommand(selstr2, conn2);
        MySqlDataReader dr = cmd.ExecuteReader();
        GridView3.DataSource = dr;
        GridView3.DataBind();
        string sql3 = "Insert into course(id,cname,tname) Values(‘05‘,‘线性代数‘,‘黎明‘)";
        MySqlCommand cmd = new MySqlCommand(sql3, conn2);
        int Num = cmd.ExecuteNonQuery();
        Response.Write("受影响的记录数为:" + Num.ToString());
        MySqlDataReader dr = cmd.ExecuteReader();
        GridView3.DataSource = dr;
        GridView3.DataBind();
        conn2.Close();
    }

asp.net对数据库增删改操作

原文:https://www.cnblogs.com/wang-jingyuan/p/14815200.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!