Those fool-type operations in MS products are always love to release, and many complex magical things simply do the button to perform two steps. It is easy to implement. SQL Server is naturally one of these products, and beginners can use him to easily open the gate of DBMS lobby. But the two-sidedness of things will make this simple bind our hands and feet or even our ideas. When there is a brave person to shout in the lobby, I want to do my own backup tool, I have to do my own query analyzer (sometimes someone may file this "brave" necessity, but if your boss wants a tool When the database is far from those primary administrators, this "brave" is not only necessary and some helpless), SQLDMO is our terminator.
SQLDMO (SQL Distributed Management Objects, SQL Distributed Management Object) is actually a set of COM components developed by MS. As soon as I heard the COM component, many people who know that it will smile, which means we use many languages without discriminating objects. We can use C, VB, Delphi, including SQL Server itself to write SQLDMO-based programs.
Below I first introduce DMO with the example of Delphi, and slowly deepened.
Get a list of SQL servers in your network:
// First reference SQLDMO
Use sqldmo_tlb;
/ / Display SQL Server list
PROCEDURE SHOWSRVLIST;
VAR
Alist: tstrings;
SQLSRVAPP: Variant;
Begin
Try
// Produce an OLE object
SQLSRVAPP: = CreateoleObject ('sqldmo.application ";
Except
Messagedlg ('system initial error!', Mtinformation, [mbok], 0);
EXIT;
END;
Alist: = TSTRINGLIST.CREATE;
Try
IF getsrvlist (sqlsrvapp, alist) THEN
Begin
ComboBox1.Items.Assign (alist);
END;
Finally
Sqlsrvapp: = unassigned;
Alist.free;
END;
END;
// Get list of SQL servers
Function Getsrvlist (const asqlsrvapp: variant; var alist: tstrings): boolean;
VAR
Srvlist: Variant;
I: integer;
Begin
Result: = FALSE;
Try
Srvlist: = asqlsrvapp.listavailablesqlservers;
For i: = 1 to Srvlist.count DO
Begin
Alist.add (Srvlist.Item (i));
END;
Srvlist: = unassigned;
Except
EXIT;
END;
RESULT: = TRUE;
END;
If the above code is understood, the following is a function of obtaining a database list is very well understood.
Function getdblist (const _srv: _sqlserver; var alist: tstrings): boolean;
VAR
DBList: Variant;
I: integer;
Begin
Result: = FALSE;
Try
// Statement object
DBList: = _srv.database;
For i: = 1 to dblist.count do
Alist.add (dblist.item (i, 'dbo.'). Name);
DBList: = unassigned; Except
EXIT;
END;
RESULT: = true;
END;
Take a list of table names, the stored procedure list is a similar method, which is no longer cumbersome here.
The above methods have been introduced in many articles. Here, repeating them is mainly to ensure the coherence of the series, but also give beginners. The subsequent articles will be deeply in-depth, because all personal experiences and summary If you have any omissions or errors, please refer to the purpose of common progress.