日期:2014-05-18 浏览次数:20887 次
string person_ID = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString(); string person_name = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString(); string person_age = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString(); string person_sex = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString(); //然后将数据更新到数据库、 SqlConnection coon = new SqlConnection(connectionstring); coon.Open(); SqlCommand cmd = new SqlCommand("update 酒店人员 set 姓名 =@name, 年龄 =@age, 性别 =@sex where 工作编号 =@id,coon); cmd.Parameters.AddWithValue("@name",person_name ); cmd.Parameters.AddWithValue("@age",person_age ); cmd.Parameters.AddWithValue("@sex",person_sex ); cmd.Parameters.AddWithValue("@id",person_ID); SqlDataReader dr = cmd.ExecuteReader(); coon.Close(); MessageBox.Show("修改数据成功!");
------解决方案--------------------
插入行:
string insertStr = "insert into 学生信息(学号,姓名,性别,出生日期,民族,所属班级,家庭地址) values ('','','','','','','')"; SqlCommand insert_com = new SqlCommand(insertStr, conn); if (insert_com.ExecuteNonQuery() > 0) { MessageBox.Show("插入新数据成功","成功提示"); }