Use sqldmo to control SQL Server

Source: Internet
Author: User
Tags try catch
Microsoft provides powerful com interfaces for access to the ms SQL Server Service of external programs. Through these interfaces, you can easily access the SQL service without any effort.

Step 1: Find the sqldmo. rll file in the installation directory of ms SQL Server, and then

# Import "sqldmo. rll" no_namespace


In this way, the sqldmo. tlh and sqldmo. TLI files are generated, including the definition and implementation of the SQL COM interface.

Step 2: implement it.


Start the SQL Server service first.

Bool startsqlserver ()
{
// Initialize com first
If (failed (coinitialize (null )))
{
Afxmessagebox ("com initialization error! ");
Return false;
}
// Define
Object Pointer of SQL Server
_ Sqlserverptr spsqlserver;
// Standard method to create an instance
If (failed (spsqlserver. createinstance (_ uuidof (sqlserver ))))
{
Afxmessagebox ("the SQL object cannot be created!
");
Return false;
}

Try
{
// Set the connection here
SQL Parameters
// Simple. Set login timeout
Spsqlserver-> putlogintimeout (10 );
// Just give me a name.
Spsqlserver-> putapplicationname ("myappname ");
Spsqlserver-> puthostname ("myhostname ");
//
Network DataPacket Size
Spsqlserver-> putnetpacketsize (1024 );
/*
The connection should be established. Why ?? The service is not started yet? How to connect? Isn't it silly? Let me tell you the truth: This is to test whether SQL server has been started. If it has been started, we don't need to start it again (it is also an error to start it again: the server instance is running.) Haha, it has already started. Why should I write code to start it? Therefore, if SQL ServerIf it is not started (of course, an exception is triggered for other reasons), the following exception handling code will be executed, and we will execute the startup in the exception handling process.
*/
Cstring strserver ("(local)");/* Name of the server that connects to the SQL Server started (this is the local SQL Server service. Remember to stop the local SQL Server service and test it.
)*/
Cstring strusername ("sa"); // the user name used to start the connection. You can also use a trusted connection without providing the user name and password. For details, refer to the msdn
Cstring strpassword ("); // the password.
Spsqlserver-> connect (_ variant_t (strserver), _ variant_t (strusername), _ variant_t ());
}
Catch (_ com_error PCE)
{
// The connection is incorrect. The server is not started.
Try
{
//
The code for truly starting SQL Server is hereSpsqlserver-> Start (false, _ variant_t (strserver), _ variant_t (strusername), _ variant_t (strpassword ));}
Catch (_ com_error PCE)
{
// This exception handling has not performed any operations. If you handle the exception at will, an error message is returned.
Afxmessagebox (PCE. Description ());
Spsqlserver. Release ();
Return false;
}
}

// The next step is to release resources.Is try catch a habit ?? There is nothing wrong with it. You can use it more.
Try
{
Spsqlserver. Release ();
Spsqlserver. Release ();
}
Catch (_ com_error PCE)
{
Afxmessagebox (PCE. Description ());

Return false;
}

// ReleaseCom
Couninitialize ();
Return true;
}

# Import "sqldmo. rll "can also obtain many other interfaces, such as attach database, fulltextservice, and properties for setting SQL Server servers. Of course, I don't need to say that I should stop the SQL service.

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.