) SqlServerManagementObjects introduction, generate SQL statements such as table Creation

Source: Internet
Author: User
Topic.csdn.netu2008061610123ecf9b-e0de-4a16-94b9-091ebd60de5c.html Smo is short for SqlServerManagementOjbects, provided by SQL2005 management object, SQL-dmo logical evolution version, the main function of C: ProgramFilesMicrosoftSQLServer90SDKAssemblies

Http://topic.csdn.net/u/20080616/10/123ecf9b-e0de-4a16-94b9-091ebd60de5c.html Smo is short for SqlServer Management Ojbects, the Management object provided by SQL2005, The Logic Evolution version of SQL-dmo, the main function is C: \ Program Files \ Microsoft SQL Server \ 90 \ SDK \ Assemblies

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

Smo is short for omanagement Ojbects. It is a Management object provided by SQL2005. It is a logical evolution version of SQL-dmo. Its main function is C: microsoft under \ Program Files \ Microsoft SQL Server \ 90 \ SDK \ Assemblies. sqlServer. smo. dll files

Objects can be referenced directly by programs developed by vs2005.
Msdn reference: http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.aspx.
The document lists seven major functions. In fact, it is no exaggeration to say that as long as SQL Server Management Studio can implement things, it can be implemented with smo, because SQL Server Management Studio is developed with smo. If you have enough strength, you can develop a tool that can despise SQL Server Management Studio, such as adding smart sensing functions.

The specific detailed application will not be expanded here. There are too many objects... for example, many people ask how to generate the SQL object script:

Code
-- Create a test environment first
Use tempdb
Create table test (id int identity (1, 1 ))

Code
// Add reference
// 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"); // a database connection string
Microsoft. SqlServer. Management. Smo. Server 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 );
}

Code
Output:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
Create table [dbo]. [test] (
[Id] [int] IDENTITY (1, 1) NOT NULL
) ON [PRIMARY]

---

The above is the original text and is supplemented as follows:

Add reference as shown in

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 passed the test

Others

Code
// Generate an SQL statement for table Creation
Foreach (String s in SC)
{
// Sb. Append (s );
}

/// Traverse the table
Foreach (var item in db. Tables ){
// Sb. AppendLine (item. ToString ());
}

// Traverse Fields
Foreach (var item in tb. Columns ){
// Sb. AppendLine (item. ToString ());
}

// Traverse the index
Foreach (var item in tb. Indexes ){
// Sb. AppendLine (item. ToString ());
}

// Traverse the trigger
Foreach (var item in tb. Triggers ){
Sb. AppendLine (item. ToString ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.