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

    How To Disable Export Button In Crystal Report When you are taking crystal report printouts through a program, such as program written using vb6. you might have a problem how to disable Export button in print preview. All you have to change following property to false or Uncheck the Export option in Property Pages. You CR.WindowShowExportBtn = False Crystal Report Object in Visual Basic 6. (CR is refer to Crystal Report Object ) You can go to Property Pages by right click and choose Properties on Crystal Report Object After you done those things you crystal Report print preview will showing like follows. I was face to above problem. We implemented a software program which is written using vb6. Cashier can take daily sale print out. before printing it is showing a preview to the user. So when the time pass user click export button and saved that report as a excel file. Then he modified the values and took printout to the management. That gives Security problem to the program a...

    Execute *.sql with .NET

    Do you ever want to execute slq statements at once. I mean SQL Script file. Normally we are using like following commands to Execute Single SQL statement. SqlCommand cmd = new SqlCommand(sSQL, conn); cmd.ExecuteNonQuery(); cmd.Dispose(); but this way you can execute one command at a time. We cannot execute SQL script using above command. Normal SQL Script Like this. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK__titleauth__au_id__0519C6AF]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[titleauthor] DROP CONSTRAINT FK__titleauth__au_id__0519C6AF GO CREATE TABLE [dbo].[authors] ( [au_id] [id] NOT NULL , [au_lname] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [au_fname] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [phone] [char] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [address] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [city] [varchar] (20) COLL...

    Magento DB access denied

    I got this error suddenly while accessing the magento site. SQLSTATE[HY000] [1129] Host 'user.workgroup' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' Solution use below command in console mysqladmin flush-hosts or log in to  mysql with a different host if its implemented on network environment and use below statement; FLUSH HOSTS; Or Restart the mysql server