SqlServer Management Objects简介

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

msdn参考文档:

http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.aspx

文档中他列举了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]

原文地址:https://www.cnblogs.com/cl1024cl/p/6204927.html