用sqldatasource控件对数据库进行操作,将maxjhbh字段数据读取后修改,重新存入数据库。现在读取没有问题,但是修改后无法保存数据库,代码如下:
            DataView dv_custom = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);//读取数据库
            string str_maxjhbh = dv_custom[0]["maxjhbh"].ToString();//一下代码对数据进行修改
            if (DateTime.Today.ToString("yyyyMMdd") != str_maxjhbh.Substring(1, 8))
            {
                str_maxjhbh = "J" + DateTime.Today.ToString("yyyyMMdd") + "001";
            }
            else
            {
                string str_shl = (Convert.ToInt32(str_maxjhbh.Substring(9, 3)) + 1).ToString("000");
                str_maxjhbh = str_maxjhbh.Substring(0, 9) + str_shl;
            }
            SqlDataSource2.UpdateParameters.Add("maxjhbh",str_maxjhbh);//保存数据
            SqlDataSource2.Update();
运行调试时,提示“您已指定 更新 命令比较 SqlDataSource“SqlDataSource2”的所有值,但为 oldValues 传入的字典是空的。请为 更新 传入有效的字典或将模式更改为 OverwriteChanges。”
SqlDataSource2.Update()这句无法通过,请问怎样解决?是参数的问题吧?

解决方案 »

  1.   

    使用参数化sql进行更新
    update tb set maxjhbh=@maxjhbhsqlParameters
      

  2.   

    DataView dv_custom = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
    string str_maxjhbh = dv_custom[0]["maxjhbh"].ToString();
    string str_id = dv_custom[0]["id"].ToString();
    if (DateTime.Today.ToString("yyyyMMdd") != str_maxjhbh.Substring(1, 8))
    {
        str_maxjhbh = "J" + DateTime.Today.ToString("yyyyMMdd") + "001";
    }
    else
    {
        string str_shl = (Convert.ToInt32(str_maxjhbh.Substring(9, 3)) + 1).ToString("000");
        str_maxjhbh = str_maxjhbh.Substring(0, 9) + str_shl;
    }
    SqlDataSource2.UpdateCommand = "UPDATE [maxid] SET [maxjhbh] = @maxjhbh WHERE [id] = @id";
    SqlDataSource2.UpdateParameters.Add("maxjhbh",str_maxjhbh);
    SqlDataSource2.UpdateParameters.Add("id", str_id);
    SqlDataSource2.Update();
    写成以上代码还是出错,提示和原来的一样
      

  3.   

    直接设置不行吗?
    SqlDataSource2.UpdateParameters["maxjhbh"].DefaultValue=str_maxjhbh;
      

  4.   

    也不行,还是提示“您已指定 更新 命令比较 SqlDataSource“SqlDataSource2”的所有值,但为 oldValues 传入的字典是空的。请为 更新 传入有效的字典或将模式更改为 OverwriteChanges。”
      

  5.   

    哦!这是一个根本的错误!
    用数据源控件和拼sql字符串是有区别的,有些限制的,即:
    where 后的字段名不可以是变量!!
    SqlDataSource,ObjctDataSource等的数据源控件都是不行的!!
      

  6.   

    哦!这是一个根本的错误!
    用数据源控件和拼sql字符串是有区别的,有些限制的,即:
    where 后的字段名不可以是变量!! 包括 set 后的字段名也不可以是变量!!
    SqlDataSource,ObjctDataSource等的数据源控件都是不行的!!
      

  7.   

    只能不用数据源控件!用传统的拼sql字符串!!