以下是代码
protected void Button3_Click(object sender, EventArgs e)
  {
  try
  {
  string a = Session["用户名"].ToString();
  SqlConnection con = new SqlConnection("server=.;user id=sa;pwd=;DataBase=book");
  con.Open();
  string UpateSql = "Update reader SET 电子邮箱='" + TextBox2.Text + "',QQ号码='" + TextBox1.Text + "',备注='" + TextBox3.Text + "' where 学号='" + a + "'";
  SqlCommand com = new SqlCommand(UpateSql, con);
  com.ExecuteNonQuery();
  SqlDataAdapter ada = new SqlDataAdapter("Select * From reader", con);
  DataSet ds = new DataSet();
  ada.Fill(ds);
  con.Close();
  Response.Write("<script language=javascript>alert('修改成功!')</script>");
  }
  catch
  {
  Response.Write("<script language=javascript>alert('修改失败!')</script>");
  }
  }
执行之后没错,也提示修改成功,但是数据库的表里没有更新。我想实现的是更新reader表里的三列,其他列都不动,不知道代码有没有写错,高手帮忙看看了。在线等

解决方案 »

  1.   

    提示成功并不一定成功啊,你可以再加个判断逻辑就知道你更新的成功没了,例如用SqlDataReader读取一条你更新的数据,要不就是你的学号那出问题了,你这更新语句写的你还是先加个判断吧
    你那成功提示总归都是提示的
      

  2.   

    string UpateSql = "Update reader SET 电子邮箱='" + TextBox2.Text + "',QQ号码='" + TextBox1.Text + "',备注='" + TextBox3.Text + "' where 学号='" + a + "'";
    =======
    单步调试 查看UpateSql 的值,确定参数都有值
    将UpateSql 的值复制到SQL查询分析器中执行一下 检查语句错误
    int n=com.ExecuteNonQuery();
    判断n是否大于0
      

  3.   

    字段最好不要用中文
    检查你的学号是不是字符文本类型,如果是纯数字,建议用int
    要判断是否修改成功,改一下:
    int AffectRows = com.ExecuteNonQuery();
    if(AffectRows>0)
    //成功
    else
    //失败
      

  4.   

    SqlDataAdapter ada = new SqlDataAdapter("Select * From reader", con);
      DataSet ds = new DataSet();
      ada.Fill(ds);仅仅是更新? 你要这个干嘛?
      

  5.   

    F9设置断点在: string UpateSql = "Update reader SET 电子邮箱='" + TextBox2.Text + "',QQ号码='" + TextBox1.Text + "',备注='" + TextBox3.Text + "' where 学号='" + a + "'";看看你的UpateSql  把语句复制下来 在SQL查询分析器重去执行。如果执行成功证明语句是没有问题的
      

  6.   

    在com.ExecuteNonQuery();里捕获一下异常
      

  7.   

    SqlDataAdapter ada = new SqlDataAdapter("Select * From reader", con);
      DataSet ds = new DataSet();
      ada.Fill(ds);是有什么用的啊。更新之后又查出来所有?
      

  8.   

    把sql语句贴上来。断点调试获取
      

  9.   

    string a = Session["用户名"].ToString();
    是指这个吗?
      

  10.   

    判断之后AffectRows>0,但数据库表里还是没更新
      

  11.   

    你在表名前边加个 from 试试
      

  12.   


    按5楼去做!放一个断点即可 获取到sql语句  你在sql查询分析器里执行一下  就什么都明白了
      

  13.   

    现在还有个问题是,三个用于输入电子邮箱、QQ号码、备注的TextBox在页面打开后会出现很多空格,在这个三个文本框内鼠标点到哪空格就到哪。会不会因为这个所以不能更新数据库的表啊?我是新手,不太懂
      

  14.   

    打个断点,找到UpateSql 值.复制到sql 里跑一下.看成不成?
      

  15.   

    string UpateSql = "Update reader SET 电子邮箱='" + TextBox2.Text.Trim() + "',QQ号码='" + TextBox1.Text.Trim() + "',备注='" + TextBox3.Text.Trim() + "' where 学号='" + a + "'";
      

  16.   


    那就 .trim()一下  去掉空格这个有可能的  比如  数据库字段长度一定   你空格太多   超出长度  无法更新
    你现在只有获取sql语句  然后在查询分析器里  去执行   才知道真正的原因! 
    学会用断点调试程序。
      

  17.   

    lz多看看书,多看看别人的代码,不要一味的拷贝别人的代码,先看明白代码什么意思,然后拷贝那样才事半功倍...up+++
      

  18.   

    结合上面所有的问题,给你总结一下:
    protected void Button3_Click(object sender, EventArgs e)
            {
                int i = 0;
                try
                {
                    string a = Session["用户名"].ToString();
                    SqlConnection con = new SqlConnection("server=.;user id=sa;pwd=;DataBase=book");
                    con.Open();
                    string UpateSql = "Update reader SET 电子邮箱='" + (TextBox2.Text).Trim() + "',QQ号码='" + (TextBox1.Text).Trim() + "',备注='" + (TextBox3.Text).Trim() + "' where 学号='" + a + "'";
                    SqlCommand com = new SqlCommand(UpateSql, con);
                    i= com.ExecuteNonQuery(); //接收一下值,如果大于0 就是更新成功了;                if (i > 0)
                    {
                        Response.Write("<script language=javascript>alert('修改成功!')</script>");
                    }
                    else
                    {
                        Response.Write("<script language=javascript>alert('修改失败!')</script>");
                    }
                   
                }
                catch
                {
                    Response.Write("<script language=javascript>alert('修改失败!')</script>");
                }
            }试试这段代码把。不行就设个断点,一走就明白了。
      

  19.   

    where 学号='" + a + "'";
    很正常,也许没有符合条件的记录给你修改,比如update student set name='test' where id=3,假如没有id=3的记录也可以操作成功,只是受影响的行数为0而已,可以用字段最好不要用中文
    检查你的学号是不是字符文本类型,如果是纯数字,建议用int
    要判断是否修改成功,改一下:
    int AffectRows = com.ExecuteNonQuery();
    if(AffectRows>0)
    //成功
    else
    //失败另外给个建议,字段名最好不要用中文
      

  20.   

    已经判断过com.ExecuteNonQuery()的值是大于0的
      

  21.   

    可以直接用sqlcommand把两个sql语句合并在一个sql语句中,中间加个分号;没问题的!!
      

  22.   

    出现这种情况很正常,提示更新了,就是你的返回值ExcuteNoneQuery都大于0 了,说明SQL语句没问题,为什么没有更新呢?原因是你的字段是中文,有的时候字段是中文会出现这种问题
      

  23.   

    1 你先用个固定的学号试试,看能不能更新,如果行,就说明你之前的学号没获取到,所以没有更新数据库
    2 如果不能更新,在 com.ExecuteNonQuery();这儿设置int i= com.ExecuteNonQuery();再把i的值打印出来,看是否大于0,如果大于0则说明更新成功了
    3 还是不行,检查sql语句,在sql server 2000中,用查询分析器,把你的语句写到里面,值都用固定的,看能不能成功,不能就是sql语句写错了,检测from where等前后是不是有空格(一般很容易忽略的),还有字段是不是都是varchar类型的,有的是数字
    能说的就这么多了、、、还有一点,如果你是点了编辑,到这个页面修改,那么应该是根据id获取的信息显示在textbox里面的,这个时候根据id获取的信息应该写在page_load里面的if(IsPostBack){}里面,不然更新不了、、、
      

  24.   

    固定学号试过了,也是一样的情况,提示成功,但是不更新
    com.ExecuteNonQuery()的值判断过了,是大于0的。
    写的UPDATE语句在查询分析器里试过了可以更新。最后红字的不太明白,能详细说下吗
      

  25.   

    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.SqlClient;public partial class 个人信息 : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
      {
      string strconn = ConfigurationManager.AppSettings["connStr"];
      SqlConnection conn = new SqlConnection(strconn);
      string strsql = "";
      string a = Session["用户名"].ToString();
      conn.Open();
      strsql = "select * from reader where 学号='" + a + "'";
      DataSet ds = new DataSet();
      SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
      da.Fill(ds);
      if (ds.Tables[0].Rows.Count == 0)
      {
      Response.Write("没有记录");
      }
      else
      {
      Label1.Text = ds.Tables[0].Rows[0]["学号"].ToString();
      Label2.Text = ds.Tables[0].Rows[0]["姓名"].ToString();
      Label3.Text = ds.Tables[0].Rows[0]["政治面貌"].ToString();
      Label4.Text = ds.Tables[0].Rows[0]["性别"].ToString();
      Label5.Text = ds.Tables[0].Rows[0]["专业班级"].ToString();
      Label6.Text = DateTime.Parse(ds.Tables[0].Rows[0]["入学时间"].ToString()).ToShortDateString();
      Label7.Text = ds.Tables[0].Rows[0]["手机号码"].ToString();
      Label8.Text = ds.Tables[0].Rows[0]["虚拟网"].ToString();
      TextBox1.Text = ds.Tables[0].Rows[0]["QQ"].ToString();
      TextBox2.Text = ds.Tables[0].Rows[0]["dzyx"].ToString();
      TextBox3.Text = ds.Tables[0].Rows[0]["bz"].ToString();
      }
      conn.Close();  }  protected void Button3_Click(object sender, EventArgs e)
      {
      int i = 0;
      try
      {
      string a = Session["用户名"].ToString();
      SqlConnection con = new SqlConnection("server=.;user id=sa;pwd=;DataBase=book");
      con.Open();
      string UpateSql = "Update reader SET dzyx='" + (TextBox2.Text).Trim() + "',QQ='" + (TextBox1.Text).Trim() + "',bz='" + (TextBox3.Text).Trim() + "' where 学号='" + a + "'";
      SqlCommand com = new SqlCommand(UpateSql, con);
      i = com.ExecuteNonQuery(); //接收一下值,如果大于0 就是更新成功了;  if (i > 0)
      {
      Response.Write("<script language=javascript>alert('修改成功!')</script>");
      }
      else
      {
      Response.Write("<script language=javascript>alert('修改失败!')</script>");
      }  }
      catch
      {
      Response.Write("<script language=javascript>alert('修改失败!')</script>");
      }我把这个CS文件的所有代码都贴上来吧。想实现的是登录之后,根据用户名读取该用户的信息,用户可以修改自己的QQ、电子邮箱和备注。
      

  26.   

     protected void Page_Load(object sender, EventArgs e)//页面
        {
            if (!this.Page.IsPostBack)
            {
                this.BindInfoById(id);//调用方法,通过id获取信息到页面
            }
            this.DataBind();
        }
     protected void BtnMod_Click(object sender, EventArgs e)//按钮
        {
            this.DataUpdate(id);//调用方法,修改数据
        }
    我是说你要这样写,
    不然容易造成数据回滚,就是你那样更新成功了,可是数据库里面还是没有更新