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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()
1 E6 D: H, J- ?( @{+ t1 G9 K) J6 h2 s  A( Y* s" v  X& }
        _ConnectionPtr pLocalConnectPtr;3 Y) a# M  D2 ~6 r$ b
        _RecordsetPtr  pRstPtrTollRate;9 N+ {1 J' |; I3 y
        _RecordsetPtr  pRstPtrSplitWastebook;9 Q0 O( N# r& e! L
) ~5 S, }2 p8 k+ u4 j
        char pcSQL[300];
: M, r* K% |0 C3 e7 y6 k8 P  J        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 ",
# {- S" A( t4 K' e" k5 t) N                g_ModelEntryInfo.m_PassCard.nEntryStationID,/ r! q6 s" s  T4 Q8 H* ?8 K
                g_ModelEntryInfo.m_PassCard.nFlagStation1,
& O0 Y: R& O% F+ I                g_ModelEntryInfo.m_PassCard.nFlagStation2,
6 Q- G6 z1 w( v# s0 U  V$ ]6 I                g_ModelNodeInfo.m_nStationID,
4 Y' W" L. s( M1 h5 G' ~+ B2 Q                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
$ G0 C4 T3 \. A) W0 C                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase# C% T. {6 ?( Q. V, l0 i: q; B
                );
( I. u  }2 D2 {# c* B3 P$ o! X+ T( N! o# Y$ U5 H# u6 S( j
        try
2 e; p/ W4 p5 W; x9 X% r# ]$ h/ u        {
: b+ N0 k7 h6 [8 ]% `                pLocalConnectPtr.CreateInstance(__uuidof(Connection));) v+ ?7 D5 t. `: N: R1 k  w3 T& B/ E0 Y
                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);4 Z. x: m4 |* i! y2 U6 s+ y) C* b
               
6 d7 `! H/ d5 |9 t                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));' }8 r! |' L! d$ B- |" {: l& X0 {
                pRstPtrTollRate->Open(pcSQL,: d7 z& s9 [% p8 K
                        pLocalConnectPtr.GetInterfacePtr(),
; m8 b0 ?: w* _) B1 t. y: S: P                        adOpenStatic,
% A' L* P2 [& W; w                        adLockPessimistic,
  |( b+ o& n; f: F; z# W) Z7 P, ]2 B2 W- N                        adCmdText);; g# s( h* T' x! v
                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));( ~; h2 }; u6 E: \9 e$ X
                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),
6 ~: n! T, T- L" c; S% W                        pLocalConnectPtr.GetInterfacePtr(),
& r, B9 O8 t7 B0 B. F' N                        adOpenUnspecified,
5 F; i8 |8 ~# _7 O" `9 Q                        adLockPessimistic,$ N3 W* d, n* _: h% D6 Q4 u
                        adCmdTable);
