SQLDMO类在C#中的应用

本文转自:http://www.csharphelp.com/archives2/archive342.html

SQLDMO For C#
By Kevin Goss

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:

  • SQLDMO.Application
  • SQLDMO.SQLServer
  • SQLDMO.Database
  • SQLDMO.NameList

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:

//get all available SQL Servers    
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers();
for(int i=0;i<sqlServers.Count;i++)
{
    object srv = sqlServers.Item(i + 1);
    if(srv != null)
    {
        this.cboServers.Items.Add(srv);                        
    }
}
if(this.cboServers.Items.Count > 0)
    this.cboServers.SelectedIndex = 0;
else
    this.cboServers.Text = "<No available SQL Servers>";

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.

//get all available databases from an SQL Server
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();                
srv.Connect(this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);
foreach(SQLDMO.Database db in srv.Databases)
{
    if(db.Name!=null)
        this.cboDatabase.Items.Add(db.Name);
}

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. 

//Get all Stored procedures - tables are in the Tables collection, views are in the Views collection
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();                
srv.Connect(this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);
for(int i=0;i<srv.Databases.Count;i++)
{
    if(srv.Databases.Item(i+1,"dbo").Name == this.cboDatabase.SelectedItem.ToString())
    {
        SQLDMO._Database db= srv.Databases.Item(i+1,"dbo");
        this.lstObjects.Items.Clear();
        for(int j=0;j<db.StoredProcedures.Count;j++)
        {
            this.lstObjects.Items.Add(db.StoredProcedures.Item(j+1,"dbo").Name);
        } 
        break;
    }
}


Well folks, that is it for my SQLDMO beginners' tutorial.  Please download 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!!!

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:

  • SQLDMO.Application
  • SQLDMO.SQLServer
  • SQLDMO.Database
  • SQLDMO.NameList

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:

//get all available SQL Servers    
SQLDMO .Application sqlApp = new SQLDMO .ApplicationClass ( ) ;
SQLDMO .NameList sqlServers = sqlApp .ListAvailableSQLServers ( ) ;
for ( int i = 0 ;i< sqlServers .Count ;i + + )
{
     object srv = sqlServers .Item (i + 1 ) ;
     if ( srv ! = null )
     {
         this . cboServers .Items .Add ( srv ) ;                        
     }
}
if ( this . cboServers .Items .Count > 0 )
     this . cboServers .SelectedIndex = 0 ;
else
     this . cboServers .Text = "<No available SQL Servers>" ;

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.

//get all available databases from an SQL Server
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();                
srv.Connect(this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);
foreach(SQLDMO.Database db in srv.Databases)
{
    if(db.Name!=null)
        this.cboDatabase.Items.Add(db.Name);
}

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. 

//Get all Stored procedures - tables are in the Tables collection, views are in the Views collection
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();                
srv.Connect(this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);
for(int i=0;i<srv.Databases.Count;i++)
{
    if(srv.Databases.Item(i+1,"dbo").Name == this.cboDatabase.SelectedItem.ToString())
    {
        SQLDMO._Database db= srv.Databases.Item(i+1,"dbo");
        this.lstObjects.Items.Clear();
        for(int j=0;j<db.StoredProcedures.Count;j++)
        {
            this.lstObjects.Items.Add(db.StoredProcedures.Item(j+1,"dbo").Name);
        } 
        break;
    }
}


Well folks, that is it for my SQLDMO beginners' tutorial.  Please download 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!!!

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:

  • SQLDMO.Application
  • SQLDMO.SQLServer
  • SQLDMO.Database
  • SQLDMO.NameList

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:

//get all available SQL Servers    
SQLDMO .Application sqlApp = new SQLDMO .ApplicationClass ( ) ;
SQLDMO .NameList sqlServers = sqlApp .ListAvailableSQLServers ( ) ;
for ( int i = 0 ;i< sqlServers .Count ;i + + )
{
     object srv = sqlServers .Item (i + 1 ) ;
     if ( srv ! = null )
     {
         this . cboServers .Items .Add ( srv ) ;                        
     }
}
if ( this . cboServers .Items .Count > 0 )
     this . cboServers .SelectedIndex = 0 ;
else
     this . cboServers .Text = "<No available SQL Servers>" ;

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.

//get all available databases from an SQL Server
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();                
srv.Connect(this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);
foreach(SQLDMO.Database db in srv.Databases)
{
    if(db.Name!=null)
        this.cboDatabase.Items.Add(db.Name);
}

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. 

