Get the App
SLTechnology News&Howtos  ›  Development  › 

How to call COM object in C #

Shulou Source: shulou.com Published: 2022-06-02 09:04:02 09月22日 Update

This article mainly introduces how to call the COM object in C #, which is very detailed and has certain reference value. Friends who are interested must finish reading it.

C # calls COM object

We used to need to get the details of SQL Server many times in previous applications. In the past, we had to use API and inefficient ADO calls to get it. Now we have a new method, which is SQLDMO (SQL Distributed Management Objects,SQL distributed Management object), although it is not yet known and used. But SQLDMO provides many powerful features similar to getting information from SQL Server using code. For example, I'll just explain how to get a SQL Server list of your local network, how to connect to each SQL Server, and how to get a list of tables, stored procedures, and views in Server.

The SQLDMO object comes from SQLDMO.dll,SQLDMO.dll and is released with SQL Server2000. SQLDMO.dll itself is a COM object, so you must refer to it first in your .NET project, and the VS.NET integrated development environment will create all the necessary wrappers for COM. Note: if you use the "using SQLDMO;" statement to quote, you will get an error message.

After C # calls the COM object, you can easily use it.

All operations in this example use one or more of the following objects:

◆ SQLDMO.Application

◆ SQLDMO.SQLServer

◆ SQLDMO.Database

◆ SQLDMO.NameList

There are many objects available for operations such as data backup and recovery, but as an example, we will try to keep it as simple as possible to make it easy for you to enter the world of SQLDMO and browse its convenience.

Listing the SQL servers available on the local network is relatively simple. First, you need to reference the SQLDMO.Application object, and second, you must create an instance of the return value of the SQLDMO.Application.ListAvailableSQLServers () method. SQLDMO.NameList,SQLDMO.NameList is the COM collection of server names.

Remember: before you get used to C # calling the COM object, it's always scary for C # to call the COM object, but you'll be fine if you get used to it. Here is some sample code that populates the drop-down list box with a list of locally available SQL servers.

SQLDMO.ApplicationsqlApp=newSQLDMO.ApplicationClass (); SQLDMO.NameListsqlServers=sqlApp.ListAvailableSQLServers (); for (inti=0;i0) this.cboServers.SelectedIndex=0; else this.cboServers.Text= ""; / / get a list of all databases on the specified SQL server SQLDMO.ApplicationsqlApp=newSQLDMO.ApplicationClass (); SQLDMO.SQLServersrv=newSQLDMO.SQLServerClass (); srv.Connect (this.cboServers.SelectedItem.ToString (), this.txtUser.Text,this.txtPassword.Text) Foreach (SQLDMO.Databasedbinsrv.Databases) {if (db. Name calling null) this.cboDatabase.Items.Add (db.Name);} that's all about the article "how to call the COM object in C #". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Objects caching servers services information code examples content data methods articles networks low scary necessary powerful valuable creative interesting distributed Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Shulou Technology Linux Apple