Control SQL Server using SQLDMO
Deng Zhenbo
Tony said nonsense, step into the topic, there will be repeatedly discussed anything about the experience and disadvantage of MS SQL Server. Microsoft provides powerful COM interfaces for external programs, through these interfaces, you can easily access SQL service.
Step 1: Find in the SQLDMO.RLL file below in the installation directory of MS SQL Server, then
#import "sqldmo.rll" no_namespace
This produces the sqldmo.tlh and sqldmo.tli files, which contains the definition and implementation of the SQL COM interface.
Step 2: It is a specific implementation.
First come a start SQL Server service
Bool Startsql Server () {// First initialization COMIF (Failed ("COM initialization error!"); Return false;} // Define a SQL Server object pointer _SQLServerPtr SPSQLServer; // standard Method, establish an example IF (Failed (SPSQLServer.createInstance))) {AFXMessageBox ("Unable to establish SQL Object!"); Return false;}
Try {// Set some parameters // simple to connect to SQL here. Set the login timeout spsqlserver-> Putlogintimeout (10); // Just a name is ok, you can't help me SPSQLServer-> PutapplicationName ("MyAppname"); SPSQLSERVER-> PuthostName ("myhostname"); // Network data packet Size SPSQLSERVER-> PutNetPacketsize (1024); / * This establishment is connected, why? ? The service has not started? How to connect? Not a stupid? The truth is talking to you slowly: This is to test the SQL Server is already started. If it is already started, then we don't have to start again (it is also an error again: the instance of the server has run), huh, huh, Already started, do you want to write code to start it? So, if SQL Server does not start (of course, other reasons will also trigger an exception), the following exception handling code will be executed, we will execute the start * / cstring strser ("(local)"); / * Connecting SQL server name (here is a local SQL Server service, remember to stop local SQL Server service, then test it) * / cstring strusername ("sa"); // Connect the username, you can Use trust connection, don't provide username and password, please refer to MSDNCSTRING STRPASSWORD (""); // is password, SPSQLSERVER-> Connect (_VARIANT_T (STRSERVER), _ variant_t ()), _ variant_t ());} catch (_EM_ERROR PCE) {// Connection error, is the server is not started. Try {// Really started SQL Server code here SPSQLServer-> Start (false, _variant_t (strserver), _ variant_t (strusername), _ variant_t (strpassword));} catch _COM_ERROR PCE) {// This exception handling does not perform anything, just handle it, return an error message. AFXMessageBox (PCE.DESCRIPTION ()); spsqlserver.release (); return false;}} // Next is the release of resources, try catch is used to habits, this is also? ? Anyway, there is no mistake, and use it more. Try {spsqlserver.release (); spsqlserver.release ();} catch (_CM_ERROR PCE) {AFXMESSAGEBOX (PCE.DESCRIPTION ()); Return False;}
// Release comCounInitialize (); return true;}
Other interfaces can be obtained by #import "sqldmo.rll", such as Attach Database, FullTextService, get the properties of the Setting SQL Server server, etc., of course, the corresponding stop SQL service, I don't have to say it.
Download Project Source File
More articles in htttp://seesi.51.net