//修改学生信息
private void btnChange_Click(object sender, EventArgs e)
{
if (tbid.Text == "") //若textbox中没有学号不行
{
MessageBox.Show("请给出学生的学号!");
}
else
{
try //连接数据库等操作
{ string sqlString = "update Student set stuName='" + tbname.Text.Trim() + "',stuPwd='" + tbpwd.Text.Trim() + "',stuSex='" + cbsex.Text.Trim() + "',stuAge=" + tbage.Text + ",stuDept'" + cbdept.Text.Trim() + "' where stuId='"+tbid.Text.Trim()+"')";
SqlCommand command = new SqlCommand(sqlString, DB.connection);
int result = (int)command.ExecuteNonQuery();
if (result == 1)
{
MessageBox.Show("修改成功!"); }
else
{
MessageBox.Show("修改失败,请重试!"); }
DB.connection.Close();
}
catch (Exception)
{ MessageBox.Show("修改失败,请重试!"); }
}
可是总是显示修改失败,请重试!求高手指导。
private void btnChange_Click(object sender, EventArgs e)
{
if (tbid.Text == "") //若textbox中没有学号不行
{
MessageBox.Show("请给出学生的学号!");
}
else
{
try //连接数据库等操作
{ string sqlString = "update Student set stuName='" + tbname.Text.Trim() + "',stuPwd='" + tbpwd.Text.Trim() + "',stuSex='" + cbsex.Text.Trim() + "',stuAge=" + tbage.Text + ",stuDept'" + cbdept.Text.Trim() + "' where stuId='"+tbid.Text.Trim()+"')";
SqlCommand command = new SqlCommand(sqlString, DB.connection);
int result = (int)command.ExecuteNonQuery();
if (result == 1)
{
MessageBox.Show("修改成功!"); }
else
{
MessageBox.Show("修改失败,请重试!"); }
DB.connection.Close();
}
catch (Exception)
{ MessageBox.Show("修改失败,请重试!"); }
}
可是总是显示修改失败,请重试!求高手指导。
{ MessageBox.Show("修改失败,请重试! 原因:"+ex.ToString()); }打印一下 失败原因
===
",stuDept='" + cbdept.Text.Trim
是不是多了半个括弧?
比如说年龄是int型,但是我修改时写字符,那么想弹出对话框提示年龄出错,求解。