SoFunction
Updated on 2025-03-07

C# How to use SQLDMO to operate database

This article describes the method of using SQLDMO to operate a database in C#. Share it for your reference. The specific analysis is as follows:

It was released with SQL Server 2000. It's a COM object

SQLDMO (SQL Distributed Management Objects) encapsulates objects in Microsoft SQL Server 2000 database. SQL-DMO allows the writing of applications in a language that supports automation or COM to manage all parts of a SQL Server installation. SQL-DMO is the application program interface (API) used by SQL Server Enterprise Manager in SQL Server 2000; therefore applications using SQL-DMO can perform all the functions performed by SQL Server Enterprise Manager.

The general relationship of SQLServer:

Application-->SQLServer-->DataBase

Instance SQLDMO, the following classes are mainly used:

(Create with)
(Use create, mainly using its Connect to connect to the database server)
(You can get the server collection through it and Application. For other purposes, please refer to its API)
(Can be obtained through it and the database collection)

Example 1: Get the list of SQL servers in the LAN

Mainly use the Application ListAvaiableSQLServers to get NameList,

 sqlapp = new ();
 names = ();
();
for(int i=1;i<;i++)
{
 if((i)!=null)
  ((i));
}
if(>0)
{
  = 1;
}
else
{
  = "No SQL Server available";
}  

Example 2: Get the database list under a certain server:

Mainly use SQLServer and its properties DataBases

 database= new SQLServerClass();
try
{
 (, "sa", "");
 ();
 foreach ( db in )
 {
  ();
 }
}
catch ( ee)
{
 ();
 ("Unable to connect to selected server");
}

Then you get the properties of the database, and then you're done.

I hope this article will be helpful to everyone's C# programming.