コード例 #1
0
        /// <summary>批量插入</summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public Int32 BatchInsert(DbTable dt)
        {
            var tableName = Sql.Substring(" ")?.Trim(';');
            var dal       = DAL.Create(ConnName);

            // 执行反向工程,该建表就建表
            //dal.CheckDatabase();

            var table = dal.Tables?.FirstOrDefault(e => e.TableName.EqualIgnoreCase(tableName));

            //if (table == null) throw new Exception($"在连接[{ConnName}]中无法找到数据表[{tableName}]");
            if (table == null)
            {
                var ioc = ObjectContainer.Current;
                table           = ioc.Resolve <IDataTable>();
                table.TableName = tableName;

                for (var i = 0; i < dt.Columns.Length; i++)
                {
                    var dc = table.CreateColumn();
                    dc.ColumnName = dt.Columns[i];
                    dc.DataType   = dt.Types[i];

                    table.Columns.Add(dc);
                }

                dal.SetTables(table);
            }

            // 选取目标表和数据集共有的字段
            tableName = dal.Db.FormatTableName(tableName);
            var columns = new List <IDataColumn>();

            foreach (var dc in table.Columns)
            {
                if (dc.ColumnName.EqualIgnoreCase(dt.Columns))
                {
                    columns.Add(dc);
                }
            }

            return(dal.Session.Insert(tableName, columns.ToArray(), dt.Cast <IIndexAccessor>()));
        }