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

📄 pinfo.cpp

📁 从vc连接到oracle数据库。包括连接
💻 CPP
字号:
/*****************************************************************************
@author  Chandar
@version 1.0
Development Environment        :  Visual C++ 6.0
Name of the File               :  PInfo.cpp
Creation/Modification History  :
                                  07-Aug-2001     Created


 PInfo.cpp : implementation file

This file includes the implementation for the class used for the PInfo 
dialog box. It defines the function to handle dialog box events. This dialog 
box is shown when the user selects 'Show Product Information ' in the start 
up dialog box of the application. 
It displays the information in MSFLexgrid controls in two categories- Orderable
and Under Development
*******************************************************************************/

#include "stdafx.h"
#include "olemr.h"
#include "PInfo.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPInfo dialog


CPInfo::CPInfo(CWnd* pParent /*=NULL*/)
	: CDialog(CPInfo::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPInfo)
	//}}AFX_DATA_INIT
}


void CPInfo::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPInfo)
	DDX_Control(pDX, IDC_MSFLEXGRID2, m_flexgrid2);
	DDX_Control(pDX, IDC_MSFLEXGRID1, m_flexgrid1);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPInfo, CDialog)
	//{{AFX_MSG_MAP(CPInfo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPInfo message handlers

/*******************************************************************************
This function is called just before the dialog box is displayed to intialize the
dialog box and its components.
Here product names  are retrieved from database and shown in list box.
********************************************************************************/

BOOL CPInfo::OnInitDialog() 
{
	CDialog::OnInitDialog();

	//Set the width of each column of MSFlexgrid controls
	 m_flexgrid1.SetColWidth(0,2600);
   	 m_flexgrid1.SetColWidth(1,1135);
     m_flexgrid1.SetColWidth(2,2500);
	 m_flexgrid1.SetColWidth(3,1030);
	 m_flexgrid2.SetColWidth(0,2600);
   	 m_flexgrid2.SetColWidth(1,1135);
     m_flexgrid2.SetColWidth(2,2500);
	 m_flexgrid2.SetColWidth(3,1030);
	
	 //Define DBHandler object to call database functions
    DBHandler obj;
	IMultipleResults*    pIMultipleResults      = NULL;  //pointer to get multiple rowsets
	IUnknown* pUnkOuter=NULL;
    long   cRowsAffected=0;
	IRowset*  pRowset = NULL; 
	HRESULT hr;

	//Get the product information using stored procedure from database
	//into recordset object p_data. It contains data as two sets of records
	//one for Orderable products and other for Under Development products
	pIMultipleResults = obj.GetProductInformation();

	int RecordsetNo=1;   //variable to hold the no of recordsets retrieved
 do
   {
	  //Get a set of rows from returned data 
     if(SUCCEEDED(hr = pIMultipleResults->GetResult(pUnkOuter, 0,IID_IRowset,
                                                    &cRowsAffected, (IUnknown**)&pRowset)))
	 if(pRowset!=NULL)  //if rows are retrieved
	 {
			 
       //Call the funcion to display the result set.
       ProcessResultSet(pRowset,RecordsetNo); 
        
	   RecordsetNo +=1;
	   
	   //Release interface pointer
       pRowset->Release();
	 }

   }while(hr == S_OK );          //loop till next recordset is retrieved
	 obj.pIMultipleResults->Release();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}



