SQLDMO (SQL distributed Management objects,sql Distributed Management Objects) encapsulates objects in a Microsoft SQL Server 2000 database. SQL-DMO allows applications to be written in languages that support automation or COM to manage all parts of a SQL Server installation. SQL-DMO is the application interface (API) used by SQL Server Enterprise Manager in SQL Server 2000, so applications that use SQL-DMO can perform all the functions that SQL Server Enterprise Manager performs.
SQL-DMO is used for any automation or COM applications that must contain SQL Server management, for example:
1. An application that encapsulates SQL Server as its own data store and wants to minimize the user's SQL Server management tasks.
2. Applications that incorporate specialized management logic into the program itself.
3. Applications that want to integrate SQL Server management tasks into their own user interfaces.
The SQLDMO object from Sqldmo.dll,sqldmo.dll is published with SQL Server2000. SQLDMO.dll itself is a COM object, so you must refer to it in your. NET project first.
Get a list of SQL Servers in your network:
' Get a list of SQL Servers
' SQL SERVER SP2 and above must be installed
Dim I as Short
Dim Sqlapp as New SQLDMO. Application ()
Dim ServerName as SQLDMO. NameList
ServerName = Sqlapp.listavailablesqlservers
For i = 1 to Servername.count
CBSERVER.ITEMS.ADD (Servername.item (i))
Next
Get a list of all databases for the specified SQL Server:
' Get a list of all databases for the specified SQL Server
Dim Sqlapp as New SQLDMO. Application ()
Dim oserver as New SQLDMO. SQL Server ()
Oserver.connect ("(local)", "sa", "sa")
CboDatabase.Items.Clear ()
Dim DB as SQLDMO. Database
For all db in oserver.databases
ME.CBODATABASE.ITEMS.ADD (db. Name)
Next
Get all the tables, views, stored procedures:
Dim I as Short
Dim oserver as New SQLDMO. SQL Server ()
Oserver.connect ("(local)", "sa", "sa")
Dim db as New SQLDMO. Database ()
For I = 1 to OServer.Databases.Count
If OServer.Databases.Item (I, "dbo"). Name = "Northwind" Then Exit for
Next
If I > OServer.Databases.Count Then Exit Sub
db = OServer.Databases.Item (I, "dbo")
ListBox1.Items.Clear ()
' Get all the stored procedures
For I = 1 to db. Storedprocedures.count
LISTBOX1.ITEMS.ADD (db. Storedprocedures.item (I, "dbo"). Name)
Next
' Get all the tables
For I = 1 to db. Tables.count
LISTBOX1.ITEMS.ADD (db. Tables.item (I, "dbo"). Name)
Next
' Get all the views
For I = 1 to db. Views.count
LISTBOX1.ITEMS.ADD (db. Views.item (I, "dbo"). Name)
Next
Using SQLDMO to implement a database backup with a progress bar:
' Add progress bar ProgressBar1 control
' Referencing the Microsoft SQLDMO Object Library
' Statement
Public WithEvents Bkps as SQLDMO. Backup
' Database backup operations
Private Sub Btnbackup_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnbackup.click
Dim oSQLServer as New SQLDMO. SQL Server ()
Osqlserver.loginsecure = False
Osqlserver.connect ("(local)", "sa", "sa") ' Connect to server
Me.cursor = Windows.Forms.Cursors.WaitCursor
Bkps = CreateObject ("SQLDMO"). Backup ")
Bkps. Database = "Northwind" specifies the databases to be backed up
Bkps. Action = 0
Bkps. Files = "F:\Northwind.bak" ' Specifies backup files
Bkps. Initialize = True
progressBar1.Value = 0
Progressbar1.maximum = 100
Me.cursor = Windows.Forms.Cursors.Default ()
Application.doevents ()
Dim Mousecur as Cursor
Me.cursor = Windows.Forms.Cursors.WaitCursor
Bkps. SQLBackup (oSQLServer)
progressBar1.Value = 100
Application.doevents ()
Bkps = Nothing
Me.cursor = Windows.Forms.Cursors.Default ()
MsgBox ("Database backup Complete", Msgboxstyle.information, "System messages")
End Sub
' Show progress
Private Sub bkps_percentcomplete (ByVal message as String, ByVal Percent as Integer) Handles bkps. PercentComplete
progressBar1.Value = Progressbar1.maximum * (percent/100)
End Sub