XCode新增数据转换功能(导数据)

用法:
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
DAL.AddConnStr( "xxgk" , "Data Source=192.168.1.21;Initial Catalog=信息公开;user id=sa;password=Pass@word" , null , "mssql" );
var dal = DAL.Create( "xxgk" );
 
DAL.AddConnStr( "xxgk2" , "Data Source=XXGK.db;Version=3;" , null , "sqlite" );
File.Delete( "XXGK.db" );
 
//DAL.ShowSQL = false;
 
var etf = new EntityTransform();
etf.SrcConn = "xxgk" ;
etf.DesConn = "xxgk2" ;
etf.AllowInsertIdentity = true ;
etf.TableNames.Remove( "PubInfoLog" );
//etf.OnTransformTable += (s, e) => { if (e.Arg.Name == "")e.Arg = null; };
var rs = etf.Transform();
Console.WriteLine( "共转移:{0}" , rs);


其实你也可以自己实现,XCode内部代码如下:
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using NewLife;
using NewLife.Collections;
using NewLife.Log;
#if !NET4
using NewLife.Reflection;
#endif
using XCode.DataAccessLayer;
 
namespace XCode.Transform
{
    /// <summary>实体转换</summary>
    public class EntityTransform
    {
        #region 属性
        private String _SrcConn;
        /// <summary>源</summary>
        public String SrcConn { get { return _SrcConn; } set { _SrcConn = value; } }
 
        private String _DesConn;
        /// <summary>目的</summary>
        public String DesConn { get { return _DesConn; } set { _DesConn = value; } }
 
        private ICollection<String> _TableNames;
        /// <summary>要导数据的表,为空表示全部</summary>
        public ICollection<String> TableNames
        {
            get
            {
                if (_TableNames == null )
                {
                    _TableNames = new HashSet<String>(StringComparer.OrdinalIgnoreCase);
                    if (!String.IsNullOrEmpty(SrcConn))
                    {
                        foreach (var item in DAL.Create(SrcConn).Tables)
                        {
                            _TableNames.Add(item.Name);
                        }
                    }
                }
                return _TableNames;
            }
            set { _TableNames = value; }
        }
 
        private Int32 _BatchSize = 1000;
        /// <summary>每批处理多少行数据,默认1000</summary>
        public Int32 BatchSize { get { return _BatchSize; } set { _BatchSize = value; } }
 
        private Boolean _AllowInsertIdentity;
        /// <summary>是否允许插入自增列</summary>
        public Boolean AllowInsertIdentity { get { return _AllowInsertIdentity; } set { _AllowInsertIdentity = value; } }
        #endregion
 
        #region 方法
        /// <summary>把一个链接的数据全部导入到另一个链接</summary>
        /// <returns></returns>
        public Int32 Transform()
        {
            var dal = DAL.Create(SrcConn);
 
            var tables = dal.Tables;
            tables.RemoveAll(t => t.IsView);
            var tns = TableNames;
            if (tns != null && tns.Count > 0) tables.RemoveAll(t => !tns.Contains(t.Name) && !tns.Contains(t.Alias));
 
            var total = 0;
            foreach (var item in tables)
            {
                if (OnTransformTable != null )
                {
                    var e = new EventArgs<IDataTable>(item);
                    OnTransformTable( this , e);
                    if (e.Arg == null ) continue ;
                }
 
                total += TransformTable(dal.CreateOperate(item.Name));
            }
 
            return total;
        }
 
        /// <summary>把一个表的数据全部导入到另一个表</summary>
        /// <param name="eop">实体操作者。</param>
        /// <param name="getData">用于获取数据的委托</param>
        /// <returns></returns>
        public Int32 TransformTable(IEntityOperate eop, Func<Int32, Int32, IEntityList> getData = null )
        {
            var name = eop.TableName;
            var count = eop.Count;
            if (getData == null ) getData = (start, max) => eop.FindAll( null , null , null , start, max);
 
            // 在目标链接上启用事务保护
            eop.ConnName = DesConn;
            eop.BeginTransaction();
            try
            {
                XTrace.WriteLine( "{0} 共 {1}" , name, count);
 
                // 允许插入自增
                var oldII = eop.AllowInsertIdentity;
                if (AllowInsertIdentity) eop.AllowInsertIdentity = true ;
                // 关闭SQL日志
                var oldShowSql = DAL.ShowSQL;
                DAL.ShowSQL = false ;
 
                var total = 0;
                var index = 0;
                while ( true )
                {
                    eop.ConnName = SrcConn;
                    var list = getData(index, BatchSize);
                    if (list == null || list.Count < 1) break ;
                    index += list.Count;
 
                    // 处理事件,外部可以修改实体数据
                    if (OnTransformEntity != null )
                    {
                        var e = new EventArgs<IEntity>( null );
                        foreach (var entity in list)
                        {
                            e.Arg = entity;
                            OnTransformEntity( this , e);
                        }
                    }
 
                    eop.ConnName = DesConn;
                    var rs = list.Insert( true );
                    XTrace.WriteLine( "{0} 导入 {1}/{2} {3:p}" , name, index, count, (Double)index / count);
 
                    total += rs;
                }
                DAL.ShowSQL = oldShowSql;
                // 关闭插入自增
                if (AllowInsertIdentity) eop.AllowInsertIdentity = oldII;
 
                // 在目标链接上启用事务保护
                eop.ConnName = DesConn;
                eop.Commit();
 
                return total;
            }
            catch (Exception ex)
            {
                XTrace.WriteLine( "{0} 错误 {1}" , name, ex.Message);
                // 在目标链接上启用事务保护
                eop.ConnName = DesConn;
                eop.Rollback();
                throw ;
            }
        }
        #endregion
 
        #region 事件
        /// <summary>转换表时触发。如果参数被置空,表示不转换该表</summary>
        public event EventHandler<EventArgs<IDataTable>> OnTransformTable;
 
        / <summary>转换实体时触发</summary>
        //public event EventHandler<EventArgs<IEntity>> OnTransformEntity;
 
        /// <summary>转换实体时触发</summary>
        public event EventHandler<EventArgs<IEntity>> OnTransformEntity;
        #endregion
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Xcode是一款由苹果公司开发的集成开发环境(IDE),是开发iOS、iPadOS、macOS和watchOS应用程序的主要工具之一。其中的navigate功能Xcode的一个重要功能之一。 Navigate可以帮助开发者在Xcode中更方便地航和编辑代码。具体来说,Navigate有以下几个主要的功能: 1. 查找和跳转:Navigate可以帮助开发者在项目中快速查找和跳转到所需的文件、类、方法、变量等。通过使用快捷键或者鼠标点击,可以在代码中自由跳转,提高了开发效率。 2. 代码结构浏览:Navigate可以帮助开发者快速了解代码的结构和层次关系。通过查看项目航器、代码航器等窗口,可以方便地查看类的继承关系、方法的调用关系等。 3. 文件和符号列表:Navigate可以帮助开发者查看当前文件中的方法、属性、变量等,也可以查看整个项目中的文件和符号列表。通过使用快捷键或者菜单命令,可以方便地在列表中进行搜索和定位。 4. 自动完成和修正:Navigate可以帮助开发者快速输入代码并进行修正。通过自动完成功能,可以快速输入代码片段、方法名、变量名等。同时,Navigate还可以自动修正代码中的语法错误和拼写错误。 总之,Navigate是Xcode中一个非常实用的功能,可以帮助开发者更方便地编辑和查看代码。无论是对于新手还是老手来说,都是一个非常重要的工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值