Home > Database > Mysql Tutorial > body text

(转)SqlServer Management Objects简介,生成建表等SQL语句

WBOY
Release: 2016-06-07 15:40:23
Original
1024 people have browsed it

原文:http://topic.csdn.net/u/20080616/10/123ecf9b-e0de-4a16-94b9-091ebd60de5c.html Smo是SqlServer Management Ojbects的简称,由SQL2005提供的管理对象,sql-dmo的逻辑进化版本,主要功能由C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies下

原文:http://topic.csdn.net/u/20080616/10/123ecf9b-e0de-4a16-94b9-091ebd60de5c.html

Smo是SqlServer Management Ojbects的简称,由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对象的脚本: 

(转)SqlServer Management Objects简介,生成建表等SQL语句(转)SqlServer Management Objects简介,生成建表等SQL语句Code
--先搞一个测试环境
use tempdb
create table test(id int identity(1,1))

 

(转)SqlServer Management Objects简介,生成建表等SQL语句Code
//添加引用
            
//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);
            }

 

(转)SqlServer Management Objects简介,生成建表等SQL语句Code
输出: 
SET ANSI_NULLS ON 
SET QUOTED_IDENTIFIER ON 
CREATE TABLE [dbo].[test]
[id] [int] IDENTITY(1,1NOT NULL 
ON [PRIMARY] 

 

---

以上为原文,补充如下

添加引用如

C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll

C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Management.Sdk.Sfc.dll

C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Smo.dll

SQL SERVER 2008环境下测试通过

其它

 

(转)SqlServer Management Objects简介,生成建表等SQL语句(转)SqlServer Management Objects简介,生成建表等SQL语句Code
//生成建表SQL语句
            foreach (String s in sc)
            {
                
//sb.Append( s );
            }

            
///遍历表
            foreach( var item in db.Tables ) {
                
//sb.AppendLine( item.ToString() );
            }

            
//遍历字段
            foreach( var item in tb.Columns ) {
                
//sb.AppendLine(item.ToString());
            }

            
//遍历索引
            foreach( var item in tb.Indexes ) {
                
//sb.AppendLine(item.ToString());
            }

            
//遍历触发器
            foreach( var item in tb.Triggers ) {
                sb.AppendLine( item.ToString() );
            }

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!