日期:2014-05-18  浏览次数:20418 次

使用Update命令时,如果不设置where条件,就改掉了所有记录,但是设置了where条件后,没有任何反应.也不提示错误,where条件应该正确.
我这是要对一片文章进行编辑,然后保存,看看这是什么问题啊?
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class Admin_News_Add : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  SqlConnection conn = new SqlConnection();
  conn.ConnectionString = "server=localhost;database=netnews;integrated security=SSPI";
  conn.Open();
  SqlCommand cmd = new SqlCommand();
  cmd.CommandText = "select * from news where id="+Request.QueryString["newsid"];
  cmd.Connection = conn;
  SqlDataReader reader = cmd.ExecuteReader();
  if (reader.Read())
  {
  this.CBgundong.Checked = (bool)reader["Gundong"];
  this.CBimgnews.Checked = (bool)reader["ImgNews"];
  this.Cbtop.Checked = (bool)reader["IndexTop"];
  this.TbTitle.Text = reader["Title"].ToString();
  this.FreeTextBox1.Text = reader["content"].ToString();
  this.TBhits.Text = reader["Hits"].ToString();
  this.TBfrom.Text = reader["From"].ToString();

  }
  else
  {
  Response.Write("没找到!");
  }
  this.TbDate.Text = DateTime.Now.ToString();
  conn.Close();
  }

  protected void Button2_Click(object sender, EventArgs e)
  {
  if (Page.IsValid)
  {
  SqlConnection conn0 = new SqlConnection();
  conn0.ConnectionString = "server=localhost;database=netnews;integrated security=SSPI";
  conn0.Open();
  SqlCommand cmd0 = new SqlCommand();
  cmd0.CommandText = "update news set Title='" + this.TbTitle.Text + "',Gundong='" + this.CBgundong.Checked + "',IndexTop='" + this.Cbtop.Checked + "',Class='" + this.DDLclass.SelectedItem.Text + "',[content]='" + this.FreeTextBox1.Text + "',date='" + this.TbDate.Text + "',Hits='" + this.TBhits.Text + "',[From]='" + this.TBfrom.Text + "' where id=" + Request.QueryString["newsid"];
  cmd0.Connection = conn0;
  cmd0.ExecuteNonQuery();
  conn0.Close();
  this.LB00.Text = Request.QueryString["newsid"].ToString();//测试where条件正确不
  }
  }
}


------解决方案--------------------
cmd0.CommandText = "update news set Title= '" + this.TbTitle.Text + " ',Gundong= '" + this.CBgundong.Checked + " ',IndexTop= '" + this.Cbtop.Checked + " ',Class= '" + this.DDLclass.SelectedItem.Text + " ',[content]= '" + this.FreeTextBox1.Text + " ',date= '" + this.TbDate.Text + " ',Hits= '" + this.TBhits.Text + " ',[From]= '" + this.TBfrom.Text + " ' where id=" + Request.QueryString["newsid"]; 
------------
未必在where 出错 !
但是一定在这个查询语句出错!
你这样写法很多的'"+那么的符号 !有时候少掉一个,或者类型出错了就完蛋了 !
建议写存储过程 !