[Simple use of SMO in SQL Server]

Source: Internet
Author: User

 

SMOIs the abbreviation of SQL Mangagement objects. It corresponds to ADO. net.

However, the difference is that ADO. NET is used for data access, whileSMO is designed, Although SMO can execute arbitrary SQL statements on the server.

Another difference is that ADO. Net can access any data source in the computer, and the SMO object isSpecifically for SQL ServerDesigned.
The most important class in SMO is server. Most other objects are descendants of server objects. For example, objects such as database, table, and view are continuously retrieved from the server attribute.
To use an assembly that must reference SMO in vs2005/vs2008, create a console application and add reference: Microsoft. sqlserver. connectioninfo and Microsoft. sqlserver. SMO.

 

For more information, see http://social.msdn.microsoft.com/Search/zh-cn? Query = SMO

 

Here is an episode: I encountered an error when doing it for the first time: the http://topic.csdn.net/u/20100515/19/c1298085-5d2e-41b4-8b91-7003b039aac0.html solution can be found in

 

The following are the basic operations of SMO:

 

 Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using Microsoft. sqlserver. management. common; <br/> using Microsoft. sqlserver. management. SMO; <br/> using Microsoft. sqlserver. management; </P> <p> namespace consoleapplication1 <br/>{< br/> class Program <br/>{</P> <p> static void main (string [] ARGs) <br/> {<br/> // establish a database instance connection <br/> server s = new server ("POOFLY-PC"); <br/> serverconnection SC = S. connectioncontext; <br/> SC. loginsecure = false; <br/> SC. login = "sa"; <br/> SC. password = "123456"; <br/> SC. connect (); <br/> // number of output databases and the first database name <br/> console. writeline ("databasecount:" + S. databases. count); <br/> console. writeline (S. databases [0]. name); <br/> // create a database <br/> database DB = new database (S, "newdb"); <br/> dB. create (); <br/> // create table tb <br/> table TB = new table (dB, "newtablename "); <br/> column C = new column (TB, "customerid"); <br/> C. identity = true; <br/> C. identityseed = 1; <br/> C. datatype = datatype. int; <br/> C. nullable = false; <br/> TB. columns. add (c); <br/> C = new column (TB, "customername"); <br/> C. datatype = datatype. varchar (20); <br/> C. nullable = true; <br/> TB. columns. add (c); <br/> TB. create (); <br/> // create a stored procedure <br/> storedprocedure sp = new storedprocedure (dB, "sptest "); <br/> storedprocedureparameter pa1 = new storedprocedureparameter (SP, "@ pa1", datatype. INT); <br/> sp. textmode = false; <br/> sp. parameters. add (pa1); <br/> sp. textbody = "select * From newtablename where customerid = @ pa1"; <br/> sp. create (); <br/> // execute the Stored Procedure <br/> dB. executenonquery ("sptest 1"); </P> <p >}< br/>

 

 

 

Related Article

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.