[Simple use of SMO in SQL Server] (load)

Source: Internet
Author: User
Category: Miscellaneous SQL Server knowledge point 2010-05-15 554 people read comments (2) collect reports

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:

 

View plaincopy to clipboardprint?
  1. UsingSystem;
  2. UsingSystem. Collections. Generic;
  3. UsingSystem. Linq;
  4. UsingSystem. Text;
  5. UsingMicrosoft. SqlServer. Management. Common;
  6. UsingMicrosoft. SqlServer. Management. Smo;
  7. UsingMicrosoft. SqlServer. Management;
  8. NamespaceLeleapplication1
  9. {
  10. ClassProgram
  11. {
  12. Static VoidMain (String[] Args)
  13. {
  14. // Establish a database instance connection
  15. Server s =NewServer ("POOFLY-PC ");
  16. ServerConnection SC = s. ConnectionContext;
  17. SC. LoginSecure =False;
  18. SC. Login = "sa ";
  19. SC. Password = "123456 ";
  20. SC. Connect ();
  21. // Number of output databases and first database name
  22. Console. WriteLine ("DatabaseCount:" + s. Databases. Count );
  23. Console. WriteLine (s. Databases [0]. Name );
  24. // Create a database
  25. Database db =NewDatabase (s, "newdb ");
  26. Db. Create ();
  27. // Create table Tb
  28. Table tb =NewTable (db, "NewTableName ");
  29. Column c =NewColumn (tb, "CustomerID ");
  30. C. Identity =True;
  31. C. IdentitySeed = 1;
  32. C. DataType = DataType. Int;
  33. C. Nullable =False;
  34. Tb. Columns. Add (c );
  35. C =NewColumn (tb, "CustomerName ");
  36. C. DataType = DataType. VarChar (20 );
  37. C. Nullable =True;
  38. Tb. Columns. Add (c );
  39. Tb. Create ();
  40. // Create a stored procedure
  41. StoredProcedure sp =NewStoredProcedure (db, "sptest ");
  42. StoredProcedureParameter pa1 =NewStoredProcedureParameter (sp, "@ pa1", DataType. Int );
  43. Sp. TextMode =False;
  44. Sp. Parameters. Add (pa1 );
  45. Sp. TextBody = "select * from NewTableName where CustomerID = @ pa1 ";
  46. Sp. Create ();
  47. // Execute the Stored Procedure
  48. Db. ExecuteNonQuery ("sptest 1 ");
  49. }
  50. }
  51. }
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.