Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFCVC++
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 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  
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

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,838
1 Tadit Dash 310
2 OriginalGriff 283
3 Sergey Alexandrovich Kryukov 260
4 Peter Leow 220
0 Sergey Alexandrovich Kryukov 9,440
1 OriginalGriff 5,618
2 Peter Leow 4,280
3 Maciej Los 3,540
4 Abhinav S 3,363


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