3 z1 h. g5 q/ l, P; G2 ]+ y2 v* ~7 D0 [9 @6 p7 s- r
                pRstPtrTollRate->MoveFirst();
$ ^5 m6 t: G% G9 P                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
/ L; C' q3 B. @0 `) C& X0 E9 X                {
+ n: Q  ^  a; A' \' Z1 ?                        pRstPtrSplitWastebook->AddNew();
4 K, }& F; z6 o3 K* x- m                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);
4 v9 \5 M* Y5 r0 M0 @                        1 t3 \& @* `( l3 W0 [/ P
                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);  X: ~- F' i4 Z2 q0 J
                        . X+ n) P  W' D4 X
                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);
7 Z4 ~4 e' [! c$ |' m- m: N  m                        9 A3 o3 ]' \" w
                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);( w% c& v6 y: c! E) ]3 _
                        1 X, s  P1 c4 B6 r" f7 _8 l1 g
                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);3 O4 \7 ^. B. T# C& j4 q
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
; C1 j- q2 m- C) m0 q; Y4 @  H                       
) g- ?3 d0 z3 z                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;
# |/ y& A0 r! m. B# u                        % r! D" Z+ M! S, d0 X
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;. o. g; ]" f% v; t: v
                        ) X7 ~3 r/ F, K; N
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;
' j# |8 L3 S! Z3 R/ E                       
  g: ^0 d4 K5 Q2 D1 E; ~                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);/ Y% w2 ~+ o" O- p. o
                       
4 [, w  Z, K- x6 V0 U5 ^                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;& b) B$ X* w& n( A" A! r
                       
7 K1 x* N; U  M  [" O5 `6 G1 q1 Z                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;
4 {$ ^9 r7 z9 m. n5 Y                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;( o: ~8 l6 C2 |, d8 U3 q
                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;
7 ^( a0 l: L) n8 n1 b; k                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;
* j$ P$ X. M9 T: J4 Y/ l3 E; X8 C                        3 c4 D' a& {0 P( w* X
                        pRstPtrSplitWastebook->Update();
8 S- f* \3 \9 Z' I1 K& b                        0 s0 O. |) ^4 E
                        m_nTableSplitTollWasteNOCount++;
& |& O( W9 Z# Z! k9 r                        pRstPtrTollRate->MoveNext();( X$ w! U( T2 }& c2 W" z+ X
                }               
5 @4 H8 h" t& n' f; {6 V                pRstPtrSplitWastebook->Close();
' W; E' ^0 Y0 T& u% n                pRstPtrTollRate->Close();
6 ?, a# J0 y4 ?. Z4 [8 j                pLocalConnectPtr->Close();( B  y- F) ]/ G% m
                m_nStatus = DB_OK;( p9 ~0 e: ]. j2 o: `+ K' J  @
                return(m_nStatus);       
' C0 k# V, I, a5 H" u4 D        }9 y1 c! c- y/ k% m
        catch(_com_error e). Z$ g. F3 Z7 @- u4 Q6 W# v1 l6 C
        {# n2 n( g, U% g/ `) @
                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());8 x3 n% w" p* m* n8 N  H
                g_ModelPrompt.Notify();        + x# T) S6 C/ T
                m_nStatus = DB_ERROR;
1 h1 T7 r! v2 M! _4 u. t                return(m_nStatus);       
5 @. s7 h) Z2 k, \( i* U" {        }
" D  C/ k$ K5 ~# k6 p1 e}; O+ q) m. H4 H6 [* G
  G# t0 q6 U% i7 Q

( A& N- _( @8 W
# P0 O' N; T: L* F$ S$ f1 _1 {& k6 Nfloat CObserverLaneDB::GetTollRate()8 a7 y5 ?5 W0 ?; t
{
6 j: t7 K2 V1 T% X8 y* [! h' Q$ C: y+ L- [4 V2 _
        _ConnectionPtr pLocalConnectPtr;
9 A4 e& D. B" @        _RecordsetPtr  pRstPtrTollRate;
' ^4 l# R2 d+ t) m( W" h  K& V        _RecordsetPtr  pRstPtrOrganInfo;
0 M& U2 O" |# {" A5 r3 X
( }% E) y4 D0 ]$ L2 A! B" P        float fToll = -1;
3 B2 f+ h, P+ Q! `7 V5 L        char pcSQL[300];
3 I* O& t1 x" x5 f( G        int  nSplitCount = 0;
4 F% f! I3 A: T: U& z- |! _        switch(g_ModelNodeInfo.m_nNodeType), L9 D$ U/ S. S% J* u
        {3 n! v8 B4 E/ w& _. f' Y% w
        case NODE_TYPE_ENTRY:
' \, o) k& d# m  ^                return(0);
7 W( ~" Q& a) z  Q) `                break;) _0 w" P' B7 U( P8 v
        case NODE_TYPE_EXIT:
5 b# o# _# X2 [+ W4 Q* ~- S        case NODE_TYPE_MAIN_LANE:( z- ?' }2 @5 Q: Q+ O. p
                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)
  j8 L% Y0 e; v                {
6 z& y! u2 g7 o$ ^4 j3 y                case VEHICLE_CASE_URGENT:
" N6 k' i- c( d0 l5 {7 M* l: y                case VEHICLE_CASE_ILLEGAL:  n/ T/ g1 y" m! L6 z
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:" p/ m2 l$ M) }" s8 R5 }0 ]
                case VEHICLE_CASE_MOTORCADE:
