public void Import(DataFile file) { string database = file.database; if (!context.DatabaseExists(database)) { context.CreateDatabase(database); context.ChangeDatabase(database); } DataTable csvData = CSVReadWrite.ReadCsvDataTable(file.path); context.ExecuteBulkCopy((bulkCopy) => CreateDatabaseTable(file, csvData, bulkCopy)); }
private List <DataFile> ExtractZip(DataFile zip) { List <DataFile> dataFiles = new List <DataFile>(); ZipArchive archive = ZipFile.OpenRead(zip.path); ReadOnlyCollection <ZipArchiveEntry> entries = archive.Entries; string directory = zip.path.Replace(DataConfig.zipExt, string.Empty); archive.ExtractToDirectory(directory); for (int i = 0; i < entries.Count; i++) { ZipArchiveEntry entry = entries[i]; dataFiles.Add(new DataFile(Path.Combine(directory, entry.FullName), entry.Name, zip.database)); } return(dataFiles); }
private void CreateDatabaseTable(DataFile file, DataTable data, SqlBulkCopy bulkCopy) { DataColumnCollection columns = data.Columns; string[] columnNames = new string[columns.Count]; for (int i = 0; i < columns.Count; i++) { string columnName = columnNames[i] = columns[i].ColumnName; bulkCopy.ColumnMappings.Add(columnName, columnName); } string createTableQuery = SQLQueryGenerator.GetCreateTableQuery(file.fileName, columnNames); context.ExecuteNonQuery(createTableQuery); context.Refresh(); bulkCopy.DestinationTableName = SQLQueryGenerator.EncloseInBrackets(file.fileName); bulkCopy.WriteToServer(data); }
public void Import(DataFile file) { if (context.DatabaseExists(file.database)) { throw new InvalidOperationException("database already exists"); } Restore restore = new Restore(); BackupDeviceItem backup = new BackupDeviceItem(file.path, DeviceType.File); restore.Database = file.database; restore.Devices.Add(backup); RelocateFile relocateDataFile = new RelocateFile(LookupLogicalName(restore, "D"), string.Format(savePath, file.database, DataConfig.mdfExt)); RelocateFile relocateLogFile = new RelocateFile(LookupLogicalName(restore, "L"), string.Format(savePath, file.database, DataConfig.ldfExt)); restore.RelocateFiles.Add(relocateDataFile); restore.RelocateFiles.Add(relocateLogFile); restore.Action = RestoreActionType.Database; restore.ReplaceDatabase = false; restore.SqlRestore(SQLServerContext.Server); context.ChangeDatabase(file.database); }