C++用ADO连接SQL数据库出错
//类文件#pragma once
#include "iostream"
#include "string"
#include "vector"
//添加对ADO的支持
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
#include <NXOpen\Session.hxx>
#include "uf.h"
#include "uf_ui.h"
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <sstream>
#include <NXOpen/ListingWindow.hxx>
using namespace NXOpen;
using namespace std;
class LinkSQL
{
public:
LinkSQL(void);
~LinkSQL(void);
void OpenSQL();
void ShowInformation();
void CloseSQL();
private:
Session *theSession;
UI *theUI;
_ConnectionPtr pMyConnect;
_RecordsetPtr pRst;
stringstream ss;
};
LinkSQL::LinkSQL(void)
{
CoInitialize(NULL); //初始化COM环境
_ConnectionPtr pMyConnect(__uuidof(Connection));//定义连接对象并实例化对象
_RecordsetPtr pRst(__uuidof(Recordset));//定义记录集对象并实例化对象
theSession=Session::GetSession();
theUI=UI::GetUI();
}
LinkSQL::~LinkSQL(void)
{
}
void LinkSQL::OpenSQL()
{
try
{
/*打开数据库“SQLServer” */
pMyConnect->Open("Provider=SQLOLEDB; Server=192.128.28.32;Database=test; uid=sa; pwd=123;","","",adModeUnknown);
}
catch (_com_error &e)
{
ss<<e.ErrorMessage();
theUI->NXMessageBox()->Show("系统信息",NXMessageBox::DialogTypeError,ss.str());
ss.str("");
}
}
void LinkSQL::CloseSQL()
{
try
{
pRst->Close(); //关闭记录集
pMyConnect->Close();//关闭数据库
pRst.Release();//释放记录集对象指针
pMyConnect.Release();//释放连接对象指针
}
catch(_com_error &e)
{
ss<<e.ErrorMessage();
theUI->NXMessageBox()->Show("系统信息",NXMessageBox::DialogTypeError,ss.str());
ss.str("");
}
CoUninitialize(); //释放COM环境
}
void LinkSQL::ShowInformation()
{
ListingWindow *thwLW=theSession->ListingWindow();
thwLW->Open();
try
{
pRst = pMyConnect->Execute("select * from 表格1",NULL,adCmdText);//执行SQL: select * from gendat
}
catch(_com_error &e)
{
ss<<e.ErrorMessage();
thwLW->WriteLine(ss.str());
ss.str("");
return;
}
}
//执行文件
LinkSQL *theLinkSQL=new LinkSQL;
theLinkSQL->OpenSQL();
theLinkSQL->ShowInformation();
theLinkSQL->CloseSQL();
delete theLinkSQL;
哪个地方出错了,在控制台程序的时候没问题,在UG中就会出错
页:
[1]