110,536
社区成员
发帖
与我相关
我的任务
分享
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
str = mytable.Rows[e.RowIndex][0].ToString(); //为变量 str 赋值
textBox1.Text = mytable.Rows[e.RowIndex][0].ToString(); //为文本框属性赋值
textBox2.Text = mytable.Rows[e.RowIndex][1].ToString();
textBox3.Text = mytable.Rows[e.RowIndex][2].ToString();
}
//更新数据库
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("修改数据记录信息不完整,重新输入!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
mycon.Open();
//Updte更新语句
string name=textBox1.Text.Trim();
string phone = textBox2.Text.Trim();
string sex = textBox3.Text.Trim();
//string myupdate = "update person set 手机 = phone,性别 = sex where 姓名 = str";
string myupdate = string.Format (@"UPDATE person SET 姓名='{0}',手机='{1}', ,性别='{2}' WHERE 姓名='{3}',name,phone,sex,str");
OleDbCommand mycmd = new OleDbCommand(myupdate, mycon);
mycmd.ExecuteNonQuery(); //执行更新语句
mycon.Close();
mycon.Dispose(); //释放OleDbConnection对象所占资源空间。
MessageBox.Show("成功修改记录!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}