Easiest Way to Generate Scripts from API
crancoder
Posts: 28
What is the easiest way to generate a SQL (as opposed to an XML) schema script and a data (records) script using the API?
Thank you.
Thank you.
Comments
I'm able to create a full schema script by using regions , but have not yet figured out how to do a full data dump.
Thank you.
http://www.red-gate.com/support/SQL_Too ... ndline.htm
HTH
Project Manager
Red Gate Software Ltd
Thank you.
If you want more detail on each step go through the worked example in the SQL Data Compare API documentation here...
http://help.red-gate.com/help/SQLDataCo ... frame.html[/url]
Project Manager
Red Gate Software Ltd
Thank you.
HTH
Project Manager
Red Gate Software Ltd
I revised your code as suggested (below) and got the following results:
The synchronization SQL contains 11 lines in 11 batchesThe SQL to be run is:SET NUMERIC_ROUNDABORT OFF
GO
SET XACT_ABORT, ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS, NOCOUNT ON
GO
-- Pointer used for text / image updates. This might not be needed, but is declared here just in case
DECLARE @pv binary(16)
BEGIN TRANSACTION
COMMIT TRANSACTION
GO
REVISED CODE:
public TestDump()
{
InitializeComponent();
Database db1=new Database();
db1.RegisterForDataCompare(new ConnectionProperties(".", "WidgetDev"), Options.Default);
// Create the mappings between the two databases
TableMappings mappings = new TableMappings();
mappings.CreateMappings(db1.Tables, null);
//
//Additionally set up trim trailing spaces...
//
mappings.Options = new EngineDataCompareOptions(
MappingOptions.Default,
ComparisonOptions.TrimTrailingSpaces | ComparisonOptions.Default,
SqlOptions.Default);
using (ComparisonSession session=new ComparisonSession())
{
//
// Remember to set up the session options
//
session.Options = mappings.Options;
session.CompareDatabases(db1, null, mappings);
// now get the ExecutionBlock containing the SQL
// we want to run this on WidgetLive so we pass on true as the second parameter
SqlProvider provider=new SqlProvider();
//
// Also rememeber to set up the provider options
//
provider.Options = session.Options;
ExecutionBlock block;
try
{
block = provider.GetMigrationSQL(session, true);
rtfResults.AppendText("The synchronization SQL contains "+block.LineCount+" lines in "+block.BatchCount+" batches");
// if the ExecutionBlock was very large this could cause memory problems
rtfResults.AppendText("The SQL to be run is:");
rtfResults.AppendText(block.GetString());
// we can access the SQL in a memory efficient manner by accessing the underlying stream
// FileStream stream=block.GetFileStream();
// run the SQL ( commented out by default )
// BlockExecutor executor = new BlockExecutor();
// executor.ExecuteBlock(block, ".", "WidgetLive");
}
finally
{
block = provider.Block;
if (block != null)
{
block.Dispose(); // dispose of the objects to delete temporary files
}
}
}
db1.Dispose();
}
Setting SelectionDelegate to true seems to do the trick.
Thanks again.
Project Manager
Red Gate Software Ltd