如何启动SQL服务
我想在程序中这样:
首先判断SQL Server服务器是否安装,如果没安装则提示。
如果安装了,判断服务是否动,如果没有启动的话,就程序中实现启动SQL。
我想请教如何判SQL Server是否启动?小弟先谢了!
问题点数:20、回复次数:3Top
1 楼seesi(不是我想骗你,是我不知道怎么才能不骗!)回复于 2001-03-07 08:42:00 得分 0
没有人知道吗? Dim oSQLServer As Object
Set oSQLServer = CreateObject("SQLDMO.SQLServer")
If oSQLServer Is Nothing Then
MsgBox "自动化错误!", vbExclamation, "失败"
GoTo CleanUp
End If
oSQLServer.Start False, "(Local)", "sa", ""
Sleep 5
Err.Clear
oSQLServer.Connect "(Local)", "sa", ""
If Err Then
oSQLServer.Continue
Sleep 5
Err.Clear
oSQLServer.Connect "(Local)", "sa", ""
If Err Then
'MsgBox "启动服务失败!", vbExclamation, "失败"
Else
StartSQLService = True
End If
Else
StartSQLService = True
End If
CleanUp:
Set oSQLServer = Nothing
在VB中可以这么写,如何转化成在VC中的写法呢?
Top
2 楼kincaid(IT苦行僧)回复于 2001-03-07 08:48:00 得分 20
在VC中,你可以建立一表单类,该表单类必须是定义为该SQL中某个表单或查询的数据集类,然后在OPEN中设置可访问的用户名及密码即可在执行时访问SQL数据库了。至于怎样自动启动该服务,数据表单类的上层类中已有说明,你可以自己去看看。Top
3 楼seesi(不是我想骗你,是我不知道怎么才能不骗!)回复于 2001-03-07 09:35:00 得分 0
谢谢(IT苦行僧)
!
我在SQL的DevTools中找到了一个例子,解决了这个问题,与大家共享。
/***********************************************************************
Copyright (c) 2000, Microsoft Corporation
All Rights Reserved.
***********************************************************************/
// **********************************************************************
// Smartptr - SQL-DMO smartpointer sample application uses
// the built-in Visual C 6.0 _comp_ptr_t support.
// If prefered, adapt to use CComPtr or CComQIPtr
// (VC 6.0 and later)
//
// the _com_error calls ->Release automatically when it
// goes out of scope to clean itself up.
// there is no need to call delete or release.
// **********************************************************************
#define STRICT // Strict type checking.
#define WIN32_LEAN_AND_MEAN // Do not include the world.
#define INC_OLE2 // Include OLE/COM files
#include <windows.h>
#include <stdio.h>
#include <comdef.h> // Compiler support for _bstr_t
// The #import statement is locale specific. Change the path to import
// from the locale specific .rll file installed.
#import "d:\Program Files\Microsoft SQL Server\80\Tools\Binn\Resources\1033\sqldmo.rll" no_namespace
// **********************************************************************
// Function declarations.
// **********************************************************************
void vDisplayError(_com_error & pCE);
BOOL bSetDefaults(_SQLServerPtr & spSQLServer);
BOOL bConnect(_SQLServerPtr & spSQLServer);
void vDisconnect(_SQLServerPtr & spSQLServer);
void vDoQuery(_SQLServerPtr & spSQLServer, LPCTSTR strQuery);
void vDisplayResults(QueryResultsPtr & spQueryResults);
// **********************************************************************
// Macros.
// **********************************************************************
#define _MAX_COL 25
#define _MAX_COL_FMT "%-25s "
#define _SERVER "." // Local server
#define _USER "sa"
#define _PWD ""
#define _QUERYSTMT "SELECT LastName, FirstName, HireDate FROM " " Northwind.dbo.Employees"
// **********************************************************************
// main()
// **********************************************************************
void main(void)
{
printf("\n\nSQL-DMO Smart Pointer Sample\n");
HRESULT hr;
if SUCCEEDED(hr = CoInitialize(NULL) )
{
// Use the interface appropriate for LPSQLDMOSERVER.
try
{
_SQLServerPtr spSQLServer;
if (SUCCEEDED(spSQLServer.CreateInstance(__uuidof(SQLServer))))
{
try
{
if (TRUE == bSetDefaults(spSQLServer))
{
vDoQuery(spSQLServer, _QUERYSTMT);
}
// Because the spSQLServer was created in main, it
// will not go out of scope until the end of main.
//
// However, this is after the OleUninitilize is called
// and causes an error.
//
// For this special case, call the .Release to
// clean up the pointer and do the work.
spSQLServer.Release();
}
catch(_com_error pCE)
{
vDisplayError(pCE);
spSQLServer.Release(); // Free the interface.
}
}
else
{
printf("\nUnable to create the SQLServer object.");
}
}
catch(_com_error pCE)
{
vDisplayError(pCE);
}
CoUninitialize();
}
else
{
printf("\nCall to CoInitialize failed.");
}
printf("\n");
}
// **********************************************************************
// vDisplayError
//
// Display error information.
// **********************************************************************
void vDisplayError(_com_error & pCE)
{
// Assuming ANSI build at this time.
printf( "\n%s Error: %ld\r\n"
"%s\r\n"
"%s\r\n",
(char*)pCE.Source(),
pCE.Error(),
(char*)pCE.Description(),
(char*)pCE.ErrorMessage());
}
// **********************************************************************
// bSetDefaults
//
// Accepts the current address so the RefCount does not need to be
// touched. There is no reason to increment it on entry and then
// decrement it on exit, just to be doing it.
// **********************************************************************
BOOL bSetDefaults(_SQLServerPtr & spSQLServer)
{
BOOL bRC = FALSE;
printf("\nSetting SQL Server object properties.");
try
{
spSQLServer->PutLoginTimeout(10);
spSQLServer->PutApplicationName("SmartPointers");
spSQLServer->PutHostName("Test");
spSQLServer->PutNetPacketSize(1024);
bRC = TRUE;
}
catch(_com_error pCE)
{
vDisplayError(pCE);
}
return bRC;
}
// **********************************************************************
// bConnect
//
// Connect to the SQL Server.
// **********************************************************************
BOOL bConnect(_SQLServerPtr & spSQLServer)
{
BOOL bRC = FALSE;
printf("\nAttempting to connect to %s as %s", _SERVER, _USER);
try
{
spSQLServer->Start(FALSE,"(local)");
spSQLServer->Connect(_SERVER, _USER, _PWD);
bRC = TRUE;
}
catch(_com_error pCE)
{
vDisplayError(pCE);
}
return bRC;
}
// **********************************************************************
// vDisconnect
//
// Disconnect from the SQL Server.
// **********************************************************************
void vDisconnect(_SQLServerPtr & spSQLServer)
{
printf("\nDisconnecting from %s", _SERVER);
try
{
spSQLServer->Close();
}
catch(_com_error pCE)
{
vDisplayError(pCE);
}
}
// **********************************************************************
// vDoQuery
//
// Execute query capturing generated result sets in a QueryResults
// object.
// **********************************************************************
void vDoQuery(_SQLServerPtr & spSQLServer, LPCTSTR strQuery)
{
QueryResultsPtr spQueryResults;
if (TRUE == bConnect(spSQLServer))
{
printf("\nExecuting %s\n", strQuery);
try
{
spQueryResults = spSQLServer->ExecuteWithResults(strQuery);
vDisplayResults(spQueryResults);
}
catch(_com_error pCE)
{
vDisplayError(pCE);
}
vDisconnect(spSQLServer);
}
}
// **********************************************************************
// vDisplayResults
//
// Print result set(s) information and contents.
// **********************************************************************
void vDisplayResults(QueryResultsPtr & spQueryResults)
{
long lMaxColLen = 0;
char strDisplayData[_MAX_COL +1] = "";
_bstr_t bstrColName;
_bstr_t bstrRowInfo;
try
{
// Loop through the results sets.
for (long lSet = 1; lSet <= spQueryResults->GetResultSets(); lSet++)
{
spQueryResults->PutCurrentResultSet(lSet);
printf("\n >>> Result set #%ld\n", lSet);
// Print the column headers.
for (long lCols = 1; lCols <= spQueryResults->GetColumns(); lCols++)
{
// You do not need to call SysFreeString when using the smart
// BSTR pointer (_bstr_t). The copy operator of the _bstr_t
// performs a free on any prior value and then handles the
// new data, preventing a memory leak when used in this loop.
//
// For complete details, see the definition of the _bstr_t
// copy coperator in COMUTIL.H.
bstrColName = spQueryResults->GetColumnName(lCols);
// Assuming ANSI build for sample so casting as char*.
strncpy(strDisplayData, (char *)bstrColName, _MAX_COL);
printf(_MAX_COL_FMT, strDisplayData);
}
printf("\n");
// Display the data.
for (long lRows = 1; lRows <= spQueryResults->GetRows(); lRows++)
{
for (long lCols = 1; lCols <= spQueryResults->GetColumns(); lCols++)
{
bstrRowInfo = spQueryResults->GetColumnString(lRows, lCols);
strncpy(strDisplayData, (char *)bstrRowInfo, _MAX_COL);
printf(_MAX_COL_FMT, strDisplayData);
}
// End of row.
printf("\n");
}
printf("\nRows processed (%ld)", spQueryResults->GetRows());
} // End of FOR loop processing a result set.
}
catch(_com_error pCE)
{
vDisplayError(pCE);
}
}
Top




