public async Task ExecuteAsync([NotNull] SqlProject project) { if (project == null) { throw new ArgumentNullException(nameof(project)); } await _connection.OpenAsync().ConfigureAwait(false); try { if (project.PreDeployFilePath != null) { _sqlCmd.ExecuteFile(project.PreDeployFilePath); } await ExecuteSqlAsync(project.Database).ConfigureAwait(false); await ExecuteSqlAsync(project.Objects).ConfigureAwait(false); if (project.PostDeployFilePath != null) { _sqlCmd.ExecuteFile(project.PostDeployFilePath); } } finally { _connection.Close(); } }
private void CreateAndSeedTestDatabase() { var databaseCreateFilePath = Path.Combine(PathHelper.GetFilesPath(), "Teacher.Database_Create.sql"); var script = File.ReadAllText(databaseCreateFilePath); script = script.Replace("DatabaseName \"Teacher.Database\"", "DatabaseName \"Teacher.Database.Tests\""); File.WriteAllText(databaseCreateFilePath, script); var databaseSeedFilePath = Path.Combine(PathHelper.GetFilesPath(), "Teacher.Database_TestSeed.sql"); var connectionString = new ConnectionStringFactory().ToServer(); using (var connection = new SqlConnection(connectionString)) { connection.Open(); var inst = new SqlCmd(connection); inst.ExecuteFile(databaseCreateFilePath); inst.ExecuteFile(databaseSeedFilePath); connection.Close(); } }
public static void CreerBaseDeDonnees() { using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLOCALDB;Integrated Security=True;")) { conn.Open(); var tempPath = Path.GetTempFileName(); File.WriteAllText(tempPath, Properties.Resources.CreateDatabase); //SqlCmd est une classe définie dans la librairie DbUtil, dont le code a été récupéré //du repository GitHub https://github.com/rusanu/DbUtilSqlCmd SqlCmd.ExecuteFile(conn, tempPath); conn.Close(); } }
void OnCreateDatabase() { using (var cn = new SqlConnection(_setting.GetMasterConstr())) { try { cn.Open(); string dbName = _setting.InitialCatalog; var sql = string.Format("Select db_id('{0}')", dbName); var cmd = cn.CreateCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = sql; if (cmd.ExecuteScalar() != DBNull.Value) { if (GetService <IMessageBoxService>().AskYesNo( string.Format("Kho dữ liệu tên '{0}' đã tồn tại. Bạn có muốn xóa dữ liệu hiện tại và tiếp tục không ?", dbName)) == System.Windows.MessageBoxResult.No) { return; } cmd.CommandText = string.Format("Drop Database [{0}]", dbName); cmd.ExecuteNonQuery(); } cmd.CommandText = string.Format("Create Database [{0}]", dbName); cmd.ExecuteNonQuery(); var nl = System.Environment.NewLine; cmd.CommandText = string.Format("Use {0}", dbName); cmd.ExecuteNonQuery(); SqlCmd sqlCmd = new SqlCmd(cn); sqlCmd.ExecuteFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "create.sql")); cn.Close(); GetService <IMessageBoxService>().ShowInfo("Tạo mới dữ liệu thành công."); } catch (Exception ex) { GetService <IMessageBoxService>().ShowError("Lỗi, tạo dữ liệu không thành công."); LogService.Logger.Error(ex); } } }