private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection nope = new SqlConnection(@"Data Source=WAYNE-PC;Initial Catalog=期刊;Integrated Security=True");
            string sql = string.Format("update 期刊信息表 set 刊名=" + textBox1.Text + ",年=" + textBox2.Text + ",数量=" + textBox3.Text + ",卷=" + textBox4.Text + ", 库存=" + textBox5.Text + "");
            SqlCommand nopel = new SqlCommand(sql, nope);
            nope.Open();
            int temp = (int)nopel.ExecuteNonQuery();
            if (temp > 0)
            {
                MessageBox.Show("修改成功!");
                Form7 修改 = new Form7();
                修改.Show();
                this.Hide();
            }
运行怎么更新不了,datagridview我已经和textbox绑定了,哪里错了???

解决方案 »

  1.   

    应该是:string sql = string.Format("update 期刊信息表 set 刊名='" + textBox1.Text + "',年='" + textBox2.Text + "',数量=" + textBox3.Text + ",卷=" + textBox4.Text + ", 库存=" + textBox5.Text + "");
    在set 字段='值',如果字段是字符类型的,值的前后要加单引号。
      

  2.   

    调试的时候先复制生成的SQL字符串在企业管理器里执行一下试试
      

  3.   

    string sql = string.Format("update 期刊信息表 set 刊名=" + textBox1.Text + ",年=" + textBox2.Text + ",数量=" + textBox3.Text + ",卷=" + textBox4.Text + ", 库存=" + textBox5.Text + "");改为
    string sql = string.Format("update 期刊信息表 set 刊名= ' " + textBox1.Text + "',年='" + textBox2.Text + "',数量='" + textBox3.Text + "',卷='" + textBox4.Text + "', 库存='" + textBox5.Text + "'");
    反正就是要在set columna='value'这个value要加单引号的
      

  4.   

    private void button1_Click(object sender, EventArgs e)
      {
      SqlConnection nope = new SqlConnection(@"Data Source=WAYNE-PC;Initial Catalog=期刊;Integrated Security=True");
      string sql = string.Format("update 期刊信息表 set 刊名=" + textBox1.Text + ",年=" + textBox2.Text + ",数量=" + textBox3.Text + ",卷=" + textBox4.Text + ", 库存=" + textBox5.Text + "");这里用参数方法,不要直接这样写,维护麻烦
    String sql="update 期刊信息表 set 刊名=@kanming ....";另外你这里的更新是更新表中的所有记录,应该有条件的吧。comm.Parameters.Add(new SqlParameter("@@kanming",textBox1.Text); SqlCommand nopel = new SqlCommand(sql, nope);
      nope.Open();
      int temp = (int)nopel.ExecuteNonQuery(); //这里不需要再转换,他返回的值直接就是int型
      if (temp > 0)
      {
      MessageBox.Show("修改成功!");
      Form7 修改 = new Form7();
      修改.Show();
      this.Hide();
      }
      

  5.   

    问题已经解决了,是自己的小错误,感谢上面对“小白”的提点!thanks  to everyone!