public SqlScipt GetCreateTableScripts(DatabaseInfo databaseInfo) { StringBuilder sb = new StringBuilder(); using (SqlConnection cn = new SqlConnection(databaseInfo.ConnectionString)) { cn.Open(); foreach (TableInfo tableInfo in databaseInfo.Tables) { string sql = SQL.Replace("{TABLE_NAME}", tableInfo.Name); using (SqlCommand command = new SqlCommand(sql, cn)) { object obj = command.ExecuteScalar(); string script = obj.ToString(); sb.AppendLine(script); } } } SqlScipt sqlScript = new SqlScipt() { Content = sb.ToString() }; return(sqlScript); }
public void ExecuteScript(string server, string database, SqlScipt script) { string connectionString = $"Server={server};Integrated security=SSPI;database={database}"; string str = script.Content; using (SqlConnection myConn = new SqlConnection(connectionString)) { using (SqlCommand myCommand = new SqlCommand(str, myConn)) { myConn.Open(); myCommand.ExecuteNonQuery(); } } }
public Result Execute(Data data) { BCP.ResultScripts scripts = BCP.GenerateExportScript(data.SourceDatabase, data.SourceServer, data.DestinationServer, data.DestinationDatabase); SqlScipt createTablesScript = data.DatabaseHelper.GetCreateTableScripts(data.SourceDatabase); data.Cmd.ExecuteScript(scripts.ExportScript); data.DatabaseHelper.CreateDatabase(data.DestinationServer, data.DestinationDatabase); data.DatabaseHelper.ExecuteScript(data.DestinationServer, data.DestinationDatabase, createTablesScript); data.Cmd.ExecuteScript(scripts.ImportScript); return(new Result()); }