Databases Reference
In-Depth Information
tion about the objects and DDL commands to be scripted. The sample code in Listing
34.11 creates a script with the
Alter
command to modify or create the FoodMart 2008
database. For more information about the
Alter
command, see Chapter 26.
LISTING 34.11
Script Database
//Create a Server object.
using (Server srv = new Server())
{
string fileName = “..\\..\\scriptDBAlter.xml”;
//Connect to the server.
srv.Connect(“localhost”);
Database db = srv.Databases.FindByName(“Foodmart 2008”);
//Create the scripter object.
Scripter scripter = new Scripter();
//Create an XML text writer, which will be used to script the command
//into the file.
System.Xml.XmlTextWriter writer =
new System.Xml.XmlTextWriter(fileName,Encoding.UTF8);
//Create a scriptInfo object.
ScriptInfo scriptInfo = new ScriptInfo(db,
ScriptAction.AlterWithAllowCreate, ScriptOptions.Default, false);
//Create an array of the scriptInfo objects. In this example, the array
//contains just one element.
ScriptInfo []scriptInfos = new ScriptInfo[] { scriptInfo };
//Create a scripter object.
scripter.Script(scriptInfos, writer);
//Close the XML writer.
writer.Close();
}
In addition to the
Script
method, AMO provides a number of methods that simplify
scripting:
ScriptCreate
,
ScriptAlter
, and
ScriptDelete
. These methods generate
ScriptInfo
objects with
ScriptAction.Create
,
ScriptAction.Alter
, or
ScriptAction.Delete
, and call the
Script
method. Listing 34.12 shows the
ScriptAlter
method.
LISTING 34.12
Using the
ScriptAlter
Command
//Create a server object.
using (Server srv = new Server())
{
//Connect to the server.
srv.Connect(“localhost”);
Database db = srv.Databases.FindByName(“Foodmart 2008”);
Search WWH ::
Custom Search