Skip to main content

SQL Distributed Management Objects (SQL-DMO)

A SQL-DMO object exposes the attributes of a Microsoft® SQL Server™ 2000 component.
Distributed Management Objects (DMO) is a set of programmable objects that come with SQL Server that make it easy to programatically administer your databases. SQL-DMO is actually the foundation of Enterprise Manager, so you can pretty much do anything programatically that you can do in the management tools. Some of these tasks include :



  • Scripting Objects
  • Backing up databases
  • Creating jobs
  • Altering tables

  • Code snippets are from VB.NET

    Connecting using SQL Authentication

    Dim objDMO as SQLServer
    Set objDMO = new SQLDMO.SQLServer
    objDMO.Connect "(local)", "sa", "of_course_I_changed_my_password"

    Connecting using NT Authentication
    This is as simple as setting the "loginsecure" property to True and leaving off the login and password.
    Dim objDMO as SQLServer
    Set objDMO = new SQLDMO.SQLServer
    objDMO.loginsecure = true
    objDMO.Connect "(local)"
    
    Don't forget at the end of this you will want to disconnect and clean up your objects with a simple :
    
    objDMO.DisConnect
    Set objDMO = nothing

    The Tables Collection

    The object model within DMO is very similar to the heirachy in Enterprise Manager. Underneath each server is a collection of databases. Each database has collections of objects. Tables, Stored Procedures and Views all exist in collections under a database.
    Here is an example of some code that would loop through the tables collection to return the name of each table.

    'Assuming, we have set up our connection.

    Dim objDB As Database
    Set objDB = objDMO.Databases("northwind")

    Dim oTable As Table
    For Each oTable In objDB.Tables
    MsgBox oTable.Name
    Next

    'Assuming, we have set up our connection.
    
      Dim objDB As Database
    
      Set objDB = objDMO.Databases("northwind")
      
      Dim oTable As Table
    
      Set oTable = objDB.Tables("employees")
     
      'Assuming we have a text box control named text1
      
      Text1.Text = oTable.Script()
    
    
    declare @objDMO int
    declare @objDatabase int
    declare @resultCode int
    declare @dbname varchar(200)
    declare @tablename varchar(200)
    declare @cmd varchar(300)
    declare @temp varchar(8000)
    
    Set @dbname = 'PUBS'
    Set @tablename = 'Authors'
    
    
    EXEC @resultcode = sp_OACreate 'SQLDMO.SQLServer', @objDMO OUT
    if @resultcode = 0
    print 'Created Object'
    
    Exec @resultcode = sp_OASetProperty @objDMO, 'loginsecure', 'true'
    
    
    EXEC @resultcode = sp_OAMethod @objDMO, 'Connect', NULL, '(local)'
    if @resultcode = 0
    print 'connected'
    
    Set @cmd = 'databases("' + @dbname + '").tables("' + @tablename + '").script'
    Exec @resultcode = sp_OAMethod @objDMO, @cmd , @temp OUTPUT, 4
    print @temp
    
    
    EXEC @resultcode = sp_OADestroy @objDMO
    if @resultcode = 0
    Print 'destroyed object'

    Conclusion

    This has been a very brief introduction to SQL-DMO. It is a topic that a huge book could be written on so I have barely scratched the surface. The examples here are very basic and not very useful in themselves but give you an idea as to what can be achieved. I urge you to experiment with DMO and see what you can do with it. If you have any questions, post them in the forums here and I am sure you will get some help.

    Comments

    Popular posts from this blog

    Application Configuration FIle Visual Studio 2005

    Through the application configaration file You can easily save values to later usage. you can create a application configuration file from project > Add New Item (Ctrl + Shift + A) example You want to add get string from config file Create a Config file using above step. after that insert following code to the config file. its a XML file. appsettings add key="DatabasePath" value="c:\\projects\data\spider.mdb" add key="SupportEmail" value="webmaster-1@dotnetspider.com" appsettings configuration in the coding add following Dim app As New System.Configuration.AppSettingsReader Dim s As String s = app.GetValue("DatabasePath", Type.GetType("System.String")) MessageBox.Show(s) its very easy when you are handling databases. you can store database connection string in that config for later reusabilty. Please post your comments

    Expire Session When the Browser Close [Codeigniter]

    I wanted user to log again to the site whenever they close the browser. i searched everywhere and i found the easiest way. application/config/config.php sess_expire_on_close = TRUE Easy Session Preferences You'll find the following Session related preferences in your file: Preference Default Options Description sess_cookie_name ci_session None The name you want the session cookie saved as. sess_expiration 7200 None The number of seconds you would like the session to last. The default value is 2 hours (7200 seconds). If you would like a non-expiring session set the value to zero: 0 sess_expire_on_close FALSE TRUE/FALSE (boolean) Whether to cause the session to expire automatically when the browser window is closed. sess_encrypt_cookie FALSE TRUE/FALSE (boolean) Whether to encrypt the session data. sess_use_database FALSE TRUE/FALSE (boolean) Whether to save the session data to a database. You must...