Loading...

SQL Server and Database Enumerator

View: 1025    Dowload: 0   Comment: 0   Post by: naruto   Category: SQL Server   Fields: Other

Download  sql-server-and-database.rar (40.30 KB)

You need to Sign In to download the file sql-server-and-database.rar
If you do not have an account then Sign up for free here

It has the functions:

BOOL EnumerateSQLServers();
BOOL EnumerateDatabase(LPCTSTR pszSQLServer,LPCTSTR pszUserId,LPCTSTR pszPwd);
BOOL EnumerateDatabaseLanguage(LPCTSTR pszSQLServer,LPCTSTR pszUserId,LPCTSTR pszPwd);

to perform this task. It fills in its result into the CStringArray m_szSQLServersArray, m_szSQLServerDatabaseArray, and m_szSQLServerLanguageArray data members respectively.

The heart of this class uses the function SQLBrowseConnect that enables you to build upon an incomplete connect string.

Example of a connect string:

ODBC;Driver={SQL Server};SERVER=MYSQLSERVER;APP=MFCAPP;WSID=San;DATABASE=mydb;UseProcForPrepare=0; UID=san;PWD=123

Note: A connect string is used to establish a database connection using the CDatabase Open or OpenEx member functions.

Passing an incomplete connect string such as ">Driver={SQL Server};." would cause retrieval of a list of SQL servers. When passed to the SQLBrowseConnect it would retrieve of a list of SQL servers as the server information is missing in the connect string. By passing "Driver={SQL Server};SERVER=MYSQLSERVER; APP=MFCAPP; WSID=San;UID=san;PWD=123;UseProcForPrepare=0;" it would retrieve a list of databases since the database information is missing. The RetrieveInformation function in the CSQLInfoEnumerator class encapuslates this function.

The function SQLDisconnect has to be called at the end of the SQLBrowseConnect browsing operation completion.

The complete function RetrieveInformation is as follows.

//Allocate the environment handle
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
			m_iRetcode = SQLBrowseConnect(hSQLHdbc, (SQLCHAR *)pszInputParam, SQL_NTS, 
			                              (SQLCHAR *)(szConnStrOut.GetBuffer(MAX_RET_LENGTH)), 
			                              MAX_RET_LENGTH,&sConnStrOut);
			szConnStrOut.ReleaseBuffer();
			
			//if the look up key is foundfill in the result set
			SQLDisconnect(hSQLHdbc);
		}
		SQLFreeHandle(SQL_HANDLE_DBC, hSQLHdbc);
	}
	SQLFreeHandle(SQL_HANDLE_ENV, hSQLEnv);
}

The CSQLInfoEnumerator class requires linking with the odbc32.lib file.

The sample application attached uses the CSQLInfoEnumerator class to display the list of SQL Servers, databases and supported languages.

SQL Server and Database Enumerator

This sample helps you to enumerate the list of SQL Servers and the databases and languages that a particular SQL server has. The class CSQLInfoEnumerator encapsulates this functionality into it.

Posted on 06-05-2014 

Comment:

To comment you must be logged in members.

Files with category

  • Data Encryption in SQL Server using T-SQL Functions (ENCRYPTBYPASSPHRASE, DECRYPTBYPASSPHRASE & HASHBYTES)

    Data Encryption in SQL Server using T-SQL Functions (ENCRYPTBYPASSPHRASE, DECRYPTBYPASSPHRASE & HASHBYTES)

    View: 607    Download: 0   Comment: 0

    Category: SQL Server     Fields: Other

    Decade ago data was just an entity which helped business to operate smoothly. By then data was considered as some sort of business related information just stored in a database, which can be retrieved based on the demand/requirement as per the...

  • Building a Multi-Cloud Strategy

    Building a Multi-Cloud Strategy

    View: 398    Download: 0   Comment: 0

    Category: SQL Server     Fields: Other

    Today, few companies are in a position to leverage a single cloud for all their needs. Even as service providers add multiple services like email and collaboration to try to keep people in the ecosystem, companies need a multi-cloud strategy.

  • Insufficient access rights error when doing backup in SQL Server

    Insufficient access rights error when doing backup in SQL Server

    View: 740    Download: 0   Comment: 0

    Category: SQL Server     Fields: none

    All of sudden SQL Server Management studio gives me the following error message when I'm trying to browse for the backup file location.

  • Migrate MySQL to Microsoft SQL Server

    Migrate MySQL to Microsoft SQL Server

    View: 2864    Download: 0   Comment: 0

    Category: SQL Server     Fields: none

    This article describes a few simple steps in order to migrate MySQL into Microsoft SQL Server 2005. The technique is very easy, but useful if you plan to move your data from MySQL and upgrade it finally to a Microsoft SQL Server environment.

  • COUNT of DISTINCT Rows in SQL Server

    COUNT of DISTINCT Rows in SQL Server

    View: 1676    Download: 0   Comment: 0

    Category: SQL Server     Fields: Other

    SQL Server does not support COUNT(DISTINCT *). For example, the below query fails.

  • Finding SQL Servers on the Network

    Finding SQL Servers on the Network

    View: 1005    Download: 0   Comment: 0

    Category: SQL Server     Fields: Other

    I am a TSQL fanatic. The programs I code are highly dependent upon MS SQL stored procedures. I pay for this love of TSQL when a major overhaul of the system is necessary. Sometimes my code needs to be updated heavily in two places; client and...

  • SQL Server and Database Enumerator

    SQL Server and Database Enumerator

    View: 1025    Download: 0   Comment: 0

    Category: SQL Server     Fields: Other

    This sample helps you to enumerate the list of SQL Servers and the databases and languages that a particular SQL server has. The class CSQLInfoEnumerator encapsulates this functionality into it.

  • Dynamic Management Views [DMV] – A SQL Server 2005 Feature

    Dynamic Management Views [DMV] – A SQL Server 2005 Feature

    View: 989    Download: 0   Comment: 0

    Category: SQL Server     Fields: Other

    The DMVs; newly introduced in SQL Server 2005 gives the database administrator information about the current state of the SQL Server machine. These values will help the administrator to diagnose problems and tune the server for optimal performance....

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd