⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datalink.cpp

📁 visual c++ 实例编程
💻 CPP
字号:

#include <iostream.h>
#include <fstream.h>
#include <comdef.h>

#import "c:\program files\common files\system\ado\msado20.tlb" no_namespace rename("EOF", "IsEOF") rename("BOF", "IsBOF")
#import "c:\program files\common files\system\ole db\oledb32.dll" rename_namespace("oledb")

//Function ..... : inline void TESTHR( HRESULT hr )
//Purpose ...... : Inline HRESULT tester function that triggers a _com_error exception for us
//Parameters ... : HRESULT hr - The result to test for failure
//Returns ...... : Nothing
inline void TESTHR( HRESULT hr )
	{
	if( FAILED(hr) ) _com_issue_error( hr );
	}	

BOOL	ShowDataLink( _bstr_t* );
void main( )
	{

	fstream		iosCfg;
	char		lpsz_Buffer[ 512 ];
	_bstr_t		bstr_myConnectString="";

    //初始化COM
	if( FAILED(CoInitialize( NULL )) ) 
		{
		cout << "\nFailed to initialise COM";
		return;
		}
	memset( lpsz_Buffer, 0, 512 );

	iosCfg.open( "my.cfg", ios::in|ios::nocreate );
	
	if( !iosCfg.fail() )
		{
		iosCfg.getline( lpsz_Buffer, 512 );
		bstr_myConnectString = lpsz_Buffer;
		}
	
	iosCfg.close();
	
	if( ShowDataLink( &bstr_myConnectString ) )
		{
		iosCfg.clear();
		iosCfg.open( "my.cfg", ios::out );
		
		if( !iosCfg.fail() )
			{
			iosCfg << (char*) bstr_myConnectString << "\n";
			}
		
		iosCfg.close();
		}

	/*
	 *   We are done, so uninit COM
	 */
	CoUninitialize();
	}


//Function ..... : BOOL ShowDataLink( _bstr_t * bstr_ConnectString )
//Purpose ...... : Display the OLE DB Data Link properties dialog, using (if supplied) a 
//	        	 : pre-configured connection string.
//Parameters ... : bstr_ConnectString = A pointer to a _bstr_t that currently holds the 
// 			     : connection string. Upon completion, it will contain 
//				 : the new connection string.
//  Returns      : TRUE indicating OK was pressed to save the connection string.
BOOL ShowDataLink( _bstr_t * bstr_ConnectString )
	{

	HRESULT		hr;
	oledb::IDataSourceLocatorPtr	p_IDSL= NULL;			// This is the Data Link dialog object
	_ConnectionPtr					p_conn = NULL;			// We need a connection pointer too
	BOOL							b_ConnValid = FALSE;
	
	try
		{
	
		hr = p_IDSL.CreateInstance( __uuidof( oledb::DataLinks ));
		TESTHR( hr );


		if( *bstr_ConnectString == _bstr_t("") )
			{
			p_conn = p_IDSL->PromptNew();
			if( p_conn != NULL ) b_ConnValid = TRUE;
			
			}
		else 
			{
			p_conn.CreateInstance( "ADODB.Connection" );
			p_conn->ConnectionString = *bstr_ConnectString;

			IDispatch * p_Dispatch = NULL;
			p_conn.QueryInterface( IID_IDispatch, (LPVOID*) & p_Dispatch );
			
			if( p_IDSL->PromptEdit( &p_Dispatch )) b_ConnValid = TRUE;
			
			p_Dispatch->Release();
			}
		if( b_ConnValid )
			{
			*bstr_ConnectString = p_conn->ConnectionString;
			}

		}
	catch( _com_error & e )
		{
		cout << "\nCom Exception raised\n";
		cout << "Description : " << (char*) e.Description() << "\n";
		cout << "Message     : " << (char*) e.ErrorMessage() << "\n";		
		
		return FALSE;
		}

	return b_ConnValid;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -