Click to Rate and Give Feedback
Community Content
In this section
Statistics Annotations (4)
Collapse All/Expand All Collapse All
Other versions are also available for the following:
Database Class

The Database object represents a SQL Server database.

Namespace: Microsoft.SqlServer.Management.Smo
Assembly: Microsoft.SqlServer.Smo (in microsoft.sqlserver.smo.dll)
Visual Basic (Declaration)
<EvaluationModeAttribute(AutomatedPolicyEvaluationMode.CheckOnSchedule)> _
<PhysicalFacetAttribute> _
Public NotInheritable Class Database
    Inherits ScriptNameObjectBase
    Implements ICreatable, IAlterable, IDroppable, IRenamable, _
    IExtendedProperties, IScriptable, IDatabaseOptions, IDmfFacet
C#
[EvaluationModeAttribute(AutomatedPolicyEvaluationMode.CheckOnSchedule)] 
[PhysicalFacetAttribute] 
public sealed class Database : ScriptNameObjectBase, ICreatable, IAlterable, IDroppable, 
    IRenamable, IExtendedProperties, IScriptable, IDatabaseOptions, IDmfFacet
C++
[EvaluationModeAttribute(AutomatedPolicyEvaluationMode::CheckOnSchedule)] 
[PhysicalFacetAttribute] 
public ref class Database sealed : public ScriptNameObjectBase, ICreatable, IAlterable, IDroppable, 
    IRenamable, IExtendedProperties, IScriptable, IDatabaseOptions, IDmfFacet
J#
/** @attribute EvaluationModeAttribute(AutomatedPolicyEvaluationMode.CheckOnSchedule) */ 
/** @attribute PhysicalFacetAttribute() */ 
public final class Database extends ScriptNameObjectBase implements ICreatable, IAlterable, 
    IDroppable, IRenamable, IExtendedProperties, IScriptable, IDatabaseOptions, 
    IDmfFacet
JScript
EvaluationModeAttribute(AutomatedPolicyEvaluationMode.CheckOnSchedule) 
PhysicalFacetAttribute 
public final class Database extends ScriptNameObjectBase implements ICreatable, IAlterable, 
    IDroppable, IRenamable, IExtendedProperties, IScriptable, IDatabaseOptions, 
    IDmfFacet

The Database class represents databases, either system or user-defined, on the instance of SQL Server. The Server class is the parent of the Database class.

By using the Database class, you can do the following:

  • Create a new database or drop and existing database.

  • Register the database in the Active Directory directory service.

  • Reference database objects in collections, such as tables, users, and triggers.

  • Set up database mirroring.

  • Create a master database key.

  • Set up a full-text search catalog.

  • Check data, allocations, catalogs, and tables.

  • Issue a checkpoint.

  • Grant, revoke, and deny permissions to users on the database.

  • Run Transact-SQL statements.

  • Enumerate database information, such as locks or object permissions.

  • Remove the backup history.

  • Monitor the number of transactions.

  • Set the database offline or online.

  • Change the owner of the database.

  • Update statistics.

  • Shrink the database.

  • Truncate the log.

  • Script the database.

To get Database object properties, users can be a member of the public fixed server role.

To set Database object properties and run the Alter method, users must have ALTER permission on the database, or be a member of the db_owner fixed database role.

To create a database, users must have CREATE DATABASE, CREATE ANY DATABASE, or ALTER ANY DATABASE permission on the parent server.

To drop a database, users must have CONTROL permission on the database, or be a member of the db_owner fixed database role.

To grant, revoke, or deny other users permission on the database, users must have CONTROL permission on the database or be a member of the db_owner fixed database role.


Visual Basic
'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()
Any public static (Shared in Microsoft Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Development Platforms

For a list of the supported platforms, see Hardware and Software Requirements for Installing SQL Server.

Target Platforms

For a list of the supported platforms, see Hardware and Software Requirements for Installing SQL Server.
Community Content   What is Community Content?
Add new content RSS  Annotations
This class' constructor is for creating new databases, not referencing existing ones      KrymsonSkorpyon   |   Edit   |   Show History

Creating a new Database object with the server and database name of an existing database does NOT give you a reference to the existing database. If you try to call .Create() on a new Database object in this scenario, the call will fail.

To obtain a reference to an existing database, you should create an object of class Database and assign it the desired Database element from the Server object's Databases collection, thus:
[C#]
Database myDatabase = myServer.Databases["myDatabase"];
or
[VB]
Dim myDatabase as Database = myServer.Databases["myDatabase"]

Brian Knopp, Microsoft SQL support

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker