Hi,
We need to export data from sqlServer-database in readable and applyable form for tests.
Perhaps other people need this taskaction too, so here is the example code to implement this action.
perhaps it would be great to make the three scripting options configurable. (see http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.scriptingoptions.scriptdata.aspx)
// Define a Scripter object and set the required scripting options.
var scrp = new Scripter(sqlServer);
scrp.Options.ScriptSchema = false;
scrp.Options.ScriptData = true;
scrp.Options.ScriptDrops = true;
// Iterate through the tables in database and script each one. Display the script.
foreach (Table tb in db.Tables)
{
// check if the table is not a system table
if (tb.IsSystemObject == false)
{
Console.WriteLine("-- Scripting for table " + tb.Name);
// Generating script for table tb
var sc = scrp.EnumScript(new Urn[] { tb.Urn });
foreach (string st in sc)
{
Console.WriteLine(st);
}
Console.WriteLine("--");
}
}
I have only tried posted code on sqlserver2012.
Comments: Resolved with changeset 84932: Sql2012\Database.cs: New TaskAction="ScriptData"
We need to export data from sqlServer-database in readable and applyable form for tests.
Perhaps other people need this taskaction too, so here is the example code to implement this action.
perhaps it would be great to make the three scripting options configurable. (see http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.scriptingoptions.scriptdata.aspx)
// Define a Scripter object and set the required scripting options.
var scrp = new Scripter(sqlServer);
scrp.Options.ScriptSchema = false;
scrp.Options.ScriptData = true;
scrp.Options.ScriptDrops = true;
// Iterate through the tables in database and script each one. Display the script.
foreach (Table tb in db.Tables)
{
// check if the table is not a system table
if (tb.IsSystemObject == false)
{
Console.WriteLine("-- Scripting for table " + tb.Name);
// Generating script for table tb
var sc = scrp.EnumScript(new Urn[] { tb.Urn });
foreach (string st in sc)
{
Console.WriteLine(st);
}
Console.WriteLine("--");
}
}
I have only tried posted code on sqlserver2012.
Comments: Resolved with changeset 84932: Sql2012\Database.cs: New TaskAction="ScriptData"