cmd.CommandText = "update 学生 SET 中文姓名=@chname where id =@urlid";
        int urlid = Convert.ToInt32(lblid.Text);
        cmd.Parameters.AddWithValue("@urlid", urlid);            cmd.Parameters.AddWithValue("@chname", txbchname.Text);       
我在一个网页中用上面的代码试图对数据库更新。更新的数据从文本框txbchname中输入,但没办法实现对数据库的更新,网页也不报任何错误。
我把代码改成下面这样就可以了 cmd.CommandText = "update 学生 SET 中文姓名='haha' where id =@urlid";
        int urlid = Convert.ToInt32(lblid.Text);
        cmd.Parameters.AddWithValue("@urlid", urlid);    
但这样我不能插入要更新的参数啊,求助

解决方案 »

  1.   

    代码写反了,你用的一定是Access数据库。注意变量和赋值的顺序要完全一致。改为。
    cmd.CommandText = "update 学生 SET 中文姓名=@chname where id =@urlid";
            int urlid = Convert.ToInt32(lblid.Text);
            cmd.Parameters.AddWithValue("@chname", txbchname.Text);
            cmd.Parameters.AddWithValue("@urlid", urlid);    
      

  2.   

    Page_Load里要有!IsPostBack()
    否则不会更新 
      

  3.   

    加断点,看看有没有执行,如果执行而没有更新的话 就是SQL语句的问题
      

  4.   

    if(Page.IsPostBack) return;
      

  5.   

      string connstr = "";
            SqlConnection conn = new SqlConnection(connstr);
            SqlCommand cmd = new SqlCommand("", conn);
            cmd.CommandType = CommandType.StoredProcedure;
             cmd.Parameters.AddWithValue("@username", Txtname.Text);         conn.Open();
            cmd.ExecuteNonQuery(); using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "select *  from admin where id=@id";
                    cmd.Parameters.AddWithValue("@id", TextBox1.Text);
                    SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (dr.HasRows)
                    {
                      
                    }      
                }
      

  6.   

    AddWithValue主要用于存储过程
    用new SqlParameter
      

  7.   

    我用的是SQL数据库,按照你的方法,我把cmd.Parameters.AddWithValue("@urlid", urlid);   放在后面了还是不行,我更新的不是一个文本框,是很多文本框。但就不是带参数的时候不行,查看数据库,数据根本就没更新。
    但如果不用参数,比如cmd.CommandText = "update 学生 SET 中文姓名='haha' where id =@urlid";
    就没问题。
      

  8.   

    这位大哥,我如果写成这样对不对啊        cmd.CommandText = "update 学生 SET 中文姓名=@chname where id =@urlid";
            int urlid = Convert.ToInt32(lblid.Text);
            SqlParameter parurlid = new SqlParameter();
            parurlid.ParameterName = "@urlid";
            parurlid.Value = urlid;
            cmd.Parameters.Add(parurlid);
            SqlParameter parchname = new SqlParameter("@chname", txbchname.Text);
            cmd.Parameters.Add(parchname);
            cmd.ExecuteNonQuery();
    但我写成这样还是不行啊。
    请指教。谢谢。
      

  9.   

    你在加载页面时判断一下是不是首次加载!IsPostBack() 
    还有就是用 sqlparameter[] param = new sqlparameter[]
    {
            new sqlparameter("@chname",txbchname.Text),
            new sqlparameter("=@urlid",urlid)
    };
    然后调用执行方法就可以了
      

  10.   


    我现在的问题是对数据库就更新不了.
    另外我在PAGE_LOAD()里对数据重新执行了查询。应该不会有问题的。我现在就是不能UPDATE。其他的INSERT.SELECT什么的都可以。
    不理解啊
    如果我的UPDATE语句不带参数也没问题,比如比如cmd.CommandText = "update 学生 SET 中文姓名='haha' where id =@urlid"; 
    一带参数就不能更新了,头大
    哪位朋友指点小弟一下
      

  11.   

    1,你的参数值是不是还是原来的值呀?我是说别把"1"还更新为"1",看上去没更新实际上已经更新了
    2,断点把参数的值取出来,直接运行SQL语句调试(其实不用调试肯定能更新的)declare @chname nvarchar(50)
    declare @urlid int
    set @chname = '写值'
    set @urlid = 100--改成自己的值
    update 学生 SET 中文姓名=@chname where id =@urlid
    --执行这个SQL语句看什么结果.3,故意写错SQL语法,看会不会报错,确认你有程序有调用到该SQL...
      

  12.   


    这位大哥。
    第一个问题我没写错;
    第二个你给的代码我直接在SQL数据调试了,能够更新。OK的。
    第三个,我写错SQL语法。网页报错了但我现在通过网页执行,就不能在数据库中更新了,我查看数据库了。这个是什么问题啊
      

  13.   

    断点一下...一步一步跟踪,直到它执行完了SQL,再到数据库里查看结果,
    网页执行极有可能跳过了某些CASE最后没有执行SQL...
      

  14.   

    尤其注意if那些条件判断,确认它们都能命中...确认能执行SQL...
      

  15.   

    我晕,我把程序改成这样也可以执行
    cmd.CommandText = "update 学生 SET 中文姓名=@chname where id '78'";
      

  16.   

    cmd.CommandText = "update 学生 SET 中文姓名=@chname where id ='78'"; 
      

  17.   

    找到问题了。按照前面几位大哥说的,我设了断点。发现txbchname.Text的值根本不是取我改过的值,还是以前的值,这是为什么啊?