找回密码
 注册
搜索
查看: 3667|回复: 0

[收藏]一个数据库操作例子

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook(): a# |0 b; W8 s0 p
{
5 g7 n& \2 C( r; r8 |- Z        _ConnectionPtr pLocalConnectPtr;
7 l" \1 N4 N! j- V- @* P        _RecordsetPtr  pRstPtrTollRate;
# k$ V0 k& b) d/ y: a2 y, E        _RecordsetPtr  pRstPtrSplitWastebook;
# k* i7 b* ]! A. r3 H( g3 c$ R) Z5 I, X% H4 T
        char pcSQL[300];; ^0 @) Q, @8 R( K: N0 ^+ v
        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Entry_Station_ID = %ld and Flag_Station1 = %ld and Flag_Station2 = %ld and Exit_Station_ID = %ld and Vehicle_Class = %ld and Vehicle_Case = %ld ",
) t7 S4 g) s. M! L) J/ O/ v                g_ModelEntryInfo.m_PassCard.nEntryStationID,
, Z, H* x2 S% W/ g2 k                g_ModelEntryInfo.m_PassCard.nFlagStation1,
' T0 \7 H" Q) P) ~5 b  D                g_ModelEntryInfo.m_PassCard.nFlagStation2,
" p, x" `5 ]- }2 _                g_ModelNodeInfo.m_nStationID,
, p4 r8 s9 S5 x' K  A5 x% x                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
3 |% u7 k! g# ~, T; ~                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase' g- g9 m' E: m& v4 d( `% \1 c5 y* ^' s
                );- Z, w9 o9 o7 w$ |% Y# F" F
- e* `2 h! h) H" ]1 V$ Z
        try6 q# l1 \8 u/ b: K
        {' T# d# w. h8 W7 Y; Y! J5 W
                pLocalConnectPtr.CreateInstance(__uuidof(Connection));
% F5 ^/ o" K# ^' `                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);; _9 ^' B, ~. |' |! n
                - H& f1 d9 {* G
                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
/ v" F( |5 C- p2 h                pRstPtrTollRate->Open(pcSQL,) q/ g  U7 [1 l0 T6 F* G
                        pLocalConnectPtr.GetInterfacePtr(),
" r0 w  W' S. B8 ?: s                        adOpenStatic,6 B6 s7 p# O4 r$ T
                        adLockPessimistic,
2 b% p& q8 n% D& p                        adCmdText);
% U5 ]& S! r* k                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));
% b; i  l9 w; N1 d6 g6 t                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),1 n3 U0 J! e# W/ B( l: y3 D" f+ |
                        pLocalConnectPtr.GetInterfacePtr(),
% l6 e3 Q- C. a/ g* b# Z3 M  I                        adOpenUnspecified,0 N6 k4 x! l# r: |8 c% x/ h
                        adLockPessimistic,1 A! j% K, B  u$ X( U( f; p
                        adCmdTable);
% \2 I) `; C" a& W2 `6 D4 V' t
. _. N- o) ?) s# j                pRstPtrTollRate->MoveFirst();* w' m+ y, X* O& m% g6 L
                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
4 R0 f$ M, m# }                {
  X2 x1 Q: Z( Q/ M# j" s                        pRstPtrSplitWastebook->AddNew();# x) ^( c2 @9 Y! D3 d
                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);/ @& z, d% F" v$ Z$ ^" q
                       
4 j8 C! B4 S2 p- \7 l                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);) B9 w/ d( G( @% r
                       
' K! u- ~( c% ~7 r6 l8 g                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);/ S$ q! a" Q- \8 u' V$ t: Z
                        ' ?' Q' \  `, ?
                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);- X. x3 o  Z% e5 d
                        9 K1 L: h3 m& I9 X
                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);3 p+ R7 v' S5 ]3 e: X+ r
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
) f1 H! e. o; d                        % Z/ E# e8 U/ a
                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;: a0 E6 o, B: o, N
                        ' O0 \: K/ L% x+ c& B- u
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;3 P3 Q( v' |) n3 C9 s$ r
                        # C! v+ y- J, }8 n3 ^1 y: l3 Q
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;! k+ o8 x" [4 z4 o
                       
- _4 K+ n2 F& W                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);
6 }  h& D5 J7 M  N  ?$ Y                       
+ j7 y6 [6 m) ]2 v8 y                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;  _3 s; \; W) p2 M
                       
8 J: q& b1 v9 N) a2 R6 w                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;
! H8 e& \- q. C2 z                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;# l$ Y7 [) X7 I6 L" H8 g
                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;
