SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
9,057 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi I would like to produce SQL Data Script via SMO with C#
My code :
public string GenerateSqlScript(bool includeData) {
Server server = new Server(@"" + DBMain.ConnDetails.Server);
Database database = server.Databases[DBMain.ConnDetails.Catalog];
List<Urn> list = new List<Urn>();
DataTable dataTable = database.EnumObjects(DatabaseObjectTypes.Table);
List<Table> tableList = new List<Table>();
foreach (DataRow row in dataTable.Rows) {
if(row.ItemArray[2].ToString()== "myTable")
list.Add(new Urn((string)row["Urn"]));
//MessageBox.Show(row.ItemArray[2].ToString());
}
Scripter scripter = new Scripter();
scripter.Server = server;
scripter.Options.IncludeDatabaseContext = false;
scripter.Options.IncludeHeaders = true;
scripter.Options.SchemaQualify = true;
scripter.Options.ScriptData = includeData;
scripter.Options.SchemaQualifyForeignKeysReferences = true;
scripter.Options.NoCollation = true;
scripter.Options.DriAllConstraints = true;
scripter.Options.DriAll = true;
scripter.Options.DriAllKeys = true;
scripter.Options.Triggers = true;
scripter.Options.DriIndexes = true;
scripter.Options.ClusteredIndexes = true;
scripter.Options.NonClusteredIndexes = true;
scripter.Options.ToFileOnly = false;
//this is very slow...
var scripts = scripter.EnumScript(list.ToArray());
string result = "";
foreach (var script in scripts)
result += script + Environment.NewLine;
return result;
}
with a few rows only of a table , not all table data. How add filter ?
There is no option to add a filter, the data script generator create INSERTS for all data in the table. If you want to limit it, create a view with all columns and a filter on the data.