Using SQLDMO to control SQL Server

Source: Internet
Author: User
Tags exception handling try catch

Microsoft provides a powerful COM interface for MS SQL Server service access to external programs, which allows easy access to SQL services.

The first step: Locate the file in Sqldmo.rll under the installation directory of MS SQL Server, and then

#import "sqldmo.rll" no_namespace

This produces the SQLDMO.TLH and Sqldmo.tli files, which contain the definition and implementation of the SQL COM interface.

The second step: is the concrete realization.

First, start a SQL Server service.

BOOL StartSQLServer()
{
//先初始化COM
if (FAILED(CoInitialize(NULL)))
{
AfxMessageBox("Com初始化错误!");
return FALSE;
}
//定义一个SQL Server的对象指针
_SQLServerPtr spSQLServer;
//标准方法,建立实例
if (FAILED(spSQLServer.CreateInstance(__uuidof(SQLServer))))
{
AfxMessageBox("无法建立SQL对象!");
return FALSE;
}
try
{
//这里设置连接SQL的一些参数
//简单。设置Login超时
spSQLServer->PutLoginTimeout(10);
//随便起个名字好啦,管不着我啦
spSQLServer->PutApplicationName("MyAppName");
spSQLServer->PutHostName("MyHostName");
//网络数据Packet的大小
spSQLServer->PutNetPacketSize(1024);
/*
It's time to set up a connection, why?? Service hasn't started yet? How do you connect? Isn't it stupid? The truth with you slowly told you: this is to test whether the SQL Server has been started, if it has been started, then we do not have to start again (restart is also wrong: The server instance is already running), has been started, why do I write code to start it ah? So if SQL Server does not start (and of course other causes can trigger an exception), the following exception handling code is executed, and we will perform the startup in exception handling: */
CString strServer(”(Local)”);
/*连接启动的SQL 的服务器名(
这里是本地SQL Server 服务,记住将本地的SQL Server服务停掉,然后测试啊)*/
Cstring strUserName(“sa”);
//连接启动的用户名,也可以使用信任连接,不用提供用户名和密码,请参考MSDN
Cstring strPassword(””);
//就是密码啦 spSQLServer->
Connect(_variant_t(strServer),_variant_t(strUserName),_variant_t());
}
catch(_com_error pCE)
{
//连接错误啦,是服务器没有启动吧
try
{
//真正启动SQL Server的代码在这里
spSQLServer->Start(FALSE,_variant_t(strServer),
_variant_t(strUserName),_variant_t(strPassword)); }
catch(_com_error pCE)
{
//这次的异常处理倒没有执行什么操作啊,随便处理一下,返回一个错误信息啦。
AfxMessageBox(pCE.Description());
spSQLServer.Release();
return FALSE;
}
}
//
Next is the release of resources, try catch is not used to become a habit, anyway, there is no wrong, more use is good. try
{
spSQLServer.Release();
spSQLServer.Release();
}
catch(_com_error pCE)
{
AfxMessageBox(pCE.Description());
return FALSE;
}
//释放COM
CoUninitialize();
return TRUE;
}

Many other interfaces can be obtained by #import "Sqldmo.rll", such as attach database, FullTextService, obtaining properties for setting up SQL Server server, and so on, of course, the corresponding stop SQL service I don't have to say.

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.