|
||||||||||||||||||
|
|
Using the SMO API... The new SMO APIs can be used to interface with SQL Serer to perform a multitude of actions. By: Brian Mains
With the Microsoft SQL Server 2005 product, a new set of .NET API's are available. These API's
can be used to interface with a SQL Server (called SMO) to perform a multitude of actions. For
example, the APIs can retrieve all of the databases, tables, and columns that reside on a specific
server, as well as create and manage them. Basically, the APIs can do what the front-end GUI can
do, which are a lot of features. So, to illustrate, the code I created uses the SMO API to
retrieve information about all of the databases, tables, and columns for the SQL Express database
that is installed with Visual Studio .NET and Visual Web Developer.
The Databases collection property exposes all the databases on the server, which the code sample iterates through each one, creates a TreeNode object to represent it, and adds it to a TreeView control. The Database class itself has a list of tables that belong to it, which is iterated and added to the TreeView control as such:
Table name is put in the form of "<schema name>.<table name>z" to show distinction. You will also notice the "t:" (and "d:" before); this has a key use and is mentioned later. The last level of looping is the retrieval of columns, which are added to the proper table node.
Pretty simple? It really is straightforward and easy to do. But obviously we want more than that;
we want to see information about a database, table, or column that we select. How do we do this?
Well, in the tables and columns node creation (previous two code samples), you will see the creation
of a TreeNode, as mentioned above. The constructor is taking two arguments: the text and a key.
The key is the key to finding out what the object is. So upon selection, the key has either a d:",
"t:", or "c:" that we use to decipher the appropriate type. This is important because when the
appropriate node is selected, we have to backtrack to get the parent nodes, reestablish the
connection to the database, and reiterate through the collections. For example, when selecting a
column, we need to get the name of the parent table and database, and then loop through all of the
child collections to get the right Column object for the right Database. Remember that the TreeView
control mimics the database structure exactly, so when getting the reference to the selected column,
the Parent property contains the reference to the table name, and reference that node's Parent
property to get the database name.
Upon retracing this, we render some information about the selected object. I'll only show how this works for the database; you can see in the code sample how it works for all three. I'm referencing properties about the object directly to a label.
When dealing with this, remember the natural tree order of Server > Database > Table >
Column. You may ask: where is the schema? It is a property of the Table object (a string value).
In our example, I appended it to the table name; however, it needs to be removed when referencing
the name in the collection (db.Tables[name]).
|
|
||||||||||||||||
|
||||||||||||||||||