现已将需要修改的数据信息取出在文本框中,如下代码:
SqlConnection conn = new SqlConnection(WindowsFormsApplication1.Properties.Settings.Default.StudentsSysConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
if (stuno.Text == "")
cmd.CommandText = id;
else
cmd.CommandText = "select * from StudentTable where stuno=" + stuno.Text.Trim();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[0].Rows.Count == 1)
{
this.xuehao.Text = (ds.Tables[0].Rows[0]["stuno"].ToString());
this.xingming.Text = (ds.Tables[0].Rows[0]["stuname"].ToString());
this.nianji.Text = (ds.Tables[0].Rows[0]["gradeno"].ToString());
this.banji_.Text = (ds.Tables[0].Rows[0]["classno"].ToString());
this.xingbie.Text = (ds.Tables[0].Rows[0]["stusex"].ToString());
this.minzu.Text = (ds.Tables[0].Rows[0]["nation"].ToString());
this.shenfenzheng.Text = (ds.Tables[0].Rows[0]["idcard"].ToString());
this.dizhi.Text = (ds.Tables[0].Rows[0]["address"].ToString());
conn.Close();
}
然后进行修改,如下图:
点击确定修改后(xiugaibtn_Click)如下代码:
private void xiugaibtn_Click(object sender, EventArgs e)
{
if (xuehao.Text.Trim() == "" || xingming.Text.Trim() == "" || nianji.Text.Trim() == "" || banji_.Text.Trim() == ""
|| xingbie.Text.Trim() == "" || minzu.Text.Trim() == "" || shenfenzheng.Text.Trim() == ""|| dizhi.Text.Trim() == "")
{
MessageBox.Show("请输入完整信息!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string str1 = xuehao.Text.Trim();
int l = str1.Length;
for (int i = 0; i < l; i++)
{
if (!char.IsNumber(str1[i]))
{
MessageBox.Show("输入学号有误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
xuehao.SelectAll();
xuehao.Focus();
return;
}
}
SqlConnection conn = new SqlConnection(WindowsFormsApplication1.Properties.Settings.Default.StudentsSysConnectionString);
conn.Open();
string sql = "update StudentTable set stuno='" + xuehao.Text + "',stuname='" + xingming.Text + "',gradeno='" + nianji.Text
+ "',classno='" + banji_.Text + "',stusex='" + xingbie.Text + "',nation='" + minzu.Text
+ "',idcard='" + shenfenzheng.Text + "',address='" + dizhi.Text + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
connection.Close();
xiugaixuesheng_Click(sender, e);
}
}
但是为什么数据库中的信息不能正确更改呢,在修改信息代码中是sql语句有问题吗?希望大家帮忙解决一下,谢谢啦!