业务流程-Oracle反写快照数据迁移到sqlserver,解析报错

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-23浏览:2

业务流程-Oracle反写快照数据迁移到sqlserver,解析报错

问题描述:

Oracle反写快照数据迁移到sqlserver,解析报错"XML parsing:line1,character 38,unable to switch the encoding",如下图 。


解决方案:

原因:这是因为oracle数据迁移到sqlserver会加上xml声明,导致的


解决方案:使用插件代码把xml声明给去掉


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.IO.Compression;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.BusinessEntity.BusinessFlow;
using Kingdee.BOS.Orm.Drivers;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
namespace Kingdee.BOS.TestPlugIn22.FormPlugin
{
    [HotUpdate]
    [Description("单据测试插件")]
    public class TestFormPlugIn : AbstractBillPlugIn
    {
       public override void BarItemClick(BarItemClickEventArgs e)
        {
            if (e.BarItemKey.EqualsIgnoreCase("某某升级菜单key"))
            {
                //需要处理的两张表
                List<string> hisTableNames = new List<string>() { "t_BF_InstanceSnapHis", "t_BF_SnapBackUp" };
                foreach (var tbName in hisTableNames)
                {
                    bool isContinue = true;
                    while (isContinue)
                    {
                        List<string> lstId = new List<string>();
                        string sql = string.Format("select top 30 FCID from {0} where FZIPTYPE =1 ", tbName);//一次处理30条
                        using (IDataReader reader = DBUtils.ExecuteReader(this.Context, sql))
                        {
                            while (reader.Read())
                            {
                                var fcid = reader["FCID"] as string;
                                lstId.Add(fcid);
                            }
                        }
                        if (lstId.Count < 30)
                        {
                            isContinue = false;
                        }
                        //得到压缩的数据进行处理
                        IDbDriver driver = new OLEDbDriver(this.Context);
                        HisSnapshotType dtSnap = new HisSnapshotType(tbName);
                        IDataManager dataManager = Orm.DataManagerUtils.GetDataManager(dtSnap, driver);
                        var snapshots = dataManager.Read(lstId).ToArray<DynamicObject>();
                        this.DecompressSnaps(dtSnap, snapshots);  // 解压
                        dataManager.Save(snapshots); // 保存
                    }
                }
            }
        }
 
        /// <summary>
        /// 解压历史快照
        /// </summary>
        /// <param name="dtSnap">历史快照实体模型</param>
        /// <param name="snaps">历史快照数据包</param>
        private void DecompressSnaps(HisSnapshotType dtSnap, IEnumerable<DynamicObject> snapshots)
        {
            // 逐个快照进行解压
            foreach (var snapshot in snapshots)
            {
                string zipType = dtSnap.ZipTypeProperty.GetValue<string>(snapshot);
                string xmlZip = dtSnap.XmlZipProperty.GetValue<string>(snapshot);
                //把压缩的数据还原为xml,并把申请去掉
                string xmlBody = this.ZipDecompress(xmlZip);
                xmlBody = xmlBody.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
                //这里有两种出来方式
                //方式一,把数据还原会xmlbody中
                dtSnap.XmlBodyProperty.SetValue(snapshot, xmlBody); //还原到xmlbody中
                dtSnap.ZipTypeProperty.SetValue(snapshot, "0"); //zip类型为0
                dtSnap.XmlZipProperty.SetValue(snapshot, null);   // 清除压缩xml内容
                ////方式二,压缩正确的xmlbody,仍然放在压缩字段中,
                ////但这种情况前面的取数方式需要改变,目前也没有更好的区分
                //var zimxml = this.ZipCompress(xmlBody);
                //dtSnap.XmlZipProperty.SetValue(snapshot, zimxml); 
            }
        }
 
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="xmlZip"></param>
        /// <returns></returns>
        private string ZipDecompress(string xmlZip)
        {
            byte[] base64Bytes = Convert.FromBase64String(xmlZip);
            byte[] bytes = null;
            using (MemoryStream tempMs = new MemoryStream())
            {
                using (MemoryStream ms = new MemoryStream(base64Bytes))
                {
                    GZipStream Decompress = new GZipStream(ms, CompressionMode.Decompress);
                    Decompress.CopyTo(tempMs);
                    Decompress.Close();
                    bytes = tempMs.ToArray();
                }
            }
            // 还原为字符串      
            string oldStr = Encoding.Default.GetString(bytes);
            return oldStr;
        }
 
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="xmlBody"></param>
        /// <returns></returns>
        private string ZipCompress(string xmlBody)
        {
            byte[] bytes2 = Encoding.Default.GetBytes(xmlBody);
            string base64str2 = Convert.ToBase64String(bytes2);
            byte[] base64Bytes2 = Convert.FromBase64String(base64str2);
            using (MemoryStream ms = new MemoryStream())
            {
                GZipStream Compress = new GZipStream(ms, CompressionMode.Compress);
                Compress.Write(base64Bytes2, 0, base64Bytes2.Length);
                Compress.Close();
                var bb = ms.ToArray();
                string compressStr = Convert.ToBase64String(bb);
                return compressStr;
            }
        }
    }
}



业务流程-Oracle反写快照数据迁移到sqlserver,解析报错

问题描述:Oracle反写快照数据迁移到sqlserver,解析报错"XML parsing:line1,character 38,unable to switch the encoding",如下...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息