المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : MSSQL Database get database



C++ Programming
12-15-2009, 04:24 AM
Hi all

I am using Ms SQL Server 2005.I try to get all databases names of MSSQL.I am able to connect with mssql through this code.

#import ".\sqldmo.dll" no_namespace
#include "afxwin.h"
_SQLServer2Ptr spSQLServer;
CString str1="",str2="",str3="",m_MHOST="";
LPWSTR HOST;
HRESULT hr;
if SUCCEEDED(hr = CoInitialize(NULL))
{
try
{

if (SUCCEEDED(spSQLServer.CreateInstance(__uuidof(SQLServer2))))
{
try
{
HOST=(_bstr_t)m_MHOST;


spSQLServer->LoginSecure = TRUE;
spSQLServer->Connect((_bstr_t)m_MHOST);



str1.Format(_T("Connected to %s"), (LPTSTR) spSQLServer->Name);


}
catch(_com_error pCE)
{


str2.Format(_T("%s"),(TCHAR*)pCE.Description());
MessageBox(str2,"Message");
return ;
}
}
else
{
str3.Format(_T("Unable to create the SQL Server object.\n"));

}
}
catch(_com_error pCE)
{
str2.Format(_T("\n%s Error: %ld\r\n%s\r\n%s\r\n"),
(TCHAR*)pCE.Source(),
pCE.Error(),
(TCHAR*)pCE.Description(),
(TCHAR*)pCE.ErrorMessage());
AfxMessageBox(str2);
return;
}


}
else
{
str2.Format(_T("Call to CoInitialize failed.\n"));
}

i try to get database name through this way.

QueryResultsPtr Qry;
CString dbs="SELECT * FROM SYS.DATABASES";
Qry=spSQLServer->ExecuteWithResults((_bstr_t)dbs);

but i am not able to get database name.Please help me