Click to See Complete Forum and Search --> : SQLDMO Error Connecting


R. Myers
November 28th, 2003, 11:50 AM
I am trying to connect to a SQL Server 2000 database using SQLDMO. The following code returns "FAILED" while it's equivalent in Visual Basic is able to connect just fine:

if FAILED(CoInitialize(NULL))
return FALSE;

LPSQLDMOSERVER2 m_pSQLServer = NULL;

HRESULT hr = CoCreateInstance(CLSID_SQLDMOServer2, NULL, CLSCTX_INPROC_SERVER, IID_ISQLDMOServer2, (LPVOID*)&m_pSQLServer);
if FAILED(hr)
return FALSE;

if (m_pSQLServer == NULL)
return FALSE;

m_pSQLServer->SetLoginTimeout(10);

hr = m_pSQLServer->SetLoginSecure(TRUE);
if FAILED(hr) return FALSE;
hr = m_pSQLServer->Connect((SQLDMO_LPCSTR)"RAY");
if FAILED(hr)
AfxMessageBox("FAILED");
else
AfxMessageBox("OK");

if (m_pSQLServer != NULL)
m_pSQLServer->Release();

CoUninitialize();

Can anyone tell me why this works in VB, but not C++?

mwilliamson
November 29th, 2003, 09:44 PM
Does "RAY" need to be a unicode string?

R. Myers
December 1st, 2003, 09:34 AM
I don't think so. I should've mentioned that the sample that comes with SQL Server connects as follows:

if FAILED(hr = pSQLServer->Connect(TEXT(""),TEXT("sa"),TEXT("")));

and that doesn't work either.

The default for the first parameter (server name) is NULL, so

if FAILED(hr = pSQLServer->Connect();

should also work for the local server, but it doesn't.

R. Myers
December 1st, 2003, 09:51 AM
Sorry, you were right. It did need to be UNICODE. It's working now. Thanks.