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

📄 ff.cpp

📁 vc++连接oracle数据库
💻 CPP
字号:
// ff.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "ff.h"
#include "ffDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFfApp

BEGIN_MESSAGE_MAP(CFfApp, CWinApp)
	//{{AFX_MSG_MAP(CFfApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFfApp construction

CFfApp::CFfApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance

	AfxOleInit();///初始化COM库

	m_pConnection=NULL;
	m_pRecordset=NULL;
	m_lenofFeild=0;
	m_lenofRecord=0;

}


/////////////////////////////////////////////////////////////////////////////
// The one and only CFfApp object

CFfApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CFfApp initialization

BOOL CFfApp::InitInstance()
{
	AfxEnableControlContainer();
	
    CString	sqlString="SELECT L1_VALUE,L2_VALUE, L3_VALUE FROM MA_NVAP where ROWNUM < 10";
	GetConn(sqlString);
	CString *testString=new CString[3];
	testString=GetFieldName(3);
	float *testfloat=new float[9];
	CString testFieldName=testString[1];
	testfloat=GetData(testFieldName);

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CFfDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

//DEL _Connection CFfApp::m_pConnection()
//DEL {
//DEL 
//DEL }

void CFfApp::GetConn(CString sqlString)
{
	
	HRESULT hr;
	try
	{
		hr=m_pConnection.CreateInstance("ADODB.Connection");
		if (SUCCEEDED(hr))
		{
			hr=m_pConnection->Open("Provider=MSDAORA.1;Password=" ";User ID=...;Data Source=...;Persist Security Info=True","","",adModeUnknown);
			/*Provider=MSDAORA;Data   Source=serverName;User   ID=userName;   Password=*/
		}
	}
	catch (_com_error e) 
	{
		CString errormessage;
		errormessage.Format("连接数据库失败,\r\n错误信息:%s",e.ErrorMessage());
		AfxMessageBox(errormessage);
	}

	m_pRecordset.CreateInstance(__uuidof(Recordset));

	m_pRecordset->CursorType=adOpenStatic;
	m_pRecordset->CursorLocation=adUseClient;//客户端的游标设置,使用GetRecordCount()
	//sqlString="SELECT L1_VALUE,L2_VALUE, L3_VALUE FROM MA_NVAP where ROWNUM < 10";
    m_pRecordset->Open(variant_t(sqlString),_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);


	m_lenofRecord=m_pRecordset->GetRecordCount();//记录数长度

	m_lenofFeild=m_pRecordset->Fields->Count;//字段列长度
}
int CFfApp::GetFeildNum()
{
	return m_lenofFeild;
}

CString* CFfApp::GetFieldName(int m_lenofFeild)
{
	CString *array=new CString[m_lenofFeild];
	CString arrayi="";

	
	//string array[m_lenofFeild]="";
	for (long i=0;i<m_lenofFeild;i++)
		{
		arrayi=LPCTSTR(m_pRecordset->Fields->GetItem(_variant_t(i))->GetName());
		array[i]=arrayi;
		arrayi="";
		}	
	return array;	
}



float* CFfApp::GetData(CString fieldName)
{
	_variant_t var;
	float *floatData=new float[m_lenofRecord];

	try
	{
		if (!m_pRecordset->adoEOF)
		{
			m_pRecordset->MoveFirst();
		}
		else
		{
			AfxMessageBox("数据为空");
			return NULL;
		}
		int i=0;
		while(!m_pRecordset->adoEOF)
		{
			var = m_pRecordset->GetCollect(_variant_t(fieldName));
			if (var.vt==VT_BSTR||var.vt==VT_LPSTR||var.vt==VT_LPWSTR||var.vt==VT_NULL||var.vt==VT_EMPTY)
			{
				AfxMessageBox("所选择的字段属性为非数值类型或表内数据为空");
				return NULL;
			}
			floatData[i++] = float(var);
			m_pRecordset->MoveNext();
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_pRecordset->MoveFirst();///移到首条记录
	return floatData;

}

⌨️ 快捷键说明

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