static int Main(string[] args) { IMessage message = new MessageConsole(); if (args.Length == 0) { message.ShowParameterError(); return(1); } string path = AppDomain.CurrentDomain.BaseDirectory; string fileName = Path.Combine(path, args[0]); if (!File.Exists(fileName)) { message.ShowParameterError(); return(2); } SQLObjects dbGen = new SQLObjects(); dbGen.Read(fileName); SQLGererator gen = new SQLGererator(message); Console.WriteLine(gen.ScriptDatabase(dbGen)); return(0); }
public void Read(string fileName) { SQLObjects obj = JsonConvert.DeserializeObject <SQLObjects>(System.IO.File.ReadAllText(fileName)); this.DataBaseName = obj.DataBaseName; this.Items = obj.Items; this.Password = obj.Password; this.Username = obj.Username; this.Server = obj.Server; this.Filename = obj.Filename; this.OneFilePerObject = obj.OneFilePerObject; }
public string ScriptDatabase(SQLObjects dbGen) { var sb = new StringBuilder(); _message.ShowMessage("Conectando ao banco de dados..."); var server = new Server(dbGen.Server); server.ConnectionContext.LoginSecure = false; // set to true for Windows Authentication server.ConnectionContext.Login = dbGen.Username; // "brasilriskSQL"; server.ConnectionContext.Password = dbGen.Password; // "Cristiano67108"; var databse = server.Databases[dbGen.DataBaseName]; var scripter = new Scripter(server); scripter.Options.ScriptDrops = false; scripter.Options.WithDependencies = true; scripter.Options.IncludeHeaders = true; _message.ShowMessage("Conectado"); _message.ShowMessage("Gerando scripts das tabelas..."); var smoObjects = new Urn[1]; foreach (Table t in databse.Tables) { smoObjects[0] = t.Urn; if (t.IsSystemObject == false && dbGen.Items.Count(c => c.Nome == t.Name && c.Tipo == SQLItemTypeEnum.Table) > 0) { var sbItem = new StringBuilder(); var item = dbGen.Items.First(f => f.Nome == t.Name && f.Tipo == SQLItemTypeEnum.Table); scripter.Options.ScriptData = item.ScriptData; //StringCollection sc = scripter.Script(smoObjects); var sc = scripter.EnumScript(smoObjects); foreach (var st in sc) { sb.Append(st + "\n"); sbItem.Append(st + "\n"); } if (dbGen.OneFilePerObject) { var path = AppDomain.CurrentDomain.BaseDirectory; var fileName = MakeUnique(Path.Combine(path, t.Name + ".sql")); File.WriteAllText(fileName.FullName, sbItem.ToString()); } } } _message.ShowMessage("Gerando scripts das procedures..."); foreach (StoredProcedure t in databse.StoredProcedures) { smoObjects[0] = t.Urn; if (t.IsSystemObject == false && dbGen.Items.Count(c => c.Nome == t.Name && c.Tipo == SQLItemTypeEnum.Table) > 0) { var sbItem = new StringBuilder(); var item = dbGen.Items.First(f => f.Nome == t.Name && f.Tipo == SQLItemTypeEnum.Table); StringCollection sc = scripter.Script(smoObjects); foreach (var st in sc) { sb.Append(st); } if (dbGen.OneFilePerObject) { var path = AppDomain.CurrentDomain.BaseDirectory; var fileName = MakeUnique(Path.Combine(path, t.Name + ".sql")); File.WriteAllText(fileName.FullName, sbItem.ToString()); } } } _message.ShowMessage("Processo terminado."); if (!dbGen.OneFilePerObject) { var path = AppDomain.CurrentDomain.BaseDirectory; var fileName = MakeUnique(Path.Combine(path, dbGen.Filename)); File.WriteAllText(fileName.FullName, sb.ToString()); } return(sb.ToString()); }