• 使用OleDbParameter来写Access的更新没反应的解决办法


      动软代码生成器生成的DAL,一开始生成的是MsSQL的。后来有需求,要转换成了Access。动软对Access生成支持不好,我手动替换了一次。然后把数据库从SQL导进Access。咋一看都没问题。
      结果到了产品更新的部分,提示我Product_ID不能更新:


      public void Update(WoooXVi.EPCHR.Model.Product model)
      {
       StringBuilder strSql=new StringBuilder();
       strSql.Append("update Product set ");
       strSql.Append("Product_ID=@Product_ID,");
       strSql.Append("Product_Title=@Product_Title,");
       strSql.Append("Product_Class=@Product_Class,");
       strSql.Append("Product_CreateDate=@Product_CreateDate,");
       strSql.Append("Product_CreateAdmin=@Product_CreateAdmin,");
       strSql.Append("Product_Hit=@Product_Hit,");
       strSql.Append("Product_Content=@Product_Content,");
       strSql.Append("Product_Top=@Product_Top,");
       strSql.Append("Product_SImg=@Product_SImg,");
       strSql.Append("Product_BImg=@Product_BImg,");
       strSql.Append("Product_UpdateDate=@Product_UpdateDate,");
       strSql.Append("Product_UpdateAdmin=@Product_UpdateAdmin,");
       strSql.Append("Product_PVContent=@Product_PVContent");
       strSql.Append(" where Product_ID=@Product_ID ");
       OleDbParameter[] parameters = {
         new OleDbParameter("@Product_ID", OleDbType.Integer,4),
         new OleDbParameter("@Product_Title", OleDbType.VarChar,50),
         new OleDbParameter("@Product_Class", OleDbType.Integer,4),
         new OleDbParameter("@Product_CreateDate", OleDbType.Date),
         new OleDbParameter("@Product_CreateAdmin", OleDbType.VarChar,50),
         new OleDbParameter("@Product_Hit", OleDbType.Integer,4),
         new OleDbParameter("@Product_Content", OleDbType.VarChar,0),
         new OleDbParameter("@Product_Top", OleDbType.Integer,4),
         new OleDbParameter("@Product_SImg", OleDbType.VarChar,200),
         new OleDbParameter("@Product_BImg", OleDbType.VarChar,200),
         new OleDbParameter("@Product_UpdateDate", OleDbType.Date),
         new OleDbParameter("@Product_UpdateAdmin", OleDbType.VarChar,50),
         new OleDbParameter("@Product_PVContent", OleDbType.VarChar,50)};
       parameters[0].Value = model.Product_ID;
       parameters[1].Value = model.Product_Title;
       parameters[2].Value = model.Product_Class;
       parameters[3].Value = model.Product_CreateDate;
       parameters[4].Value = model.Product_CreateAdmin;
       parameters[5].Value = model.Product_Hit;
       parameters[6].Value = model.Product_Content;
       parameters[7].Value = model.Product_Top;
       parameters[8].Value = model.Product_SImg;
       parameters[9].Value = model.Product_BImg;
       parameters[10].Value = model.Product_UpdateDate;
       parameters[11].Value = model.Product_UpdateAdmin;
       parameters[12].Value = model.Product_PVContent;

       DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
      } 

    ——原来是SQL里面我Product_ID没设置自增长,Access设置了自增长。把更新Product_ID的语句去掉。(红色加粗字体部分)

    运行……结果错误是没提示了,但是却没有效果——更新之后一刷新,又还原了!数据库里面根本没改动。

    后来查资料才知道。是微软的一个BUG。在更新Access数据库时,如果使用OleDbParameter。那么OleDbParameter参数的的定义顺序必须和查询语句中参数出现的顺序一致!
    就是说我删掉了本来更新Product_ID的语句,那么Product_ID在查询语句中出现的位置从一开始的第一个出现(Update Product_ID = @Product_ID)变成了最后一个出现(Where Product_ID = @Product_ID)。
    所以,只要把参数定义的顺序换过来就行了。以下代码执行成功:


            public void Update(WoooXVi.YKYSRH.Model.Product model)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update Product set ");
                strSql.Append("Product_Title=@Product_Title,");
                strSql.Append("Product_Class=@Product_Class,");
                strSql.Append("Product_CreateDate=@Product_CreateDate,");
                strSql.Append("Product_CreateAdmin=@Product_CreateAdmin,");
                strSql.Append("Product_Hit=@Product_Hit,");
                strSql.Append("Product_Content=@Product_Content,");
                strSql.Append("Product_Top=@Product_Top,");
                strSql.Append("Product_SImg=@Product_SImg,");
                strSql.Append("Product_BImg=@Product_BImg,");
                strSql.Append("Product_UpdateDate=@Product_UpdateDate,");
                strSql.Append("Product_UpdateAdmin=@Product_UpdateAdmin,");
                strSql.Append("Product_PVContent=@Product_PVContent");
                strSql.Append(" where Product_ID=@Product_ID ");
                OleDbParameter[] parameters = {
         new OleDbParameter("@Product_Title", OleDbType.VarChar,50),
         new OleDbParameter("@Product_Class", OleDbType.Integer,4),
         new OleDbParameter("@Product_CreateDate", OleDbType.Date),
         new OleDbParameter("@Product_CreateAdmin", OleDbType.VarChar,50),
         new OleDbParameter("@Product_Hit", OleDbType.Integer,4),
         new OleDbParameter("@Product_Content", OleDbType.VarChar,0),
         new OleDbParameter("@Product_Top", OleDbType.Integer,4),
         new OleDbParameter("@Product_SImg", OleDbType.VarChar,200),
         new OleDbParameter("@Product_BImg", OleDbType.VarChar,200),
         new OleDbParameter("@Product_UpdateDate", OleDbType.Date),
         new OleDbParameter("@Product_UpdateAdmin", OleDbType.VarChar,50),
         new OleDbParameter("@Product_PVContent", OleDbType.VarChar,50),
         new OleDbParameter("@Product_ID", OleDbType.Integer,4)};
                parameters[0].Value = model.Product_Title;
                parameters[1].Value = model.Product_Class;
                parameters[2].Value = model.Product_CreateDate;
                parameters[3].Value = model.Product_CreateAdmin;
                parameters[4].Value = model.Product_Hit;
                parameters[5].Value = model.Product_Content;
                parameters[6].Value = model.Product_Top;
                parameters[7].Value = model.Product_SImg;
                parameters[8].Value = model.Product_BImg;
                parameters[9].Value = model.Product_UpdateDate;
                parameters[10].Value = model.Product_UpdateAdmin;
                parameters[11].Value = model.Product_PVContent;
                parameters[12].Value = model.Product_ID;

                DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            }

  • 相关阅读:
    TypesScript+Webpack
    TypeScript 类型
    git操作
    kafka
    java: cannot find symbol symbol: variable log
    Angular结构型指令,模块和样式
    Angular 自定义拖拽指令
    Angular changeDetction
    Angular 依赖注入
    RXJS Observable的冷,热和Subject
  • 原文地址:https://www.cnblogs.com/sshh/p/1612430.html
Copyright © 2020-2023  润新知