SQLDMO(SQL Distributed Management Objects,SQL分布式管理对象)封装 Microsoft SQL Server 2000 数据库中的对象。SQL-DMO 允许用支持自动化或 COM 的语言编写应用程序,以管理 SQL Server 安装的所有部分。SQL-DMO 是 SQL Server 2000 中的 SQL Server 企业管理器所使用的应用程序接口 (API);因此使用 SQL-DMO 的应用程序可以执行 SQL Server 企业管理器执行的所有功能。
SQL-DMO 用于必须包含 SQL Server 管理的任何自动化或 COM 应用程序,例如:
1.封装 SQL Server 作为自己的数据存储并想尽量减少用户的 SQL Server 管理任务的应用程序。
2.在程序本身并入了专门的管理逻辑的应用程序。
3.想在自己的用户界面中集成 SQL Server 管理任务的应用程序。
SQLDMO对象来自SQLDMO.dll,SQLDMO.dll是随SQL Server2000一起发布的。SQLDMO.dll自身是一个COM对象,因此,在你的.NET项目里必须先引用它。
Download SQLDMO.zip
Many times I have had a need to get at SQL Server details in my applications. Until recently I had to use API calls and bastardized ADO calls to 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 performed in the example use one or more of the following objects:
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 ease 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:
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.
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.
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!!!