// orcl1Dlg.cpp : 实现文件
//
#include "stdafx.h"
#include "orcl1.h"
#include "orcl1Dlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordSet; //数据集指针
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// Corcl1Dlg 对话框
Corcl1Dlg::Corcl1Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(Corcl1Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void Corcl1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(Corcl1Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &Corcl1Dlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &Corcl1Dlg::OnBnClickedButton2)
//ON_LBN_SELCHANGE(IDC_LIST1, &Corcl1Dlg::OnLbnSelchangeList1)
ON_LBN_SELCHANGE(IDC_LIST2, &Corcl1Dlg::OnLbnSelchangeList2)
ON_BN_CLICKED(IDC_BUTTON3, &Corcl1Dlg::OnBnClickedButton3)
END_MESSAGE_MAP()
// Corcl1Dlg 消息处理程序
BOOL Corcl1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void Corcl1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void Corcl1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR Corcl1Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
inline void TESTHR(HRESULT x) {
if (FAILED(x)) {
_com_issue_error(x);
}
}
void CountX(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);
void PrintProviderError(_ConnectionPtr pConnection){
ErrorPtr pErr=NULL;
if((pConnection->Errors->Count)>0){
long nCount=pConnection->Errors->Count;
for(long i=0;i<nCount;i++){
pErr=pConnection->Errors->GetItem(i);
printf("Error number:%x\t%s\n",pErr->Number,pErr->Description);
}
}
}
void PrintComError(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("Error\n");
printf("\tCode=%081x\n",e.Error());
printf("tCode meaning=%s\n",e.ErrorMessage());
printf("\tSource=%s\n",(LPCSTR) bstrSource);
printf("\tDescription=%s\n",(LPCSTR) bstrDescription);
}
void Corcl1Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
_ConnectionPtr userconn;
_RecordsetPtr pRstEmployee=NULL;
char buffer[500];
HRESULT hr=S_OK;
_variant_t Index;
Index.vt=VT_I2;
int j=0;
//_bstr_t strCnn("Provider=OraOLEDB.Oracle.1;Data Source=orcl1;"
//"Persist Security Info=False;User Id=system;Password=123456;");
_bstr_t strCnn("Provider=OraOLEDB.Oracle.1;Persist Security Info=False;Data Source=orcl1;");
try{
//open recordset with data from employee table
userconn.CreateInstance(__uuidof(Connection));
TESTHR(userconn->Open(strCnn,"system","123456",NULL));
TESTHR(pRstEmployee.CreateInstance(_uuidof(Recordset)));
strcpy(buffer,"select count(*) aa from TABLE1");
pRstEmployee->Open(buffer,_variant_t((IDispatch *)userconn,TRUE),adOpenStatic,adLockOptimistic,adCmdText);
std::vector<std::string>group_id,member_id,product_id; // 动态字符串数组来存储数据
std::string query = "SELECT GROUP_ID, MEMBER_ID,PRODUCT_ID FROM TABLE1";
while(!pRstEmployee->EndOfFile)
{
_variant_t group_id,member_id,product_id;
group_id=pRstEmployee->GetCollect(_variant_t("GROUP_ID"));
member_id=pRstEmployee->GetCollect(_variant_t("MEMBER_ID"));
product_id=pRstEmployee->GetCollect(_variant_t("PRODUCT_ID"));
pRstEmployee->MoveNext();
}
pRstEmployee->Close();
userconn->Close();
}
catch(_com_error &e){
//nodify user of any errors that result from executing the query
_variant_t vtConnect=pRstEmployee->GetActiveConnection();
switch(vtConnect.vt){
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured");
break;
}
}
}
void Corcl1Dlg::OnLbnSelchangeList2()
{
// TODO: 在此添加控件通知处理程序代码
CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST2);
if (pListBox != NULL)
{
CString strValue;
//strValue.Format(_T("%d"), group_id); // 将变量转换为字符串
// 使用AddString()方法将字符串添加到ListBox中
pListBox->AddString(strValue);
}
}
报错:
1>orcl1Dlg.obj : error LNK2001: 无法解析的外部符号 "public: void __cdecl Corcl1Dlg::OnBnClickedButton1(void)" (?OnBnClickedButton1@Corcl1Dlg@@QEAAXXZ)
1>orcl1Dlg.obj : error LNK2001: 无法解析的外部符号 "public: void __cdecl Corcl1Dlg::OnBnClickedButton3(void)" (?OnBnClickedButton3@Corcl1Dlg@@QEAAXXZ)
1>C:\Users\Administrator\Desktop\orcl1\x64\Debug\orcl1.exe : fatal error LNK1120: 2 个无法解析的外部命令