9 q3 D; H) Z! {* W% a                case VEHICLE_CASE_COMMUTATION_TICKET:
' @* O* Z3 K, B$ n8 r9 {                case VEHICLE_CASE_ETC:
5 T4 \2 `3 x1 [1 k                case VEHICLE_CASE_BAD_CARD:
- G3 \  @8 q; c/ a( w                case VEHICLE_CASE_NO_CARD:. |! o$ h. X" y$ }: c0 x7 D2 W+ g
                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",6 @  D9 X/ V: S
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
, C* [! o" H. p6 |  A                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);6 G  S! Y5 l' O: ^: ?7 c; d- y
                        break;1 c( f9 X5 ?; {
                default:
/ z8 e! z- o' Q- W. i6 P. ~                        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 ",
) t( c5 _$ Y- U( K% E, m* l7 p6 D                                g_ModelEntryInfo.m_PassCard.nEntryStationID,  P1 S2 o+ n5 U8 A. `6 ]6 Q! Y2 e
                                g_ModelEntryInfo.m_PassCard.nFlagStation1,
# l3 e' O1 i6 v1 L) f                                g_ModelEntryInfo.m_PassCard.nFlagStation2,+ p5 t; p4 ^- `1 h  c' p, A) }
                                g_ModelNodeInfo.m_nStationID,
# e: N. P% R8 q* A* H% X* C0 y                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
. h: v4 n" Q4 @                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
7 ]$ J# J3 s7 l$ x                        break;1 n% J) {4 f8 W$ b: z. |
                }
* }  F) v% X- |. Q1 e               
8 }0 @) ~% ?; _, q3 n* P                try
: _+ ]8 r. a& X. c& y                {8 _  T7 F5 W8 l2 i: d! B
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
7 `( g5 U& O5 o                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);' d% u) K" k- U. `0 p: c* u/ L* n
                       
/ p% I7 l$ E& h) `; g  a                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));- ~+ Q5 w4 M3 g+ _* H; ^
                        pRstPtrTollRate->Open(pcSQL,
& S: g9 N4 A5 e/ G: ]                                pLocalConnectPtr.GetInterfacePtr(),1 L, g9 I. K. @  a1 Z+ Y
                                adOpenStatic,
1 ?) P, V9 c8 t! x' ~. P3 V                                adLockPessimistic,* G& e8 s/ D( d+ h$ V# u7 @% r
                                adCmdText);. v; O4 L8 z# a: l2 T  f
                        if(pRstPtrTollRate->RecordCount > 0)* a+ H3 w0 s4 w9 z8 F
                        {
/ Z2 m7 y+ G) ~                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;) L9 n" ~8 a( w/ b
                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )
' _# k8 P& Q2 {& `' h                                {
: N7 Z7 J/ F3 u                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));
6 {& ?" ?+ Q* V9 O+ n  S$ n                                        pRstPtrTollRate->MoveFirst();5 h9 P8 I8 h6 W* N% G! S7 G+ I
                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
, q+ c0 b) x: t# _$ Q                                        {: {. O, L7 a! z2 k, v& m9 t' A3 y
                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);; I- b1 A1 t/ p
                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));* v# |% H: {( z$ U6 N; x9 a& b2 q
                                                pRstPtrOrganInfo->Open(pcSQL,
' x! B( D1 s, S' r' {                                                        pLocalConnectPtr.GetInterfacePtr(),
: G0 z0 E& s1 F  v                                                        adOpenStatic,- l5 X- R' W" ^( z- h
                                                        adLockPessimistic,+ m, u/ F- \2 Q; o
                                                        adCmdText);' p* ], y0 P1 i  g& n: ]3 [) E
                                                nSplitCount++;
3 u" v8 q* Z( B                                                char pcBuf[10];" N! S9 k1 U$ V* e( |4 F
                                                if(0 == nSplitCount%3)1 L7 M) Z; W0 ], U$ A3 V
                                                {% j, ]5 T5 M- g0 A8 d3 T. F
                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);- o7 f+ _- K5 a0 }9 C
                                                }else  D" T: A# r( \  X9 c4 {! h1 f
                                                {
, S0 q9 Z: q6 {3 l, N, D                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
6 F* c* l3 o5 ]9 s% I# i. G                                                }
: e1 Y$ F' Y6 J# z8 G  a                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);
. b0 B! e, ^. {9 F; p# G                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);
; l, |) ]3 O& c) U/ c                                                pRstPtrOrganInfo->Close();  ]1 m* x& }  o3 @6 I  p% O
                                                pRstPtrTollRate->MoveNext();! Q4 T" G9 X- s$ C; ^$ a
                                        }. Q) J8 B9 L  p: }) |4 a" Z: M1 Y
                                }) f3 ^6 z4 x4 Q2 P2 I, @3 v
                               
