Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFC VC++
How to get SQL server Name (Not SQL Instance Name) Programmatically From VC++ ,MFC
Project. Please Help
Posted 10-Jan-13 0:45am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
adityarao31 at 10-Jan-13 22:58pm
   
Very Sorry But I did not able to find out How to find server name from MFC using this articles
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I found Santosh Rao article on Code Project It self ,From That I was able to find servers on local machine.Here is Code
LPCTSTR pszInputParam = _T("Driver={SQL Server}");
	LPCTSTR pszLookUpKey = _T("SERVER:Server=");
	SQLHENV       hSQLEnv;
	SQLHDBC       hSQLHdbc;
	short		  sConnStrOut;
	BOOL		  bReturn = FALSE;
	//Allocate the environment handle
	SQLRETURN m_iRetcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hSQLEnv);
	if (m_iRetcode == SQL_SUCCESS || m_iRetcode == SQL_SUCCESS_WITH_INFO)
	{
		//Set the environment attribute to SQL_OV_ODBC3
		m_iRetcode = SQLSetEnvAttr(hSQLEnv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
		if (m_iRetcode == SQL_SUCCESS || m_iRetcode == SQL_SUCCESS_WITH_INFO) 
		{
		    //Allocate a connection handle
			m_iRetcode = SQLAllocHandle(SQL_HANDLE_DBC, hSQLEnv, &hSQLHdbc);
			if (m_iRetcode == SQL_SUCCESS || m_iRetcode == SQL_SUCCESS_WITH_INFO) 
			{
				CString szConnStrOut;
				//Call SQLBrowseConnect for additional information
				SQLSetConnectAttr(hSQLHdbc,SQL_COPT_SS_BROWSE_SERVER,_T("(local)"), SQL_NTS);
				m_iRetcode = SQLBrowseConnect(hSQLHdbc, (SQLWCHAR *)pszInputParam, SQL_NTS,
						 (SQLWCHAR *)(szConnStrOut.GetBuffer(MAX_RET_LENGTH)), MAX_RET_LENGTH, &sConnStrOut);
				szConnStrOut.ReleaseBuffer();
				//if the look up key is found
				//fill in the result set
				int iFind = szConnStrOut.Find(pszLookUpKey);
				if(iFind != -1)
				{
					CString szLookUpKey = pszLookUpKey;
					szConnStrOut = szConnStrOut.Mid(iFind+szLookUpKey.GetLength());
					iFind = szConnStrOut.Find('{');
					if(iFind != -1)
					{
						szConnStrOut = szConnStrOut.Mid(iFind+1);
						iFind = szConnStrOut.Find('}');
						if(iFind != -1)
						{
							szConnStrOut = szConnStrOut.Left(iFind);
							FillupStringArray(szConnStrOut,szArray);
							/*for(int i = 0;i< szArray.GetSize();i++)
							{
								ObjCommon.ShowMessage(szArray.GetAt(i));
							}*/
							bReturn = TRUE;
						}
					}
				}	
 
				SQLDisconnect(hSQLHdbc);
			}
			SQLFreeHandle(SQL_HANDLE_DBC, hSQLHdbc);
		}
		SQLFreeHandle(SQL_HANDLE_ENV, hSQLEnv);
	}
	CString serverName;
	if(szArray.GetSize() > 0)
	{
		serverName = szArray.GetAt(0);
		if(serverName.GetLength() < 2 && szArray.GetSize() > 1)
		{
			serverName = szArray.GetAt(1);
		}
		else
		{
			if(serverName.GetLength() < 2)
			{
				ObjCommon.ShowError(_T("SQL Server Not Installed"));
				return 0;
			}
		}
		if(serverName == _T("SQLEXPRESS"))
		{
			DWORD   dwComputerSize = MAX_COMPUTERNAME_LENGTH + 1;
			TCHAR   charComputer[MAX_COMPUTERNAME_LENGTH + 1];
			GetComputerName(charComputer, &dwComputerSize);
			CString csComputer(charComputer);
			serverName = csComputer + _T("\\\SQLEXPRESS");
		}
	}
	else
	{
		ObjCommon.ShowError(_T("SQL Server Not Installed"));
				return 0;
	}
	ObjCommon.ShowMessage(serverName + _T("  Final Server Name"));
	return serverName;
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 338
1 _Amy 290
2 Maciej Los 260
3 Manfred R. Bihy 200
4 OriginalGriff 195
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,347
2 Maciej Los 3,799
3 Peter Leow 3,558
4 CHill60 2,702


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100