Get User Database Properties with SMO
By : Kasim Wirama, MCITP, MCDBA
Still with SMO discussions, this time I would like to show how we easily get properties of a user database with SMO (Server Management Object). As brief explanation, SMO is an SQL Server specific API that enables application developer to build custom SQL Server solution inside their application.
Let’s jump into example here.
Fire up Visual Studio 2008 and create windows application project with C# (you could choose other .NET compliant language such as VB.NET). Double click on Form1 listed in Solution Explorer and drag-drop a button and a propertyGrid control.
Add 4 SMO references by right click on References folder (in Solution Explorer) and choose “Add Reference…”, choose : Microsoft.SqlServer.ConnectionInfo, Microsoft.SqlServer.Management.Sdk.Sfc, Microsoft.SqlServer.Smo, Microsoft.SqlServer.SmoExtended and click OK.
Double click on the form to open code behind and add 2 namespace declaration here :
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
Switch back to graphical designer of Form1 and double click on the button to create its event handler and put the code here :
Server srv = new Server(“(local)”);
Database db = srv.Databases[“AdventureWorks2008”];
this.PropertyGrid1.SelectedObject = db;
The code above is to retrieve database properties of AdventureWorks2008 in your local SQL Server instance.
Run your windows application project (by pressing F5 button) and click the button to display the below result :

