用dataGridView实现对数据库数据的修改并显示

private void update_Click(object sender, EventArgs e) { int stuNo = 0; string stuName = ""; string stuSex = ""; int stuAge = 0; string stuID = ""; string stuAddress = ""; string sql = ""; for (int i = 0; i <this.dataGridView1.Rows.Count; i++) { stuNo = int.Parse(this.dataGridView1.Rows[i].Cells[0].Value.ToString()); stuName = this.dataGridView1.Rows[i].Cells[1].Value.ToString(); stuSex = this.dataGridView1.Rows[i].Cells[2].Value.ToString(); stuAge = int.Parse(this.dataGridView1.Rows[i].Cells[3].Value.ToString()); stuID = this.dataGridView1.Rows[i].Cells[4].Value.ToString(); stuAddress = this.dataGridView1.Rows[i].Cells[5].Value.ToString(); sql = "update stuInfo set stuName='" + stuName + "',stuSex='" + stuSex + "',stuAge="+stuAge+",stuID='" + stuID + "',stuAddress='" + stuAddress + "' where stuNo="+stuNo+""; DBHelp.ExecuteSql(sql); } string sql2 = "select * from stuInfo"; DBHelp.GetTable(sql2); DataTable dt = DBHelp.GetTable(sql2); this.dataGridView1.DataSource = dt;}

你可能感兴趣的:(sql,数据库,String,object)