先介绍 一下我想干什么我做了一个学生成绩管理系统,目前实现了 通过窗体界面的文本框往数据库的一张Student表写数据,也可按学号查询学生的成绩。(都很正常)
现在 我想 在查询完学生成绩后 修改文本框的数据然后再重新写到数据库 可是老是失败我的代码如下:
String conString = @"Data Source=PC-201303201731\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True;Pooling=False";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = conString;
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
String cmdString = "update Student set StuJava='@StuJava',[StuAsp]='@StuAsp',[Stu汇编]='@Stu汇编',[StuLinux]='@StuLinux',[StuC]='@StuC',[StuC#]='@StuC#' where StuId=" + textBox1.Text;
//StuJAVA,StuAsp 等等是字段名
SqlCommand cmd = new SqlCommand(cmdString, conn);
cmd.Parameters.Add(new SqlParameter("@StuJava", this.textBox2.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@StuAsp", this.textBox3.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@Stu汇编", this.textBox4.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@StuLinux", this.textBox5.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@StuC", this.textBox6.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@StuC#", this.textBox7.Text.Trim()));
int count = 0;
try
{ conn.Open();
count = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw new Exception(ex.Message); }
finally
{
MessageBox.Show("修改成功!");
conn.Close();
}
现在 我想 在查询完学生成绩后 修改文本框的数据然后再重新写到数据库 可是老是失败我的代码如下:
String conString = @"Data Source=PC-201303201731\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True;Pooling=False";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = conString;
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
String cmdString = "update Student set StuJava='@StuJava',[StuAsp]='@StuAsp',[Stu汇编]='@Stu汇编',[StuLinux]='@StuLinux',[StuC]='@StuC',[StuC#]='@StuC#' where StuId=" + textBox1.Text;
//StuJAVA,StuAsp 等等是字段名
SqlCommand cmd = new SqlCommand(cmdString, conn);
cmd.Parameters.Add(new SqlParameter("@StuJava", this.textBox2.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@StuAsp", this.textBox3.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@Stu汇编", this.textBox4.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@StuLinux", this.textBox5.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@StuC", this.textBox6.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@StuC#", this.textBox7.Text.Trim()));
int count = 0;
try
{ conn.Open();
count = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw new Exception(ex.Message); }
finally
{
MessageBox.Show("修改成功!");
conn.Close();
}
parameter.Value= this.textBox2.Text.Trim()
cmd.Parameters.Add(parameter);