Monday, March 19, 2012

How to get Server Name? (Using VC++, MSSQL & ODBC)

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!I'm not using CDatabase at the moment, but check out this page:
http://www.cppdoc.com/example/mfc/classdoc/MFC/CDatabase.html

There is a function called GetConnectInfo(); maybe that can help you?|||I think it depends on the driver you are using. I think one problem you are having is that you are not actually providing a "DBName"; your Format function has a string with "DSN=" which I am nearly certain means that you are using a datasource. The server is specified in the datasource.

Note that the CDatabase class has a member m_hdbc; it can be used to get a handle for use with the SDK version of ODBC. Look at the sample code for CDatabase::m_hdbc; it might lead you to a good answer.|||Maybe I'm missing something, but couldn't you just use:SELECT @.@.servername-PatP|||Maybe I'm missing something, but couldn't you just use:SELECT @.@.servername-PatPI don't know how to do that in VC; cwong asked how to do it "using MSSQL ODBC in Visual C++".

No comments:

Post a Comment