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

📄 adorswizardsheet.cpp

📁 一个简单的数据库程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ADORsWizardSheet.cpp : implementation file
//

#include "stdafx.h"
#include "ADORsWizard.h"
#include "ADORsWizardSheet.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CADORsWizardSheet

IMPLEMENT_DYNAMIC(CADORsWizardSheet, CPropertySheet)

CADORsWizardSheet::CADORsWizardSheet(CWnd* pWndParent)
	 : CPropertySheet(IDS_PROPSHT_CAPTION, pWndParent)
{
	// Add all of the property pages here.  Note that
	// the order that they appear in here will be
	// the order they appear in on screen.  By default,
	// the first page of the set is the active one.
	// One way to make a different property page the 
	// active one is to call SetActivePage().

	AddPage(&m_Page1);
	AddPage(&m_Page2);
	m_bIsConnectionOpen = FALSE;
	SetWizardMode();
	m_pConnection = NULL;
}

CADORsWizardSheet::~CADORsWizardSheet()
{
}


BEGIN_MESSAGE_MAP(CADORsWizardSheet, CPropertySheet)
	//{{AFX_MSG_MAP(CADORsWizardSheet)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDHELP, OnAbout)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CADORsWizardSheet message handlers



BOOL CADORsWizardSheet::OnInitDialog() 
{
	BOOL bResult = CPropertySheet::OnInitDialog();
	SetWizardButtons(0);
	GetDlgItem(IDHELP)->SetWindowText("About...");
	return bResult;
}

BOOL CADORsWizardSheet::GetDBConnection()
{
	if(m_bIsConnectionOpen)
		return TRUE;

	HRESULT hr = S_OK;
	IDataSourceLocatorPtr dlPrompt = NULL;

     try
     {
       // Instantiate DataLinks object.
       TESTHR(hr = dlPrompt.CreateInstance(__uuidof(DataLinks)) );

       // Prompt for connection information.
       m_pConnection = dlPrompt->PromptNew();

       // If connection object is NULL, user cancelled.
       if ( NULL == m_pConnection ) 
		   return FALSE;

		// Open connection (connection returned by DataLinks is just
		// a holder for the returned ConnectionString).
		TESTHR(hr = m_pConnection->Open( m_pConnection->ConnectionString, L"", L"", -1 ) ); 
		m_bIsConnectionOpen = TRUE;

	 }
     catch ( _com_error &ce )
     {
		// Notify user of any errors that result from
        // executing the query.
 		GenerateProviderError(m_pConnection);
        GenerateComError(ce.Error(), ce.Description());
		if(m_pConnection != NULL)
			m_pConnection.Release();

		m_bIsConnectionOpen = FALSE;
	   return FALSE;
     }

	return TRUE;
}

void CADORsWizardSheet::PostNcDestroy() 
{
	CloseDBConnection();
	CPropertySheet::PostNcDestroy();
}

BOOL CADORsWizardSheet::FillListOfTables()
{
	CWaitCursor wait;
	_RecordsetPtr rs = NULL;

	CListBox * pListBox = &m_Page2.m_listboxTables;
	pListBox->ResetContent();

    try
	{
		// Get a list of tables and dump list to console.
		rs = m_pConnection->OpenSchema( adSchemaTables );

		while ( !rs->EndOfFile)
		{
			pListBox->AddString((char*) (_bstr_t) rs->Fields->Item[L"TABLE_NAME"]->Value );
			rs->MoveNext();
		}
    }
    catch ( _com_error &ce )
    {
		// Notify user of any errors that result from
        // executing the query.
        // Pass a connection pointer accessed from the Recordset.
        GenerateProviderError(m_pConnection);
        GenerateComError(ce.Error(), ce.Description());

		CloseDBConnection();
		return FALSE;
    }

	return TRUE;
}

void CADORsWizardSheet::GenerateComError(HRESULT hr, PWSTR pwszDescription)
{
	CString strError;

	strError.Format("Run-time error '%d (%x)'", hr, hr);
	strError += "\n\n";
	strError += pwszDescription;

	AfxMessageBox(strError);
}


