先介绍 一下我想干什么我做了一个学生成绩管理系统,目前实现了  通过窗体界面的文本框往数据库的一张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();
            }

解决方案 »

  1.   

    加断点进行调试跟一下cmdString的值,等到给cmdString赋完值,将语句复制到查询分析器中执行,如果语句有问题很容易就看出来了。
      

  2.   

    数据类型没有转换照成的错误,像学生的成绩应该是tinyint型的但从text中获取的数据是字符串型的所以应该这样写SqlParameter parameter=new SqlParameter("@StuJava",SqlDbType.TINYINT)  
    parameter.Value= this.textBox2.Text.Trim()
    cmd.Parameters.Add(parameter);