Exemplo n.º 1
0
        /// <summary>
        /// Loads the data from XML.
        /// </summary>
        /// <param name="doc">The document.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">doc</exception>
        static ExportedDataModel LoadDataFromXml(XDocument doc)
        {
            if (doc == null)
            {
                throw new ArgumentNullException(nameof(doc));
            }
            ExportedDataModel model = new ExportedDataModel();
            var table = doc.Descendants("table").First();

            model.LoadXml(table);
            return(model);
        }
Exemplo n.º 2
0
        void ExportToXml(ExportModel data, AppOptionInfo opts, EntityMap entityMap, CodeGenRunner codeGenRunner, List <string> files)
        {
            var fileName     = GetFileName(entityMap.Name, null, opts.OutputFile);
            var exportedData = new ExportedDataModel
            {
                EntityName = entityMap.FullName,
                Name       = entityMap.TableName,
                DataRows   = data.DataBag
            };

            var filePath = Path.Combine(codeGenRunner.WorkingFolder, fileName);

            exportedData.Save(filePath);
            files.Add(filePath);
        }
Exemplo n.º 3
0
        void ImportData(MapConfig mapConfig, SqlConnection conn, SqlTransaction transaction, AppOptionInfo opts, string filePath)
        {
            var exportedData = ExportedDataModel.LoadFromFile(filePath);
            var ent          = mapConfig.GetEntityMap(exportedData.EntityName);

            Logger.Log(LogLevel.Debug, $"[{ent.FullName}] - table: {ent.TableName} - rows: {exportedData.DataRows.Count}");
            string destinationTableName = $"[{ent.SchemaName}].[{ent.TableName}]";

            try
            {
                DataTable        table       = ent.CreateTable();
                RazorInterpreter interpreter = new RazorInterpreter();
                int count = 0;

                if (opts.Merge)
                {
                    //create table
                    var tableCreate = interpreter.CompileTemplate(Templates.CreateTempTable, ent);
                    destinationTableName = $"[{ent.SchemaName}].[#{ent.TableName}]";

                    ExecuteCommand(tableCreate, conn, transaction);
                }

                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, transaction))
                {
                    foreach (DataColumn tableColumn in table.Columns)
                    {
                        sqlBulkCopy.ColumnMappings.Add(tableColumn.ColumnName, tableColumn.ColumnName);
                    }
                    foreach (var exportedDataDataRow in exportedData.DataRows)
                    {
                        var dataRow = table.NewRow();
                        foreach (var field in exportedDataDataRow)
                        {
                            var prop    = ent.FindPropertyByColumnName(field.Key);
                            var clrType = SqlTypeHelper.GetClrType(prop.DbType, prop.IsNullable);
                            var column  = table.Columns[field.Key];
                            if (field.Value == null || string.IsNullOrWhiteSpace(field.Value.ToString()))
                            {
                                dataRow[field.Key] = DBNull.Value;
                            }
                            else
                            {
                                var convertMethod = converter.GetConverterFactoryMethod(clrType);
                                var value         = convertMethod(field.Value.ToString().Trim());
                                dataRow[field.Key] = value ?? DBNull.Value;
                            }
                        }

                        count++;
                        Logger.Log(LogLevel.Debug, "Adding row " + count);
                        table.Rows.Add(dataRow);
                        dataRow.AcceptChanges();
                    }

                    sqlBulkCopy.DestinationTableName = destinationTableName;
                    Logger.Log(LogLevel.Debug, $"Data table [{sqlBulkCopy.DestinationTableName}] loaded. Will now write to the database.");
                    sqlBulkCopy.WriteToServer(table);

                    if (opts.Merge)
                    {
                        var mergeScript = interpreter.CompileTemplate(Templates.Merge, ent);
                        ExecuteCommand(mergeScript, conn, transaction);
                        ExecuteCommand($"DROP TABLE {destinationTableName}", conn, transaction);
                    }

                    table.Clear();
                }
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Error, $"Error trying to import data to {ent.TableName}...");
                throw;
            }
        }