void CADORsWizardSheet::GenerateProviderError(_ConnectionPtr pConnection)
{
 	if(pConnection == NULL)
		return;

   // Print Provider Errors from Connection object.
    // pErr is a record object in the Connection's Error collection.
    ErrorPtr  pErr = NULL;
	CString strError;
    if( (pConnection->Errors->Count) > 0)
    {
        long nCount = pConnection->Errors->Count;
        // Collection ranges from 0 to nCount -1.
        for(long i = 0; i < nCount; i++)
        {
            pErr = pConnection->Errors->GetItem(i);
            strError.Format("\t Error number: %x\t%s", pErr->Number,
                pErr->Description);
			AfxMessageBox(strError);

        }
    }
}


void CADORsWizardSheet::CloseDBConnection()
{
	if(m_bIsConnectionOpen)
	{
		HRESULT hr = S_OK;
		try
		{
			TESTHR(hr = m_pConnection->Close() );
			m_pConnection.Release();
			m_bIsConnectionOpen = FALSE;
		}
		catch ( _com_error &ce )
		{
			// Notify user of any errors that result from
			// executing the query.
			// Pass a connection pointer accessed from the Recordset.
			GenerateProviderError(m_pConnection);
			GenerateComError(ce.Error(), ce.Description());
		}
	}

}

BOOL CADORsWizardSheet::GenerateFile()
{
	CWaitCursor wait;

	if(m_strTableName.IsEmpty())
		return FALSE;


    // Define ADO object pointers.
    // Initialize pointers on define.
    // These are in the ADODB::  namespace
    _RecordsetPtr   pRecordset  = NULL;
    FieldsPtr  fldLoop = NULL;    
	FieldPtr pField = NULL;

	CString strTableName(m_strTableName);
	if(strTableName.Find(_T(' ')) != -1 ) 
		strTableName = _T("[") + strTableName + _T("]");

	_bstr_t bstrTableName( strTableName);

    //Define Other Variables
    HRESULT  hr = S_OK;
    _variant_t Index;
    Index.vt = VT_I2;

	CString strMACRO;
	CString strAttributes;
    try
    {
        
        TESTHR(hr = pRecordset.CreateInstance(__uuidof(Recordset)) );
        TESTHR(hr = pRecordset->Open( bstrTableName, _variant_t((IDispatch *)m_pConnection,true), adOpenForwardOnly,
            adLockReadOnly, adCmdTable) );

        fldLoop = pRecordset->GetFields();

        for (short i = 0; i < fldLoop->GetCount(); i++)
        {
            Index.iVal=i;
			CString strType;
			pField = fldLoop->GetItem(Index);
			CString strClassAttributes;
			CString strClassMACRO;

			CString strName;
			strName.Format("%s", (LPSTR)fldLoop->GetItem(Index)->GetName());
			strName.Remove(' '); //Remove spaces
			BuildClassBindingString(pField, strName,i , strClassAttributes, strClassMACRO);
			strAttributes += strClassAttributes;
			strMACRO += strClassMACRO;

        }

        // Clean up objects before exit.
        pRecordset->Close();
    }
	catch ( _com_error &ce )
	{
		// Notify user of any errors that result from
        // executing the query.
 	   GenerateProviderError(m_pConnection);
       GenerateComError(ce.Error(), ce.Description());
		return FALSE;

	}

	CString strOutputFile;

	CStdioFile fOutput;
	CFileException e;

	if(m_strDir.IsEmpty())
	{

		strOutputFile = m_strFileName;
	}
	else
	{
		if(m_strDir.Right(1) != "\\")
			strOutputFile = m_strDir + "\\" + m_strFileName;
		else
			strOutputFile = m_strDir + m_strFileName;
	}
	
	//Open file
	if(!fOutput.Open( strOutputFile, CFile::modeCreate|CFile::modeWrite, &e))
	{
#ifdef _DEBUG   
		afxDump << "File could not be opened " << e.m_cause << "\n";
#endif   

		return FALSE;
	}

⌨️ 快捷键说明

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