//Get all Stored procedures - tables are in the Tables collection, views are in the Views collection
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();                
srv.Connect(this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);
for(int i=0;i<srv.Databases.Count;i++)
{
    if(srv.Databases.Item(i+1,"dbo").Name == this.cboDatabase.SelectedItem.ToString())
    {
        SQLDMO._Database db= srv.Databases.Item(i+1,"dbo");
        this.lstObjects.Items.Clear();
        for(int j=0;j<db.StoredProcedures.Count;j++)
        {
            this.lstObjects.Items.Add(db.StoredProcedures.Item(j+1,"dbo").Name);
        } 
        break;
    }
}


Well folks, that is it for my SQLDMO beginners' tutorial.  Please download 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!!!

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:

  • SQLDMO.Application
  • SQLDMO.SQLServer
  • SQLDMO.Database
  • SQLDMO.NameList

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:

//get all available SQL Servers    
SQLDMO .Application sqlApp = new SQLDMO .ApplicationClass ( ) ;
SQLDMO .NameList sqlServers = sqlApp .ListAvailableSQLServers ( ) ;
for ( int i = 0 ;i< sqlServers .Count ;i + + )
{
     object srv = sqlServers .Item (i + 1 ) ;
     if ( srv ! = null )
     {
         this . cboServers .Items .Add ( srv ) ;                        
     }
}
if ( this . cboServers .Items .Count > 0 )
     this . cboServers .SelectedIndex = 0 ;
else
     this . cboServers .Text = "<No available SQL Servers>" ;

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.

//get all available databases from an SQL Server
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();                
srv.Connect(this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);
foreach(SQLDMO.Database db in srv.Databases)
{
    if(db.Name!=null)
        this.cboDatabase.Items.Add(db.Name);
}

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. 

//Get all Stored procedures - tables are in the Tables collection, views are in the Views collection
SQLDMO.SQLServer srv = new SQLDMO.SQLServerClass();                
srv.Connect(this.cboServers.SelectedItem.ToString(),this.txtUser.Text,this.txtPassword.Text);
for(int i=0;i<srv.Databases.Count;i++)
{
    if(srv.Databases.Item(i+1,"dbo").Name == this.cboDatabase.SelectedItem.ToString())
    {
        SQLDMO._Database db= srv.Databases.Item(i+1,"dbo");
        this.lstObjects.Items.Clear();
        for(int j=0;j<db.StoredProcedures.Count;j++)
        {
            this.lstObjects.Items.Add(db.StoredProcedures.Item(j+1,"dbo").Name);
        } 
        break;
    }
}


Well folks, that is it for my SQLDMO beginners' tutorial.  Please download 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!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sql server sqldmo_x86_x64,C#数据库备份还原很好用的.dll,里面有使用方法,引用Interop.SQLDMO.dll后的注意事项。 SQLDMO.dll是个好东西,ASP.NET利用它可以实现在线备份、还原数据库等各种功能。近日有客户要求为其在后台添加一个管理数据库的功能。于是就出现了这篇文章。 由于客户的数据库和WEB服务不再同一台服务器,当我们把网站部署在服务器上以后,运行程序,提示如下错误: Retrieving the COM class factory for component with CLSID {10020200-E260-11CF-AE68-00AA004A34D5} failed due to the following error: 80040154. 而客户又不想在这台电脑安装MSSQL,所以我们只需要在没有安装MSSQL的电脑上注册SQLDMO.DLL组件。 第一步:首先将msvcr71.dll, SQLDMO.DLL, Resources\2052\sqldmo.rll,Resources\1033\sqldmo.rll 拷贝到C:\Program Files\Microsoft SQL Server\80\Tools\Binn目录。 下载SQLDMO文件 第二步:打开开始,在运行输入 regsvr32 "C:\Program Files\Microsoft SQL Server\80\Tools\Binn\sqldmo.dll" 注册sqldmo.dll。 正常情况下,经过以上两个步骤,网页就应该可以访问了的。 但是我们经过以上两次操作后,访问网页依然提示如下错误: Retrieving the COM class factory for component with CLSID {10020200-E260-11CF-AE68-00AA004A34D5} failed due to the following error: 80070005.后经过一段时间的检查,我们发现C:\Program Files\文件夹仅有Administrator和System的控制权限,而没有其他任何用户的权限,因此我们为Microsoft SQL Server文件夹增加上Network Service 的读取权限。 至此,问题得到解决!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值