collect unicode values using C++ code and ODBC calls

Discussion in 'C++' started by ketu0001, May 23, 2007.

  1. ketu0001

    ketu0001 New Member

    Joined:
    May 23, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    collect unicode values using C++ code and ODBC calls:
    Following is my code:
    SQL database contains both ascii and unicode values in the name field.
    I am not able to collect unicode values.
    Can anybody please help.

    Code:
    #define _UNICODE
    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sql.h>
    #include <sqlext.h>
    #include <sqlucode.h>
    #include <odbcss.h>
    #include<iostream.h>
    
    void main()
    {      
    
    SQLHENV			hEnv=NULL;
    SQLHDBC			hDBC=NULL;
    SQLRETURN		retcode;
    
    /* Convert the strings... */ 
    //WCHAR wszDSN[] = L"myDataSource";
    WCHAR wszDSN[] = L"myDataSource";
    WCHAR wszUID[] = L"sa";
    WCHAR wszPWD[] = L"bmcAdm1n";
    WCHAR wszSqlState[SQL_SQLSTATE_SIZEW] = L"";
    WCHAR wszMsg[200] = L"";
    
    
    SQLHSTMT	hStmt = NULL;// Statement handle
    WCHAR		szSqlStr[150]= L"select name ,default_database_name,default_language_name from sys.sql_logins" ;
    
    	cout<<"hEnv before SQLAllocEnv = "<<hEnv<<endl;
    	retcode=  SQLAllocEnv (&hEnv);
    	cout<<"retcode= "<<retcode<<endl;
    	cout<<"hEnv after SQLAllocEnv = "<<hEnv<<endl;
    	cout<<"\n***********************\n\n";
    				
    				
    	// Allocate memory for the connection handle
    	cout<<"hDBC before SQLAllocConnect = "<<hDBC<<endl;
    	retcode= SQLAllocConnect (hEnv, &hDBC);
    	cout<<"retcode= "<<retcode<<endl;
    	cout<<"hDBC after SQLAllocConnect = "<<hDBC<<endl;
        cout<<"\n***********************\n\n";
    
    
    
    	retcode = SQLConnect (hDBC, wszDSN, SQL_NTS,wszUID, SQL_NTS, wszPWD, SQL_NTS);
    		
    	
    	cout<<"retcode= "<<retcode<<endl;
    	
    	if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
    	{	
    		printf("\n------------Connected---------------\n");
    
    			// Allocate memory for the statement handle
    					cout<<"hStmt before SQLAllocStmt = "<<hStmt<<endl;
    					retcode = SQLAllocStmt (hDBC, &hStmt);
    					cout<<"retcode= "<<retcode<<endl;
    					cout<<"hStmt after SQLAllocStmt = "<<hStmt<<endl;
    					cout<<"\n***********************\n\n";
    						
    					
    					retcode = SQLExecDirect(hStmt,szSqlStr, SQL_NTS);
    					
    					WCHAR       name[10],defDBnm[10],defLanguage[10];
    					SDWORD      retcode2, cbname, cbdefDBnm, cbdefLanguage;
    				
    					
    					retcode2=NULL;
    					
    					if (retcode2 == SQL_SUCCESS)
    					{
    						retcode2 = SQLBindCol(hStmt, 1, SQL_C_CHAR, name, 10, &cbname);
    						retcode2 = SQLBindCol(hStmt, 2, SQL_C_CHAR, defDBnm, 10, &cbdefDBnm);
    						retcode2 = SQLBindCol(hStmt, 3, SQL_C_CHAR, defLanguage, 10, &cbdefLanguage);
    					}
    
    		cout <<"\n****************************************\n";
    		while (TRUE)
    		{
    			retcode2 = SQLFetch(hStmt);
    			
    			if (retcode2 == SQL_SUCCESS || retcode2 == SQL_SUCCESS_WITH_INFO)
    			{
    				if (cbname == SQL_NULL_DATA)               /* check null data */
    					printf("\nname: NULL\n");
    				else
    				{
    					printf("\n1Name: %s", name);
    					char strBuffer[20];					
    					memset (strBuffer, 0,sizeof(strBuffer));
    					WideCharToMultiByte(CP_UTF8, 0, name,-1, strBuffer, 20, NULL, NULL);					
    					printf("\nBuffer: %s\n", strBuffer);
    				
    				}
    				
    				if (cbdefDBnm == SQL_NULL_DATA)
    					printf("defDBnm name: NULL\n");
    				else
    					printf("defDBnm name: %s\n", defDBnm);
    				
    				if (cbdefLanguage == SQL_NULL_DATA)
    					printf("defLanguage: NULL\n");
    				else
    					printf("defLanguage : %s\n\n", defLanguage);
    			}									
    			else                         /* if no more data or errors returned */
    				break;  
    		}		
    		
    // Free the allocated statement handle
    		SQLFreeStmt (hStmt, SQL_DROP);
    		
    		// Disconnect from datasource
    		SQLDisconnect (hDBC);
    	}                
    	
        // Free the allocated connection handle
    	SQLFreeConnect (hDBC);
    	
        // Free the allocated ODBC environment handle
    	SQLFreeEnv (hEnv);           
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice