PLM之家PLMHome-工业软件践行者

[求助] C++用ADO连接SQL数据库出错

[复制链接]

2014-10-17 08:11:57 2991 0

xiao2076 发表于 2014-10-17 08:11:57 |阅读模式

xiao2076 楼主

2014-10-17 08:11:57

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

x
//类文件) d" [3 q% E3 D7 l
#pragma once. B' p3 i% i4 `+ }
#include "iostream"     
% i( m5 ?) f6 a. }#include "string"     " F! u8 D; Q, W" X% P( e2 A
#include "vector"     ! ]$ i' b) R4 ?3 b  D6 L7 H
5 T( _% C- x8 Q8 s
//添加对ADO的支持   
% L& J( B1 g9 P2 A#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
. g1 T6 M8 A( V( t#include <NXOpen\Session.hxx>
! q! h7 m% N5 d( s% I' F
/ U5 F" W, D1 T#include "uf.h", m3 E- @1 X/ V2 x& K4 W
#include "uf_ui.h"
& a+ d5 i# P0 d. n% P#include <NXOpen/UI.hxx>  Z3 {+ G& {' [
#include <NXOpen/NXMessageBox.hxx>! V/ A- U6 N0 p3 _
#include <sstream>3 X# r4 H. Q5 h$ V
#include <NXOpen/ListingWindow.hxx>; c: B7 {$ l; W- h! f5 c
using namespace NXOpen;
5 y& U; |4 L4 Z, {using namespace std;3 v+ A+ ?, ~9 g, X# x
class LinkSQL# ]* {% q" X: c4 h. `
{0 g; ]- L6 x: X
public:+ v* b6 A# I( T5 f
        LinkSQL(void);* f  t% c9 Y! s3 @" R9 z* Y3 H
        ~LinkSQL(void);4 n+ `1 c. C9 y6 W+ @) K6 A
        void OpenSQL();$ y( W4 d) p$ x1 Q( G; f$ s
        void ShowInformation();
" H# u& t! W" W4 o( ?2 o        void CloseSQL();
* Q) L3 n7 N; e' aprivate:3 Q/ Q. T) R" D+ j' I
        Session *theSession;
) F7 ], l  F1 s* j$ [/ x        UI *theUI;; `+ d7 M9 e# L8 o) Q0 a: U, g; ?
        _ConnectionPtr pMyConnect;
- l) F: j7 b0 ]) L! n- w        _RecordsetPtr pRst; : L1 g9 J# `3 Q5 u! g
        stringstream ss;8 p# S- y- v6 S, o/ ?' j& _
};
0 ~4 x: [4 W$ O3 l: [% B: k
3 M' Y. @8 Y0 x5 Y3 VLinkSQL:inkSQL(void)
: @: e' `! y* s% ~: [. V1 h  [{/ P: t* U$ i+ Y; }1 `# Q
        CoInitialize(NULL); //初始化COM环境              
) ^' m- Y9 [- ?: A        _ConnectionPtr pMyConnect(__uuidof(Connection));//定义连接对象并实例化对象    4 \. O  |3 G$ }5 D9 L; D, V* E# ?
        _RecordsetPtr pRst(__uuidof(Recordset));//定义记录集对象并实例化对象  
# T: B- s) U& ^0 O9 ~        theSession=Session::GetSession();
* w6 ?9 Z3 m4 D. E/ `# \& E3 a6 c        theUI=UI::GetUI();
9 X3 ?: t8 A/ o0 v) X, x, K. ^}
  Z! i: c# L; b8 K( r; u5 g/ c7 L& ~4 F' o; g) z7 \
3 ~, v- H9 g* v+ C; h3 n* f
LinkSQL::~LinkSQL(void)
, U* G9 x. r. a2 Q  c/ h{
) ]0 }. f* W1 d2 p" m* l6 A}; Y, k3 D2 S' D# t2 ]$ y5 Y
void LinkSQL::OpenSQL()
) g, t- t* u! J7 K1 C: E: g{% ]$ v% M' Q, r- N
        try             8 Q) m- I' O# W4 r
        {                . e8 o: f! k! A( L: j3 r! [& K4 i0 {$ t
       
5 r/ b/ R! ~, U* c# H                /*打开数据库“SQLServer” */               ! l. J4 ~: t! V8 _9 v
                pMyConnect->Open("rovider=SQLOLEDB; Server=192.128.28.32;Database=test; uid=sa; pwd=123;","","",adModeUnknown);  
7 R$ v# T" [$ s% {$ T/ D                    % ~( }# F  N4 ?: q5 {
        }   ! a! x5 C! x7 m4 ]
        caTCh (_com_error &e)            
* j; n9 f7 R$ M" u8 ]        {                # H, ^7 a: N* i5 w* _
                 
" \; z* Z+ R$ s8 I8 m8 Y                ss<<e.ErrorMessage();     
* q# ?5 c+ F+ N3 Q3 f/ z+ }: N                theUI->NXMessageBox()->Show("系统信息",NXMessageBox:ialogTypeError,ss.str());3 E; H7 _% _* d. [' E
                ss.str("");- s' s) ?# y! d! }, Z8 G: S+ W
                           
6 I! {& ]( i7 ~$ q9 `% h% e& v6 w8 P# L        }             & b: O: i* y& I$ L& X+ t
                         + S( ]( ^( i% p- D0 }1 O( z
}- d. G' U  ?2 r8 w- d
void LinkSQL::CloseSQL()- f6 ?' m) ^( Q$ h
{& c6 ^* c' |  `9 c/ l+ j
        try            
# `, j( A; A9 C% o2 h        {                 * V4 J; {# N0 o- l2 `) A9 F$ I# M! u
4 t' S, ]* z5 A# w2 g, O
                pRst->Close();     //关闭记录集                  : \( Q9 B8 b% f3 p& d3 S
                pMyConnect->Close();//关闭数据库                    M4 J$ Y% s5 D7 p$ i2 l; _
                pRst.Release();//释放记录集对象指针                  $ M( A# V+ o% ]* M9 I' y$ D0 m
                pMyConnect.Release();//释放连接对象指针   
4 c5 c. ^4 U" b6 V+ E0 b        }  : I! e1 q* x: j+ X. k& [
        catch(_com_error &e)            
% P5 d7 Y* V" I! f$ f2 F% S; `        {                 3 [% N, @- W  [* ?( @) V
                ss<<e.ErrorMessage();     
* ]) V" j/ S( s3 d# U                theUI->NXMessageBox()->Show("系统信息",NXMessageBox:ialogTypeError,ss.str());
# U$ K! R" C! S4 K# d+ e6 t/ I. S/ I                ss.str("");         
! [0 @+ x3 S# i        }                   & Q, h. y# Y& e4 b& S
       
2 Z  I# J3 X+ G( o( S; I        CoUninitialize(); //释放COM环境   % h7 _/ E5 {! X
}
4 u8 D9 N! S( D: l% X" Ivoid LinkSQL::ShowInformation()/ S9 y0 R, a6 S5 g. F4 L, d  D
{5 Q9 @, m3 b- q1 g0 `% ^; S

$ J; V, i- A7 H( J" X/ U        ListingWindow *thwLW=theSession->ListingWindow();5 z9 C9 ]' Z7 O) w) V. z
        thwLW->Open();
3 }' J& s4 q+ X1 D0 w: C/ K4 Y, K2 p9 S4 e, E9 p) b1 Z& N
        try, K  z0 A2 R- p
        {* N' d5 i3 x3 ^; o- [
                pRst = pMyConnect->Execute("select * from 表格1",NULL,adCmdText);//执行SQL: select * from gendat    ; U1 b% ^3 V, U0 u. A, j* b
        }/ a; b. S, d$ Q& I
        catch(_com_error &e)) B+ O7 [6 I! H( v+ K' b
        {& e+ T5 w7 v" f1 F" }, m% R. X1 _
        1 G9 `" h% r$ Y$ b6 e/ p+ o1 y5 A- q
                ss<<e.ErrorMessage();# o" `4 g0 K  V. c0 H
                thwLW->WriteLine(ss.str());
$ k( Q" `1 g8 l" O9 _/ j8 W                ss.str("");. T0 C5 S( d9 D% X
                return;
  b* j, p) e& f/ Z5 M        }
) N) h3 N, C5 f' {$ a}  m9 L8 ]5 C. C; j4 E3 ~) V
//执行文件/ U3 O- ]; w) }5 A
        LinkSQL *theLinkSQL=new LinkSQL;
: n+ E5 E' [# E, J        theLinkSQL->OpenSQL();% ?7 g0 o! N* h; b
        theLinkSQL->ShowInformation();
$ |; O4 I/ ^3 q/ b4 p" y* z0 P        theLinkSQL->CloseSQL();( B4 i2 a  @; Z
        delete theLinkSQL;. U; D$ \) J: p+ o. c! C
# U. |; f; g, @2 T. I$ Q' \. Y
哪个地方出错了,在控制台程序的时候没问题,在UG中就会出错
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com/ doTeam.tech
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 注册

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了