VC++SQLデータベースに接続し、対応するクエリー内容を取得

4347 ワード

先日VC++を使って、SQLの中の内容を取得して保存して、あまり熟知していないため、多くの回り道を歩いて、今まとめて、必要な人にいくつか助けを与えることができることを望みます.
 
直接コードを貼ります.
#include "string"
#include "map"

//  SQL   
#import "C:\Program Files\Common Files\System\ado\msado15.dll"no_namespace rename("EOF","ADOEOF")

using namespace std;

//        
void XXXDlg::YourFun() 
{
	// TODO: Add your control notification handler code here
	::CoInitialize(NULL);
	_ConnectionPtr m_pConnection("ADODB.Recordset");
	_RecordsetPtr m_pRecordset("ADODB.Recordset");
	_CommandPtr m_pCommand;

	//       ,  ini    ,filepath ini     
	//ini    :
	//[Info]
	//Database = driver={SQL Server};Server=     ip  (    127.0.0.1),1433;Database=     ;
	//name =      
	//pwd =        
	//filepath1 = F:/
	//filepath2 = .txt
	CString Database;
	GetPrivateProfileString("Info", "Database", "", Database.GetBuffer(MAX_PATH), MAX_PATH, filepath);
	
	//           ,       
	_bstr_t strConnect = Database;
	
	//     
	try
	{
		m_pConnection.CreateInstance(__uuidof(Connection));
		CString name;
		CString pwd;
		GetPrivateProfileString("Info", "name", "", name.GetBuffer(MAX_PATH), MAX_PATH, filepath);
		GetPrivateProfileString("Info", "pwd", "", pwd.GetBuffer(MAX_PATH), MAX_PATH, filepath);
		_bstr_t name1 = name;
		_bstr_t pwd1 = pwd;
		HRESULT ConnectResult = m_pConnection->Open(strConnect,name1,pwd1,adModeUnknown);
		if(SUCCEEDED(ConnectResult))
		{
			MessageBox("       !");
		}
		
	}
	catch(_com_error e)
	{
		MessageBox("       !");
		return;
	}
	
	//        
	try
	{
		_bstr_t CommandText = "select a.  1,b.  2,b.  3,b.  4 from   1 a,  2 b where     ";

		//m_pConnection->Execute(CommandText, NULL, adCmdText);
		m_pRecordset.CreateInstance(__uuidof(Recordset));
		m_pRecordset->Open(CommandText, m_pConnection.GetInterfacePtr(), 
			adOpenDynamic, adLockOptimistic, adCmdText);

		_variant_t   1Result;
		_variant_t   2Result;
		_variant_t   3Result;
		_variant_t   4Result;

		//         ,   txt   
		//    ini            
		CString filepath1;
		CString filepath2;
		GetPrivateProfileString("Info", "filepath1", "", filepath1.GetBuffer(MAX_PATH), MAX_PATH, filepath);
		GetPrivateProfileString("Info", "filepath2", "", filepath2.GetBuffer(MAX_PATH), MAX_PATH, filepath);
		string m1 = filepath1.GetBuffer(0);
		string m2 = filepath2.GetBuffer(0);
		CString m3 = m1.c_str();
		CString m4 = m2.c_str();		
	
		//    map    ,             
		map > filename;
		//      ,   
		while(!m_pRecordset->ADOEOF)
		{		
			  1Result = m_pRecordset->GetCollect("  1");
			CString   1Results;
			  Results=(LPCSTR)_bstr_t(  1Result);
			CString fileEnd = m3 +   1Results + m4;
		
			//      1                
			CStdioFile* pfile = NULL;	
			if(filename.find((LPCSTR)fileEnd)!=filename.end())
			{
				pfile = filename[(LPCSTR)fileEnd].get();
			}
			else
			{
				pfile = new CStdioFile(fileEnd,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
				filename[(LPCSTR)fileEnd] = auto_ptr(pfile);
			}

			  2Result = m_pRecordset->GetCollect("  2");
			CString   2Results;
			  2Results=(LPCSTR)_bstr_t(  2Result);	
			pfile->SeekToEnd();//        
			pfile->WriteString(  2Results);//    	
			pfile->WriteString("\t");//       talbe   
				
			  3Result = m_pRecordset->GetCollect("  3");
			CString   3Results;
			  3Results=(LPCSTR)_bstr_t(  3Result);
			pfile->SeekToEnd();//        
			pfile->WriteString(  3Results);	
			pfile->WriteString("\t");
				
			  4Result = m_pRecordset->GetCollect("  4");
			CString   4Results;
			  4Results=(LPCSTR)_bstr_t(  4Result);
			pfile->SeekToEnd();//        
			pfile->WriteString(  4Results);	
			pfile->WriteString("\r
");// , m_pRecordset->MoveNext(); // , } // map >::const_iterator map_it = filename.begin(); while(map_it!=filename.end()) { (map_it->second)->Close(); map_it++; } filename.clear(); //MessageBox(s); } catch(_com_error e) { return; } }

才疎学浅、もし間違いがあれば、海涵を望んで、ははは.