/*******************************************************************	
  This function retrieves and displays data contained in the IRowset
  pointer into MSFlexgrid Control
*******************************************************************/
void CPInfo::ProcessResultSet(IRowset*  pIRowset,int RecordsetNo)
{
  
    IColumnsInfo*     pIColumnsInfo=NULL;
	DBCOLUMNINFO*     pDBColumnInfo;
	IAccessor*        pIAccessor   =NULL;
	
    DBBINDING*        pBindings;
	HACCESSOR         hAccessor;
    
    ULONG             ConsumerBufColOffset=0;
    ULONG             lNumCols,j,lNumRowsRetrieved;
    WCHAR*            pStringsBuffer;
    HROW              hRows[5];
    HROW*             pRows = &hRows[0];
    HRESULT hr;
    char*             pBuffer;  //Consumer buffer to hold the row data

  

   
    //Obtain access to the IColumnInfo interface, from the Rowset object.
    //to get column information  
    hr = pIRowset->QueryInterface(IID_IColumnsInfo, 
                                 (void **)&pIColumnsInfo);
    if(FAILED(hr))
    {
        cout << "Failed to get IColumnsInfo interface.\n";
    } //end if

    //Retrieve the column information.
    pIColumnsInfo->GetColumnInfo(&lNumCols, 
                                 &pDBColumnInfo, 
                                 &pStringsBuffer);

    //Free the column information interface.
    pIColumnsInfo->Release();

    //Create a DBBINDING array.
    pBindings = new DBBINDING[lNumCols];

    //Using the ColumnInfo structure, fill out the pBindings array.
    for(j=0; j<lNumCols; j++) {
		 
		//Ordinal position of column
        pBindings[j].iOrdinal = j+1;
		
		//Set the offset position in buffer for this column value
        pBindings[j].obValue = ConsumerBufColOffset;
        pBindings[j].pTypeInfo = NULL;
        pBindings[j].pObject = NULL;
        pBindings[j].pBindExt = NULL;

		//Retrieve only the value of column
        pBindings[j].dwPart = DBPART_VALUE;

		//Memory to be owned by client
        pBindings[j].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
        pBindings[j].eParamIO = DBPARAMIO_NOTPARAM;

		//Max lenth of column 
        pBindings[j].cbMaxLen = pDBColumnInfo[j].ulColumnSize;
        pBindings[j].dwFlags = 0;
        
		//The data type of the column retrieved
		//If it is numeric convert to string for displaying
		if ( pDBColumnInfo[j].wType==DBTYPE_NUMERIC)
			pBindings[j].wType=DBTYPE_STR;
		else
            pBindings[j].wType = pDBColumnInfo[j].wType;
        
		//Set precision of value after decimal point
        pBindings[j].bPrecision = pDBColumnInfo[j].bPrecision;
        pBindings[j].bScale = pDBColumnInfo[j].bScale;
        
        //Compute the next buffer offset.
       ConsumerBufColOffset = ConsumerBufColOffset + pDBColumnInfo[j].ulColumnSize;
    };

    //Get the IAccessor interface.
    hr = pIRowset->QueryInterface(IID_IAccessor, (void **) &pIAccessor);

    if(FAILED(hr))
    {
        cout << "Failed to obtain IAccessor interface.\n";
    }
    
	//Create an accessor from the set of bindings (pBindings).
    pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA,
                                lNumCols,
                                pBindings,
                                0,
                                &hAccessor,
                                NULL);
                                               
       
    //Get a set of 5 rows.
    pIRowset->GetNextRows(
                        NULL,
                        0,                  //Offset to return rows from
                        5,                  //No of rows to get
                        &lNumRowsRetrieved, //No of rows actually returned
                        &pRows);         

    //Allocate space for the row buffer.
    pBuffer = new char[ConsumerBufColOffset];

    //Display the rows.
    while(lNumRowsRetrieved > 0) // while no of row returned is greater than zero
	{
       //For each row, add the column data to list box
       for(j=0; j<lNumRowsRetrieved; j++) 
	   {
           //Clear the buffer.
           memset(pBuffer, 0, ConsumerBufColOffset);
        
		   //Get the row data values.
           pIRowset->GetData(hRows[j], hAccessor, pBuffer);
        		
		   CString productname=&pBuffer[pBindings[0].obValue];
		   CString category = &pBuffer[pBindings[1].obValue];
		   CString suppliername = &pBuffer[pBindings[2].obValue];
           CString price = &pBuffer[pBindings[3].obValue];
            
		   //if it is first recordset display in first Flexgrid control
		   if (RecordsetNo==1)
		   {   
			  //Add one new row to Flexgrid control
              m_flexgrid1.SetRows(m_flexgrid1.GetRows() +1);
 
		      //Make the newly added row  the current row
              m_flexgrid1.SetRow(m_flexgrid1.GetRows() -1);

			  //Display the data in flexgrid control
	          m_flexgrid1.SetTextMatrix(m_flexgrid1.GetRow(),0,productname);
	          m_flexgrid1.SetTextMatrix(m_flexgrid1.GetRow(),1,category);
	          m_flexgrid1.SetTextMatrix(m_flexgrid1.GetRow(),2,suppliername);
	          m_flexgrid1.SetTextMatrix(m_flexgrid1.GetRow(),3,price);
		   }

		   //if it is second recordset display in second Flexgrid control
		   if (RecordsetNo==2)
		   {   
			  //Add one new row to Flexgrid control
              m_flexgrid2.SetRows(m_flexgrid2.GetRows() +1);
 
		      //Make the newly added row  the current row
              m_flexgrid2.SetRow(m_flexgrid2.GetRows() -1);

			  //Display the data in flexgrid control
	          m_flexgrid2.SetTextMatrix(m_flexgrid2.GetRow(),0,productname);
	          m_flexgrid2.SetTextMatrix(m_flexgrid2.GetRow(),1,category);
	          m_flexgrid2.SetTextMatrix(m_flexgrid2.GetRow(),2,suppliername);
	          m_flexgrid2.SetTextMatrix(m_flexgrid2.GetRow(),3,price);
		   }
          
       
       }; //for.

       //Release the rows retrieved.
       pIRowset->ReleaseRows(lNumRowsRetrieved, 
                              hRows, 
                              NULL, 
                              NULL, 
                              NULL);
        
	   //Get the next set of 5 rows.
       pIRowset->GetNextRows(NULL,
                              0,
                              5,
                              &lNumRowsRetrieved,
                              &pRows);
    };  //while lNumRowsRetrieved > 0.

    //Free up all allocated memory.
    delete [] pBuffer;
    pIAccessor->ReleaseAccessor(hAccessor, NULL);
    pIAccessor->Release();
    delete [] pBindings;
} 

⌨️ 快捷键说明

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