Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 7106 |  Welcome, Guest!   Register  Login
Home > Articles > .NET Framework > What are the Sql Server Management Objects?

What are the Sql Server Management Objects?

Article posted by Baldi on 8/3/2010 | Views: 3042 | Category: .NET Framework | Level: Beginner red flag


What are the Sql Server Management Objects? It is a .NET library from Microsoft which allows you to access and manage all objects of the Microsoft SQL Server. It allows you to create, alter and drop objects like views, tables, jobs from your .NET application in a easy way.

Download


 Download source code for What are the Sql Server Management Objects?


Introduction

SQL Server Management Objects (also called SMO) is a .NET library which allows you to access and manage all objects of the Microsoft SQL Server. Such a library also exists for Analysis Services (AMO) and SQL Server Replication (RMO). Why do I write about it? I did a project which used SMO a lot, so it is a sort of brain dump for me. It hope this article will save you the time I spent at the beginning of my project.  

SMO supports SQL Server 2000, 2005 and 2008. The required Dll's are in assembly folder of your SQL Server Version (they are not registered in the GAC) directory. To execute the code samples, you must have a SQL Server installed on your computer, which allows you to login with integrated security. Otherwise you will have to change server connection information.

The Server object

The library has the same hierarchical arrangement as you see it in the SQL Server Management Studio. At the top of this hierarchy is the Server object.

private Server Connect()
{
var connection = new ServerConnection();
connection.ServerInstance = "localhost";
connection.LoginSecure = true;
connection.Connect();
return new Server(connection);
}

The server object object contains all information about the SQL Server, a list with all database objects, a list with all roles, a reference to the Job Server and so on. You get a new Server object with a ServerConnection. The ServerConnection can also be instantiated with a SqlConnection (and also provides more overloads). So the ServerConnection contains the similar information than the connection string normally contains.

Database, Tables, Views and other DB objects

The sample below lists all all databases of the server, all tables and views which the databases contains. Don't execute this code on a server which contains a lot of databases, it could take some minutes!

public void PrintDatabases()
{
var server = Connect();

foreach (Database db in server.Databases)
{
Console.WriteLine("Database: {0}", db.Name);

foreach (Schema schema in db.Schemas)
{
printTables(schema, db);
}

printViews(db);
}

server.ConnectionContext.Disconnect();
}

private void printTables(Schema schema, Database db)
{
foreach (Table table in db.Tables)
{
if (schema.Name.Equals(table.Schema))
{
Console.WriteLine(" Table: {0}.{1}", table.Schema, table.Name);
printColumns(table.Columns);
}
}
}

private void printColumns(ColumnCollection columns)
{
foreach (Column c in columns)
{
Console.WriteLine(" Column: {0}", c.Name);
}
}

private void printViews(Database db)
{
foreach (View view in db.Views)
{
Console.WriteLine(" View: {0}", view.Name);
printColumns(view.Columns);
printIndexes(view.Indexes);
}
}

Also here you see the hierarchical structure. The table object contains the columns but also the indexes which the table contains. The index of each collection allows to to access every object with the name. So if I want to access the table "Contact" with the schema "Person" in the database "AdventureWorks" it would look like this.

public Table GetContact()
{
return server.Databases["AdventureWorks"].Tables["Contact", "Person"];
}

More about SMO

A lot more examples provides MSDN separated in programming specific tasks. There you will also find a great overview of the object model. My next post will show how you can modify objects which lives in the SQL Server.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:7 year(s)
Home page:http://www.mbaldinger.com
Member since:Tuesday, May 25, 2010
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

Simplest solution to pass values from one user control to another user control on a web page

This article demonstrates how to copy DLL from Global Assembly Cache.

This is the simple way of getting or accessing the INPUT HTML controls value in server side coding. This will help you in dynamic programming like create HTML controls at runtime and read their values.

In this article I am going to present you a brief comparison between C#.NET and VB.NET in A-Z technical words.

Microsoft “Velocity” is the code name for their new Distributed Cache Tool. Distributed Cache is about making your application data available on a Distributed Environment means a set of Physical Servers in a Web Farm scenario or in a Distributed multi layer system. This articel is about an overview of "Velocity" will provide more detailed articles in next coming months.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 8:24:14 AM