& A: X& w- C) ~1 u                        }else fToll = -1;) q" v* G) L, i, u
                        - I7 ^/ l6 _: y5 i$ R. M) R6 B/ e
                        pRstPtrTollRate->Close();
$ |6 g! k6 a% n, S                        pLocalConnectPtr->Close();
* }7 J3 w( J5 F& V+ q/ v( E                        m_nStatus = DB_OK;
' R' d# |% r( O" h% w' m                        return(fToll);" I6 f' K3 q. x
                }6 b6 o6 a! t* k
                catch(_com_error e)
( {1 N; h5 ]. y5 r* `                {
8 k' P: u% o- B, D- Y1 ]4 Y                        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());
+ ?3 {& B  B% e8 H$ ?/ `                        g_ModelPrompt.Notify();       
5 P& B7 t' f* Q+ k, V$ M' a                        m_nStatus = DB_ERROR;
. y! v8 z( H: F9 R& _9 _5 |& H                        return(fToll);       
9 J& j/ i( V2 Y                }6 b# T: b! l! r
                " O% f. U, ]$ n4 g+ q
                break;$ g# u" s, o& ^1 f& q
        case NODE_TYPE_OPEN:) {" ~2 k) {! R  P# R
                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",
0 w; n* L; \. m5 L$ c                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass' j( `6 x9 N. u
                        );' J* [: S7 s* y6 u: u. q
                try
! w! x9 g0 i* E" j                {
, T1 x4 O2 c  U2 n5 y& b                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
$ y' U' D  ~5 a  V: L; Z7 w                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
# @9 Q' {' |, s7 a                       
6 i6 x  ^4 z, `0 h. Y1 l                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
1 N9 T+ r1 f+ y( ]  }( @$ H                        pRstPtrTollRate->Open(pcSQL,
3 j  U+ {3 Q$ P8 |                                pLocalConnectPtr.GetInterfacePtr(),$ q; G' O/ T+ p" d2 e4 D
                                adOpenStatic,
. q& E) j4 D3 @. y1 o                                adLockPessimistic,
7 [: U) A4 e' ^+ P- a3 ]9 u- W0 E                                adCmdText);( v( ?, P8 M6 q, e/ q; `4 p" Z
                        if(pRstPtrTollRate->RecordCount > 0)  h* O* V. J9 [. ]: l/ W
                        {; g! ^( H, t# l! U
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;. E8 v$ n; h) N" O: n, ?4 C
                        }else fToll = -1;% U- [* q4 c1 y: O5 N3 }: f
                        ' h# V5 _% S* q3 M- u
                        pRstPtrTollRate->Close();
8 q4 ?% ~1 f3 L6 v  H# b                        pLocalConnectPtr->Close();
! `" R, K! e. _( r( V7 `                        m_nStatus = DB_OK;
; K: p2 f. n) o. g! |% D: p9 u& J                        return(fToll);. @5 B% h1 g2 d
                }
3 s$ E4 P" g9 \) K5 S                catch(_com_error e)
4 X. I" Z/ k! `0 i4 S                {
  N$ ]! ^/ }4 w- h& t                        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());! [- F% G) {. @, w! L! y
                        g_ModelPrompt.Notify();        ; }$ G% P6 R3 p4 @4 {6 k
                        m_nStatus = DB_ERROR;# A. o, b1 z' q6 ~/ V6 B7 r9 O
                        return(fToll);        1 Q/ N% _1 m( k# X. S: c! l' ]
                }
* x4 a& E+ g0 c9 s; i                break;4 V' o/ C9 M/ ~5 K  q$ X3 {# u
        default:
6 n7 t. Q3 ]# r. C$ g- x5 K$ U                m_nStatus = DB_ERROR;3 h" B, [% T& Z% R
                break;
1 h% ^7 J2 c2 ~4 {/ v  u        }
& y) h! y; v: A+ K2 {        return(fToll);       
' ^6 J) k6 U$ h- O/ F2 V. Z, Y}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-8-9 01:44 , Processed in 0.034476 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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