Esempio n. 1
0
        public DataFile Export(string database)
        {
            if (!context.DatabaseExists(database))
            {
                throw new InvalidOperationException("database does not exist");
            }

            string fileName     = Guid.NewGuid() + DataConfig.bakExt;
            string relativePath = Path.Combine(DataConfig.exportDir, fileName);
            string absolutePath = Path.Combine(DataConfig.baseDir, relativePath);

            Backup backup = new Backup();

            backup.Action   = BackupActionType.Database;
            backup.Database = database;
            backup.Devices.AddDevice(absolutePath, DeviceType.File);
            backup.BackupSetName        = fileName;
            backup.BackupSetDescription = database;
            backup.ExpirationDate       = DateTime.Today.AddDays(100);
            backup.Initialize           = false;
            backup.SqlBackup(SQLServerContext.Server);

            DataFile file = new DataFile(Path.Combine(DataConfig.baseDir, relativePath), fileName, database);

            return(file);
        }
Esempio n. 2
0
        public DataFile Export(string database)
        {
            if (!context.DatabaseExists(database))
            {
                throw new InvalidOperationException("database does not exist");
            }

            string folderName         = Guid.NewGuid().ToString();
            string zipName            = folderName + DataConfig.zipExt;
            string absoluteFolderPath = Path.Combine(DataConfig.baseDir, DataConfig.exportDir, folderName);
            string absoluteZipPath    = Path.Combine(DataConfig.baseDir, DataConfig.exportDir, zipName);

            Directory.CreateDirectory(absoluteFolderPath);
            TableCollection tables = context.Tables;

            for (int i = 0; i < tables.Count; i++)
            {
                string tableName = tables[i].Name;

                StreamWriter writer      = new StreamWriter(Path.Combine(absoluteFolderPath, tableName + DataConfig.csvExt));
                string       selectQuery = SQLQueryGenerator.GetSelectQuery(tableName);
                context.ExecuteReader(selectQuery, (reader) => CSVReadWrite.CreateCsvFile(reader, writer));
            }

            ZipFile.CreateFromDirectory(absoluteFolderPath, absoluteZipPath);
            Directory.Delete(absoluteFolderPath, true);

            DataFile file = new DataFile(Path.Combine(DataConfig.baseDir, DataConfig.exportDir, zipName), zipName, database);

            return(file);
        }
Esempio n. 3
0
        public void Import(DataFile file)
        {
            if (!context.DatabaseExists(file.database))
            {
                context.CreateDatabase(file.database);
            }

            DataSet dataSet = new DataSet(file.database);

            dataSet.ReadXml(file.path);
            DataTableCollection tables = dataSet.Tables;

            for (int i = 0; i < tables.Count; i++)
            {
                DataTable table       = tables[i];
                string[]  columnNames = new string[table.Columns.Count];

                for (int j = 0; j < table.Columns.Count; j++)
                {
                    columnNames[j] = table.Columns[j].ColumnName;
                }

                string query = SQLQueryGenerator.GetCreateTableQuery(table.TableName, columnNames);
                context.ExecuteNonQuery(query);
            }
        }