Sqldmo for C #
09. Oct, 2006 by admin0 comments
Download sqldmo.zip
Frequently times I have had a need to get at SQL Server details in my applications. until recently I had to use API calland bastardized ADO callto get the information I needed. now we have sqldmo (SQL distributed management objects ). although not widely known or used, sqldmo provides a very powerful set of functionality to do just about anything with an SQL Server from code. for the purposes of this example I will show how to retrieve a list of SQL servers on your local network, how to connect to one, and how to retrieve a list of tables, stored procedures, or views from a server.
The sqldmo object comes from the sqldmo. DLL that ships with SQL Server 2000. the DLL itself is a COM Object and you must reference it from your. net project as such. the IDE will create the necessary com wrappers needed to use the library. note: If you use the statement "using sqldmo;" in your app you may get an error.
(You must re-reference the COM object for the sample app to work)
After referencing the COM object, you can begin using it quite easily.
All of the operations passed med in the example use one or more of the following objects:
- Sqldmo. Application
- Sqldmo. sqlserver
- Sqldmo. Database
- Sqldmo. namelist
There are a multitude of objects available for actions such as backups and restores, but for the purpose of this article I decided to keep it simple to forget you into the world of sqldmo.
Listing the available SQL servers on your network is quite simple. first you need a references sqldmo. application object. next you set an instance of sqldmo. namelist to the return value of the sqldmo. application. listavailablesqlservers () method. the sqldmo. namelist if a com collection of the server names.
Keep in mind, calling COM objects is a little funky until you get used to it, but the conventions are similar with all of them. here is example code which fills a combo box name cboservers with a list of all available SQL servers on the local network:
// Get all available SQL servers
Sqldmo. Application Sqlapp = New Sqldmo . Applicationclass ( ) ;
Sqldmo . Namelist Sqlservers = Sqlapp . Listavailablesqlservers ( ) ;
For ( Int I = 0 ; I < Sqlservers . Count ; I + + )
{
Object Srv = Sqlservers . Item ( I + 1 ) ;
If ( Srv ! = Null )
{
This . Cboservers . Items . Add ( Srv ) ;
}
}
If ( This . Cboservers . Items . Count> 0 )
This . Cboservers . Selectedindex = 0 ;
Else
This . Cboservers . Text = "<No available SQL servers>" ;
As you can see, this is quite simple. Just remember that com collections start at an index of 1, not 0.
Connecting to a server and getting a list of databases is also fairly simple. the following code will take the chosen SQL server in the combo box, connect to it (with a user name and password in 2 text boxes ), and then poulates another Combo box with a list of databases on the server.
// Get all available databases from an SQL Server
Sqldmo . ApplicationSqlapp = New Sqldmo . Applicationclass ( ) ;
Sqldmo . Sqlserver Srv = New Sqldmo . Sqlserverclass ( ) ;
Srv . Connect ( This . Cboservers . Selecteditem . Tostring ( ) , This . Txtuser . Text, This . Txtpassword . Text ) ;
Foreach ( Sqldmo . Database DB In Srv . Databases )
{
If ( DB . Name ! = Null )
This . Cbodatabase . Items . Add ( DB . Name ) ;
}
Getting a list of objects by type is also a breeze with this library. Again, you make a connection to the database, and then you loop through the object collection.
// Get all stored procedures-tables are in the tables collection, views are in the Views collection
Sqldmo . Sqlserver Srv = New Sqldmo . Sqlserverclass ( ) ;
Srv . Connect ( This . Cboservers . Selecteditem . Tostring ( ) , This . Txtuser . Text , This . Txtpassword . Text ) ;
For ( Int I = 0 ; I < Srv . Databases . Count ; I+ + )
{
If ( Srv . Databases . Item ( I + 1 , "DBO" ) . Name = = This . Cbodatabase . Selecteditem . Tostring ( ) )
{
Sqldmo . _ Database DB = Srv . Databases . Item ( I + 1 , "DBO" ) ;
This . Lstobjects . Items . Clear ( ) ;
For ( Int J = 0 ; J < DB . Storedprocedures . Count ; J + + )
{
This . Lstobjects . Items . Add ( DB . Storedprocedures . Item ( J + 1 , "DBO" ) . Name) ;
}
Break ;
}
}
Well folks, that is it for my sqldmo beginners 'tutorial. Please do
Wnload the sample code and app to see it in action. As you can see, this is a much easier alternative when SQL information or control is needed. Happy coding !!!