有三种
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();
}
原文:https://www.cnblogs.com/wang-jingyuan/p/14815200.html