title | description | author | ms.author | ms.date | ms.service | ms.topic | ms.custom | helpviewer_keywords | monikerRange | |||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Creating, Altering, and Removing Databases |
Creating, Altering, and Removing Databases |
markingmyname |
maghan |
08/06/2017 |
sql |
reference |
|
|
=azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric |
[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance Synapse Analytics FabricSQLDB]
In SMO, a database is represented by the xref:Microsoft.SqlServer.Management.Smo.Database object.
It is not necessary to create a xref:Microsoft.SqlServer.Management.Smo.Database object to modify or remove it. The database can be referenced by using a collection.
To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see Create a Visual C# SMO Project in Visual Studio .NET.
This code example creates a new database. Files and file groups are automatically created for the database.
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Define a Database object variable by supplying the server and the database name arguments in the constructor.
Dim db As Database
db = New Database(srv, "Test_SMO_Database")
'Create the database on the instance of SQL Server.
db.Create()
'Reference the database and display the date when it was created.
db = srv.Databases("Test_SMO_Database")
Console.WriteLine(db.CreateDate)
'Remove the database.
db.Drop()
This code example creates a new database. Files and file groups are automatically created for the database.
{
//Connect to the local, default instance of SQL Server.
Server srv;
srv = new Server();
//Define a Database object variable by supplying the server and the database name arguments in the constructor.
Database db;
db = new Database(srv, "Test_SMO_Database");
//Create the database on the instance of SQL Server.
db.Create();
//Reference the database and display the date when it was created.
db = srv.Databases["Test_SMO_Database"];
Console.WriteLine(db.CreateDate);
//Remove the database.
db.Drop();
}
This code example creates a new database. Files and file groups are automatically created for the database.
#Get a server object which corresponds to the default instance
cd \sql\localhost\
$srv = get-item default
#Create a new database
$db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database -argumentlist $srv, "Test_SMO_Database"
$db.Create()
#Reference the database and display the date when it was created.
$db = $srv.Databases["Test_SMO_Database"]
$db.CreateDate
#Drop the database
$db.Drop()
xref:Microsoft.SqlServer.Management.Smo.Database