SMO Study Notes (5)-Additional database

Source: Internet
Author: User
SQL Server Management studio:
1. Select additional database operations

2. Select the additional button and the following interface will appear. Then, click the Add button.

3. Locate the database file, select the database you have detached or backed up, and click OK.

4. Add.

Added! Note: If the database already exists or has a duplicate name, The append database operation will fail!

The code for attaching the SMO database is as follows: // <summary>
/// Additional database
/// Note: if the database already exists, attaching will fail.
/// </Summary>
/// <Param name = "sqlconnectionstring"> </param>
Public static void attachdatabase (string sqlconnectionstring)
{
Using (sqlconnection connection = new sqlconnection (sqlconnectionstring ))
{

Server = new server (New serverconnection (connection ));
Stringcollection files = new stringcollection ();

Database currentdb = server. Databases ["test"];

If (currentdb = NULL)
{

// The master file is required
// The file name and suffix are case-insensitive. That is, the actual file is over. MDF, but it does not matter if it is set to over. MDF.
Files. Add (string. Format (@ "{0} \ {1}. MDF", "d: \" "," test "));
// Log can be left unspecified
Files. Add (string. Format (@ "{0} \ {1} _ log. LDF", "d: \" "," test "));
// Attachdatabase (Database Name, append database file path, database owner, attacexceptions option)
// The third setting is amazing! The attached database shows that the connected user in the connection is the owner TJ.
Server. attachdatabase ("Oye", files, "TJ", attacexceptions. None );
}
}
}

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.