先从数据库取到值 赋值到控件上
在控件上修改
然后在更新数据
显示数据
string str = "server=.;uid=sa;password=;database=test";
string str1 = "select * from student where id='"+int.Parse(this.textBox6.Text)+"'";
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand com = new SqlCommand(str1, con);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = com;
sda.Fill(dt);
this.textBox2.Text=dt.Rows[0][1].ToString();
this.textBox3.Text=dt.Rows[0][2].ToString();
this.textBox4.Text=dt.Rows[0][3].ToString();
this.textBox5.Text=dt.Rows[0][4].ToString();
this.textBox6.Text=dt.Rows[0][5].ToString();
更新数据
try
{
string str="server=.;uid=sa;password=;database=test";
string str1="update student set sex='"+this.textBox3.Text+"',age='"+int.Parse(this.textBox4.Text)+"',address='"+this.textBox5.Text+"',tel='"+int.Parse(this.textBox6.Text)+"' where name='"+this.textBox2.Text+"'";
SqlConnection
con=new SqlConnection(str);
con.Open();
SqlCommand com=new SqlCommand(str1,con);
com.ExecuteNonQuery();
Form1 form1=new Form1();
form1.Show();
this.Hide();
MessageBox.Show("学员"+this.textBox2.Text+"以修改");
con.Close();
}
catch(Exception ee)
{
MessageBox.Show(ee.ToString());
}