1 Y( g* i( ]: y8 F# h7 A                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;& C+ F1 r4 J! }; b& ?( n' w
                       
# ~+ s7 I. e, Q: Z+ t$ E                        pRstPtrSplitWastebook->Update();
. F( Q1 X+ |: Q) w0 A" @: p& Y                       
2 }5 l, |  B8 m( S                        m_nTableSplitTollWasteNOCount++;
, a/ u4 Q. l! R6 o! J% d: w                        pRstPtrTollRate->MoveNext();0 U3 v: p+ {  ^9 {3 K' h# l
                }                " b3 m6 \8 y) H; \5 X- l6 I
                pRstPtrSplitWastebook->Close();0 ?: E, L# q% Y9 T, K
                pRstPtrTollRate->Close();
$ U) L  E+ N* H( O                pLocalConnectPtr->Close();
6 X; u7 v7 q9 C4 h( K7 f                m_nStatus = DB_OK;
! I$ x4 `* X# T1 P/ l3 g5 H$ D                return(m_nStatus);       
) K& B5 H, b" t8 P  }, ]        }
* I( ^* P' }# T2 A  t+ B        catch(_com_error e)8 ]0 Y: g9 I4 |! X6 I$ i
        {: W2 J& p# }% ?& d! R
                g_ModelPrompt.strPrompt.Format("&Ecirc;&yacute;&frac34;&Yacute;&iquest;&acirc;&sup2;&Ugrave;×÷&acute;í&Icirc;ó&pound;&ordm;%s&pound;&not;%s\n&Ccedil;&euml;&Oacute;&euml;&Iuml;&micro;&Iacute;&sup3;&sup1;&Uuml;&Agrave;í&Ocirc;±&Aacute;&ordf;&Iuml;&micro;&pound;&iexcl;",e.ErrorMessage(),e.Description());
& x5 {' F6 w; H9 U- l- @2 M( x                g_ModelPrompt.Notify();       
  ^/ c5 w: t0 L                m_nStatus = DB_ERROR;
5 K/ a/ `  Z# r6 Q' q                return(m_nStatus);       
! m" J. ?# n5 f/ P/ @5 G        }- q" ~: L* j6 E* s% K4 T1 @
}
8 I6 O( m9 H8 R+ g
5 c/ K3 P5 ?3 S% Y* e
+ P6 j4 q; b$ l& e! ~/ w7 o: t1 ^  C  P9 h- H( N
float CObserverLaneDB::GetTollRate()" P9 K8 q2 ~0 c, J: r
{
$ J2 d# @1 ]7 T& P
; t% O2 t; h9 A% z        _ConnectionPtr pLocalConnectPtr;
2 t" T, H! D8 [- E0 O        _RecordsetPtr  pRstPtrTollRate;
1 U7 X# Q4 O7 d3 }' x        _RecordsetPtr  pRstPtrOrganInfo;
* j  y# w1 M8 o! \& o% R
3 i$ W) A1 ]9 |9 w        float fToll = -1;  F1 v! [2 ^0 C' B* R9 o. \
        char pcSQL[300];
$ g- {/ E6 O. N- d        int  nSplitCount = 0;& b5 N, e% l8 ?7 d" T2 K
        switch(g_ModelNodeInfo.m_nNodeType)* W1 Y; P+ J9 Y0 M( L/ u) z
        {- V+ {- O. e* N  W! A( R4 V
        case NODE_TYPE_ENTRY:) F( G  O! w' n+ E% p& G" R  B) k& P8 E
                return(0);% n6 q5 y4 d/ J2 \7 g( Q
                break;  {  p) b) n/ j' T  [  m, k( O
        case NODE_TYPE_EXIT:
( ^1 }$ F8 J7 ?* I4 D2 y: {        case NODE_TYPE_MAIN_LANE:
- Z+ j3 c8 s, y0 O7 `% }                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)
* k- I4 y% g2 N. W2 F                {
9 M9 F- A1 I, v. ^! T                case VEHICLE_CASE_URGENT:
8 b2 P7 e7 G6 q( [                case VEHICLE_CASE_ILLEGAL:" g( C% H$ ^7 H8 T0 }
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:! |2 K! D# U6 i+ |* R
                case VEHICLE_CASE_MOTORCADE:/ }1 ]" o' T. t$ C; u# T" |
                case VEHICLE_CASE_COMMUTATION_TICKET:; N7 h$ }$ ^' P( U
                case VEHICLE_CASE_ETC:
  Q( `! G& d0 ?; \                case VEHICLE_CASE_BAD_CARD:4 j# Y2 e( G8 ]5 }) q
                case VEHICLE_CASE_NO_CARD:
# ^5 @( W& Z2 G& o5 Q6 d                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",
& Y, C2 v, T; u3 S! u                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
2 S; `" g& a) ~3 Q                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);5 ?% @1 m6 f( Q" x- X  L( I
                        break;: n1 u/ J7 O' v( H; F
                default:3 R( ]$ p$ _4 \1 n3 i; T2 G
                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Entry_Station_ID = %ld and Flag_Station1 = %ld and Flag_Station2 = %ld and Exit_Station_ID = %ld and Vehicle_Class = %ld and Vehicle_Case = %ld ",( F; B, D/ E) k7 \; R0 u1 J
                                g_ModelEntryInfo.m_PassCard.nEntryStationID,
" h0 c$ R+ V+ g' b                                g_ModelEntryInfo.m_PassCard.nFlagStation1,; e8 {* H5 q8 i) C. K1 k
                                g_ModelEntryInfo.m_PassCard.nFlagStation2,
+ n2 \1 s5 }" R. _                                g_ModelNodeInfo.m_nStationID,
9 P# v( \0 q9 Z9 O- ?7 _; |: s( G- E                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
; b# i& n8 j% l% L                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
# R3 B$ w/ g( M3 [4 b7 ~- V                        break;
" m7 r" h5 V5 ~% J  _                }
+ B+ ?8 L- |  r               
& W5 ?  A# V- I" q                try& W6 E1 ^: k- Z
                {
% Y, U2 A: Q5 S# @  p, U0 N( B! _' w                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
0 D; _/ B3 z9 J1 n; x$ T                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);) f5 n# z9 F* Y7 U
                        & B9 O; D1 G' b8 g, ~! \2 R
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
/ `2 f, e' a& c5 {2 h, p                        pRstPtrTollRate->Open(pcSQL,
+ S# ~* D' S4 L6 m3 w# I# Y; U                                pLocalConnectPtr.GetInterfacePtr(),$ B9 ~3 s" N2 p9 c" G
                                adOpenStatic,
7 c7 [7 p) e& k0 a; H: S" V- C8 @                                adLockPessimistic,6 p: P3 u- A) ]! Z% D  k
                                adCmdText);- V! s1 [  d: u7 [! L
                        if(pRstPtrTollRate->RecordCount > 0)# t  C. q' c& F  |7 x1 h1 G
                        {
+ U4 b, t1 g$ |                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
* p0 d) g5 M$ R% F( Z8 F                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )( n8 N2 k4 P# x3 i& N
                                {
; m$ ~6 i; Y: f5 ^; @" N$ b0 E                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));
. T1 S' G8 w. F; [# j                                        pRstPtrTollRate->MoveFirst();, e& E; h" {; p8 p: H: ^- Z7 U
                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)* H+ ~) r0 ]0 Z/ `6 i9 k) X+ p
                                        {0 p0 C- {' V& s$ W0 ^
                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);
5 {  {# e: |* E8 o' m$ d) U                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));
5 @2 p& v6 G( P9 j& X4 O                                                pRstPtrOrganInfo->Open(pcSQL,
- ~+ f& D( z( F6 j: B' m; i                                                        pLocalConnectPtr.GetInterfacePtr(),
: q  }- B4 W% q2 S3 {: S                                                        adOpenStatic,3 \$ J7 [' Y; M- T+ [! P$ W
                                                        adLockPessimistic,5 j. S! d  ^1 G: N: w. v1 X7 c
                                                        adCmdText);* i0 D1 }+ r5 W$ m) b9 n! ^
                                                nSplitCount++;! \5 F( w: p+ G7 G2 |& U
                                                char pcBuf[10];
4 K: D1 n2 S/ o0 h) y                                                if(0 == nSplitCount%3)
$ Z$ |  c! v' W' c                                                {7 }- W0 h. f2 ~
                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);& z" X# A& u% z  K
                                                }else7 Q. l' x$ ~8 J9 `1 d1 V
                                                {
% H$ @7 ]/ i* S- `9 [                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
0 W8 i  J6 l, o8 a) l" W                                                }
# }6 N+ [/ g" z- V4 X                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);' U2 C! w6 `2 ~
                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);
% e! a( D) W. ~, }  u* i                                                pRstPtrOrganInfo->Close();
% W0 T* S& K3 l                                                pRstPtrTollRate->MoveNext();
, C! G" N) y" Z! C0 J: o                                        }
0 D& d% c6 z, Q( z& _                                }
7 K; G4 F6 A; g7 m! R6 l: F9 x$ S                                + x8 ]4 v  y' \' w- o% D' [
                        }else fToll = -1;
' I% }1 b0 B, @! v                       
% j+ v* c' W4 A                        pRstPtrTollRate->Close();3 X9 }/ s  Y; }0 C1 N  P0 d8 s
                        pLocalConnectPtr->Close();( I/ d0 ~/ K& r' s
                        m_nStatus = DB_OK;
, `$ B$ j) q# h5 G# R1 {                        return(fToll);
0 K7 Q5 a# O* f( f                }
, l. X& d' U3 @; B6 A& c& c                catch(_com_error e)
* @+ c* A0 |/ D' y0 {) e                {
* a' ~2 l& s9 `! G) \2 u                        g_ModelPrompt.strPrompt.Format("&Ecirc;&yacute;&frac34;&Yacute;&iquest;&acirc;&sup2;&Ugrave;×÷&acute;í&Icirc;ó&pound;&ordm;%s&pound;&not;%s\n&Ccedil;&euml;&Oacute;&euml;&Iuml;&micro;&Iacute;&sup3;&sup1;&Uuml;&Agrave;í&Ocirc;±&Aacute;&ordf;&Iuml;&micro;&pound;&iexcl;",e.ErrorMessage(),e.Description());
# Q4 u8 i$ J, x$ K5 T( G                        g_ModelPrompt.Notify();       
* {2 ~! d1 e: b7 j                        m_nStatus = DB_ERROR;% ^8 j6 P9 t/ G) z7 [
                        return(fToll);       
, ?) L" C, ]. T1 C/ [8 j0 C                }( L. }5 ^  u0 A4 _) M" ~
               
+ ]9 V& B9 w+ o2 t4 g                break;& D( H' V" l* d. c' M% z2 }
        case NODE_TYPE_OPEN:
( e; h" n, q- t+ I                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",
  t+ ^: m1 A1 ^3 N# I, o                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass
# W$ ~, B/ _3 Y$ b3 v' v                        );
& d2 k/ x' ^" l1 G, |: p3 U& V# M                try
* a7 c  _" e: c1 U3 D* `! Q                {; ?5 `1 M1 B2 k3 ]
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));  o0 S3 n- B2 W( R7 b( }2 j
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
& I6 P9 B$ s* p8 b' N; _                       
! O) o% N; P2 k                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
5 H; F$ s  ?- `5 r                        pRstPtrTollRate->Open(pcSQL,( b, m" c2 ]5 P8 l$ T# |
                                pLocalConnectPtr.GetInterfacePtr(),  R- ^; W: k6 n' \3 r3 A& O- J
                                adOpenStatic,
( s" {5 Q" t1 ]" `/ K                                adLockPessimistic,
& P3 o+ s1 I3 W- V                                adCmdText);& X: G. M0 U0 A/ r
                        if(pRstPtrTollRate->RecordCount > 0)4 H- G" T- i! Y8 R6 Q9 e
                        {; o, t( K, a! L# p; \% [
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
3 P* k& `1 ]7 K3 q. X. ~. I9 ~                        }else fToll = -1;
2 a' Y+ q, I/ r& I% x& a5 [                       
* B+ L" U. S7 F+ x$ z6 H% @$ @                        pRstPtrTollRate->Close();
: s/ ?! p# v- d& t                        pLocalConnectPtr->Close();
6 p  c7 l5 V) z. f( w, A) X. b  A8 J                        m_nStatus = DB_OK;
; K! D) [0 z3 ~" v1 }! g                        return(fToll);; D% N. E& r7 q$ J9 s7 I
                }
" n5 a/ G2 u' l+ E; @+ X7 w7 H                catch(_com_error e)6 R( t7 g( ^( @$ ?7 n( t$ t% i5 W
                {2 Z4 G8 x/ Z9 I
                        g_ModelPrompt.strPrompt.Format("&Ecirc;&yacute;&frac34;&Yacute;&iquest;&acirc;&sup2;&Ugrave;×÷&acute;í&Icirc;ó&pound;&ordm;%s&pound;&not;%s\n&Ccedil;&euml;&Oacute;&euml;&Iuml;&micro;&Iacute;&sup3;&sup1;&Uuml;&Agrave;í&Ocirc;±&Aacute;&ordf;&Iuml;&micro;&pound;&iexcl;",e.ErrorMessage(),e.Description());: s6 T! q* _' j8 k& h4 Z  V+ G
                        g_ModelPrompt.Notify();        $ ]' z4 H6 V# d, A3 {
                        m_nStatus = DB_ERROR;7 T) u4 C# f8 M* Z
                        return(fToll);       
1 _; x: X, ?8 E. c8 `# ?2 t                }6 ], ^  u* H, c% T1 C7 \- x5 ]' c, K
                break;, ?8 r, Z$ q3 o2 P  [
        default:$ d' ]- f' r1 x& }6 b7 a3 M
                m_nStatus = DB_ERROR;2 d% e( @! b& |) e$ j
                break;
3 x7 i: F) x& K0 l" T        }% X' b) z, Z" }
        return(fToll);        3 ~: M  b& r3 M& n, h  M
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|宁德市腾云网络科技有限公司 ( 闽ICP备2022007940号-5|闽公网安备 35092202000206号 )

GMT+8, 2025-8-9 04:28 , Processed in 0.034726 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表