Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

SQL Server Compact Code Snippet of the Week #10 : generate a CREATE TABLE script

DZone's Guide to

SQL Server Compact Code Snippet of the Week #10 : generate a CREATE TABLE script

Free Resource

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

Another entry in the scripting API samples, you will find an overview of getting started with the API here.

This weeks entry demonstrates the general pattern for scripting smaller chunks of SQL based on a single table, in this case a CREATE TABLE statement. The statement will also include any indexes and primary/foreign keys.


using (IRepository repository = new DBRepository(@"Data Source=C:\Northwind.sdf"))
{
    Generator generator = new Generator(repository, null);
    foreach (var tableName in repository.GetAllTableNames())
    { 
        generator.GenerateTableScript(tableName);
    }
    System.IO.File.WriteAllText(@"C:\script.sqlce", generator.GeneratedScript);    
}

The Generator / Generator4 class contains many methods to generate various script snippets, some of them listed here. After a number of GenerateXx calls, you can use the GeneratedScript property to get the accumulated script.



Learn tips and best practices for optimizing your capacity management strategy with the Market Guide for Capacity Management, brought to you in partnership with BMC.

Topics:

Published at DZone with permission of Erik Ejlskov Jensen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}