Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the server name using MS SQL ODBC

Status
Not open for further replies.

cwong1

Programmer
Sep 26, 2002
11
PH
Hi. Is there a way in order to get the Server name using MSSQL ODBC in Visual C++? Here's my code:

CString wstr_connect ;
wstr_connect.Format( "DSN=%s;UID=%s;PWD=%s", gstr_DBName, gstr_UserID, gstr_Password ) ;
CDatabase mydb ;
BOOL ret = mydb.OpenEx( wstr_connect, CDatabase::noOdbcDialog ) ;
CString info = mydb.GetConnect( ) ;

GetConnect only returns the database name, user ID, password and WSID. How do i get the server name?

Thanks in advance!
 
Try ::SQLGetInfo ODBC function directly. Get an ODBC handle from m_hdbc member of CDatabase.
Code:
SQLSMALLINT len;
::SQLGetInfo(mydb.m_hdbc,SQL_SERVER_NAME,name,sizeof name, &len)
Warning: I have no time to test this solution...
Good luck!
 
It worked!!! Thanks a lot ArkM!!! =))
 
Follow up question:

I got the server name now and I stored it in a char (size of 20) variable, say, g_server. But when I pass the g_server to the connect function, I get the error: cannot convert from 'char [20]' to 'const unsigned short *'. How do I solve this?

Here's my code:

char g_server[20] ; // which contains the server name
LPSQLDMOSERVER g_pSQLServer = NULL ;

// initialization here

g_pSQLServer->SetLoginTimeout (5);
g_pSQLServer->SetLoginSecure (TRUE);
SetCursor (LoadCursor (NULL, IDC_WAIT));
//g_pSQLServer->Connect( (L"(local)", (L""), (L""))) --> this is working
if FAILED(hr = g_pSQLServer->Connect( g_server, (L""), (L"")))
MessageBox(this, TEXT("Failed connecting to the SQL Server"), TEXT("Error"), MB_OK);
SetCursor (LoadCursor (NULL, IDC_ARROW));

I've tried storing the server name in SQLDMO_LPCSTR/TCHAR but still get the same error. HELP please...

Thanks in advance!
 
SQLGetInfo returns char (not wchar_t Unicode string). To all appearances your code wants Unicode string to connect. I think, you have alternatives:
1. Use SQLGetInfoW (Unicode version) function; pass wchar_t (Unicode) array to get name (but specify byte buffer length as for SQLGetInfo).
2. Try to convert accepted char name to Unicode string then pass it to connect function. Use MultiByteToWideChar function (from MSDN):
Code:
int MultiByteToWideChar(
  UINT CodePage,         // code page; CP_ACP in your case?
  DWORD dwFlags,         // character-type options
  LPCSTR lpMultiByteStr, // address of string to map
  int cchMultiByte,      // number of bytes in string
  LPWSTR lpWideCharStr,  // address of wide-character buffer
  int cchWideChar        // size of buffer
);
// MB_PRECOMPOSED for dwFlags...
// cchWideChar - size in characters, not in bytes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top