欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于SqlServer Management Objects的简单介绍

程序员文章站 2023-03-27 18:15:01
Smo是SqlServer Management Objects的简称,由SQL2005提供的管理对象,sql-dmo的逻辑进化版本,主要功能由C:/Program Files/M...

Smo是SqlServer Management Objects的简称,由SQL2005提供的管理对象,sql-dmo的逻辑进化版本,主要功能由C:/Program Files/Microsoft SQL Server/90/SDK/Assemblies下面的Microsoft.SqlServer.Smo.dll文件中的相关 对象来实现,可以直接由vs2005开发的程序来引用。

文档中他列举了7条大的功能,其实毫不夸张地说,只要SQL Server Management Studio能实现的东西,用smo都能实现,因为SQL Server Management Studio就是用smo开发的。如果你有足够的实力,完全可以开发一个可以藐视SQL Server Management Studio的工具,比如加入智能感知的功能。

具体详细应用这里就不展开了,对象太多...只举一个例子,很多人问的如何生成sql对象的脚本:

--先搞一个测试环境

use tempdb

create table test(id int identity(1,1))
//添加引用  
  
            //Microsoft.SqlServer.ConnectionInfo.dll  
  
            //Microsoft.SqlServer.Smo.dll  
  
            Microsoft.SqlServer.Management.Common.ServerConnection conn = new Microsoft.SqlServer.Management.Common.ServerConnection(  
  
                new System.Data.SqlClient.SqlConnection("server=localhost;uid=sa;pwd=***;database=master"));//一个数据库连接字符串  
  
            Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(conn);  
  
            Microsoft.SqlServer.Management.Smo.Database db = server.Databases["tempdb"];  
  
            Microsoft.SqlServer.Management.Smo.Table tb= db.Tables["test"];  
  
  
  
            System.Collections.Specialized.StringCollection sc= tb.Script();  
  
            foreach (String s in sc)  
  
            {  
  
                Console.WriteLine(s);  
  
            }  

输出: SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON CREATE TABLE [dbo].[test]( [id] [int] IDENTITY(1,1) NOT NULL ) ON [PRIMARY]