public static int CreateDatabase(BaseConfigInfo bci, out Exception resultException) { int result = 0; //数据库新建失败 resultException = null; DatabaseInfo dbi = GetDatabaseInfo(bci); string dbFile = ""; if (dbi.DBFile != null && dbi.DBFile != "") { dbFile = dbi.DBFile.Replace("{$App}", AppDomain.CurrentDomain.BaseDirectory); dbFile.Replace('\\', Path.DirectorySeparatorChar); } try { switch (bci.DBType) { case "SqlServer": string masterstring = string.Format(@"Server={0};Database={1};User={2};Password={3};", dbi.Server, "master", dbi.User, dbi.Password); string sql = string.Format(@"IF NOT EXISTS (SELECT * FROM SYSDATABASES WHERE NAME=N'{0}') CREATE DATABASE {0}", dbi.Database); IDbDriver driver = new SqlDbDriver(); using (IConnection conn = driver.CreateConnection(masterstring)) { SqlStatement st0 = new SqlStatement(string.Format("SELECT count(*) FROM SYSDATABASES WHERE NAME=N'{0}'", dbi.Database)); int count = (int)conn.QueryScalar(st0); if (count == 0) { SqlStatement st = new SqlStatement(sql); driver.FormatSQL(st); driver.FormatSQL(st0); conn.Update(st); if ((int)conn.QueryScalar(st0) > 0) { result = 1; //代表数据库新建成功 } } else { result = -1; //代表数据库已经存在,无需新建 } } break; case "MySql": result = -1; break; case "Oracle": result = -1; break; case "SQLite": if (File.Exists(dbFile)) { result = -1; } else { string dbPath = Path.GetDirectoryName(dbFile); if (!Directory.Exists(dbPath)) { Directory.CreateDirectory(dbPath); } System.Data.SQLite.SQLiteConnection.CreateFile(dbFile); if (File.Exists(dbFile)) { result = 1; } } break; case "Access": if (File.Exists(dbFile)) { result = -1; } else { string dbPath = Path.GetDirectoryName(dbFile); if (!Directory.Exists(dbPath)) { Directory.CreateDirectory(dbPath); } ADOX.Catalog catalog = new ADOX.Catalog(); catalog.Create(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Engine Type=5;", dbFile)); if (File.Exists(dbFile)) { result = 1; } } break; } } catch (Exception ex) { resultException = ex; result = 0; } return(result); }
public static bool BackupDatabase(BaseConfigInfo bci, string file) { string tempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "_temp/backupDB"); if (Directory.Exists(tempPath)) { DirectoryInfo d = new DirectoryInfo(tempPath); We7Helper.DeleteFileTree(d); } DatabaseInfo dbi = GetDatabaseInfo(bci); string dbFile = dbi.DBFile.Replace("{$App}", AppDomain.CurrentDomain.BaseDirectory); bool success = false; switch (bci.DBType.ToLower()) { case "sqlite": case "access": File.Copy(dbFile, Path.Combine(tempPath, Path.GetFileName(file)), true); success = true; break; case "sqlserver": string masterString = String.Format("Server={0};Database={1};User={2};Password={3}", dbi.Server, "master", dbi.User, dbi.Password); string sql = String.Format("backup database {0} to disk='{1}' with init", dbi.Database, Path.Combine(tempPath, Path.GetFileName(file))); //创建数据库 IDbDriver driver = new SqlDbDriver(); using (IConnection conn = driver.CreateConnection(masterString)) { SqlStatement st = new SqlStatement(sql); driver.FormatSQL(st); conn.Update(st); } success = true; break; case "mysql": break; } if (success) { string[] FileProperties = new string[2]; FileProperties[0] = tempPath; //压缩目录 FileProperties[1] = file; //压缩后的目录 if (File.Exists(FileProperties[1])) { File.Delete(FileProperties[1]); } //压缩文件 try { ZipClass.ZipFileMain(FileProperties); } catch { } } return(success); }