예제 #1
0
        private void GenerateWithXmlDocument(IDbExtractor reader, DbConnection connection, string tableName,
                                             string path)
        {
            string filePath    = Path.Combine(path, $"{tableName}.xml");
            var    columns     = reader.GetColumns(connection, tableName);
            var    columnNames = columns.Select(x => x.ColumnName).ToArray();

            XDocument document = new XDocument();
            XElement  root     = new XElement($"{tableName}");

            document.Add(root);

            using var dataReader = reader.ReadData(connection, tableName, columnNames);
            while (dataReader.Read())
            {
                XElement entity = new XElement("entity");
                root.Add(entity);
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    entity.Add(new XAttribute(dataReader.GetName(i), dataReader.GetValue(i)));
                }
            }

            document.Save(filePath);
        }
예제 #2
0
        public void Generate(IDbExtractor reader, DbConnection connection, string tableName, string path)
        {
            string filePath = Path.Combine(path, $"{tableName}.csv");

            using var fileStream = File.Create(filePath);
            using var fileWriter = new StreamWriter(fileStream);
            var columns     = reader.GetColumns(connection, tableName);
            var columnNames = columns.Select(x => x.ColumnName).ToArray();

            fileWriter.WriteLine(String.Join(",", columnNames));

            using var dataReader = reader.ReadData(connection, tableName, columnNames);
            while (dataReader.Read())
            {
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    fileWriter.Write(dataReader.GetValue(i));
                    if (i < dataReader.FieldCount - 1)
                    {
                        fileWriter.Write(",");
                    }
                }

                fileWriter.WriteLine();
            }
        }
예제 #3
0
        private void GenerateWithXmlWriter(IDbExtractor reader, DbConnection connection, string tableName, string path)
        {
            string filePath    = Path.Combine(path, $"{tableName}.xml");
            var    columns     = reader.GetColumns(connection, tableName);
            var    columnNames = columns.Select(x => x.ColumnName).ToArray();

            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent  = true;
            using var writer = XmlWriter.Create(filePath, settings);

            writer.WriteStartElement(tableName);
            using var dataReader = reader.ReadData(connection, tableName, columnNames);
            while (dataReader.Read())
            {
                writer.WriteStartElement("Entity");
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    writer.WriteElementString(dataReader.GetName(i), dataReader.GetValue(i).ToString());
                }

                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }
        public void Generate(IDbExtractor reader, DbConnection connection, string tableName, string path)
        {
            string filePath = Path.Combine(path, $"{tableName}.html");

            using var fileStream = File.Create(filePath);
            using var fileWriter = new StreamWriter(fileStream);

            var columns     = reader.GetColumns(connection, tableName);
            var columnNames = columns.Select(x => x.ColumnName).ToArray();

            fileWriter.WriteLine("<!DOCTYPE html>");
            fileWriter.WriteLine("<html>");
            fileWriter.WriteLine("<head>");
            fileWriter.WriteLine(1, "<style>");
            fileWriter.WriteLine(2, "table {");
            fileWriter.WriteLine(3, "font-family: arial, sans-serif; border-collapse: collapse; width: 100%; }");
            fileWriter.WriteLine(2, "td, th {");
            fileWriter.WriteLine(3, "border: 1px solid #dddddd;text-align: left; padding: 8px; }");
            fileWriter.WriteLine(2, "tr:nth-child(even) {");
            fileWriter.WriteLine(3, "background-color: #dddddd;}");
            fileWriter.WriteLine(1, "</style>");
            fileWriter.WriteLine("</head>");
            fileWriter.WriteLine("<body>");
            fileWriter.WriteLine(1, "<table style='width:100%'>");
            fileWriter.WriteLine(1, "<h2>Table ", tableName, "</h2>");

            fileWriter.WriteLine(2, "<tr>");
            foreach (var column in columns)
            {
                fileWriter.WriteLine(3, "<th>", column.ColumnName, "</th>");
            }

            fileWriter.WriteLine(2, "</tr>");

            using var readData = reader.ReadData(connection, tableName, columnNames);
            while (readData.Read())
            {
                fileWriter.WriteLine(2, "<tr>");
                for (int i = 0; i < readData.FieldCount; i++)
                {
                    fileWriter.WriteLine(3, "<td>", readData.GetValue(i).ToString(), "</td>");
                }

                fileWriter.WriteLine(2, "</tr>");
            }

            fileWriter.WriteLine(2, "</table>");
            fileWriter.WriteLine("</body>");
            fileWriter.WriteLine("</html>");
        }
예제 #5
0
        public void Generate(IDbExtractor reader, DbConnection connection, string tableName, string path)
        {
            string pascalCaseName = ToPascalCase(tableName);
            string fileName       = pluralizer.Singularize(pascalCaseName);
            string filePath       = Path.Combine(path, $"{fileName}.cs");

            using var fileStream = File.Create(filePath);
            using var fileWriter = new StreamWriter(fileStream);

            var columns = reader.GetColumns(connection, tableName);

            fileWriter.WriteLine("using System.ComponentModel.DataAnnotations;");
            fileWriter.WriteLine("using System.ComponentModel.DataAnnotations.Schema;");
            fileWriter.WriteLine();
            fileWriter.WriteLine("namespace Entities");
            fileWriter.WriteLine("{");
            fileWriter.WriteLine(1, $"[Table(\"{tableName}\")]");
            fileWriter.WriteLine(1, $"public class {fileName}");
            fileWriter.WriteLine(1, "{");
            foreach (var columnInfo in columns)
            {
                var propName = ToPascalCase(columnInfo.ColumnName);
                var type     = TypeNameOrAlias(columnInfo.ColumnType);
                if (columnInfo.IsPrimaryKey)
                {
                    fileWriter.WriteLine(2, "[Key]");
                }

                fileWriter.WriteLine(2, $"[Column(\"{propName}\")]");
                if (columnInfo.IsNullable && columnInfo.ColumnType != typeof(string))
                {
                    type += "?";
                }

                fileWriter.WriteLine(2, "public ", type, " ", propName,
                                     " { get; set; }");

                var last = columns.Last();
                if (!columnInfo.Equals(last))
                {
                    fileWriter.WriteLine();
                }
            }

            fileWriter.WriteLine(1, "}");
            fileWriter.WriteLine("}");
        }