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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()( B! F" f0 ?& i1 r3 F. s
{3 R$ l2 f# y7 b( y/ ~
        _ConnectionPtr pLocalConnectPtr;
4 \! `; E7 _2 \- O8 i% x        _RecordsetPtr  pRstPtrTollRate;
  B2 j4 f; E8 G$ N# l4 D+ h8 O4 k        _RecordsetPtr  pRstPtrSplitWastebook;
( u) W0 u4 |  B) R  j$ ~+ ~0 [6 Y& C% I
        char pcSQL[300];; k- c$ \: y) S! c
        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 ",! n5 D8 g- b, D7 Y! i  D1 E
                g_ModelEntryInfo.m_PassCard.nEntryStationID,
6 k  p% O7 U- Q5 C( m" _                g_ModelEntryInfo.m_PassCard.nFlagStation1,' x; i+ ^0 [! y& c
                g_ModelEntryInfo.m_PassCard.nFlagStation2,8 Z0 ^2 ?9 U* A! ^# L2 M, G- m
                g_ModelNodeInfo.m_nStationID,
0 i1 d! w9 n: X. K6 w                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,8 s( q. J8 W6 M
                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase! u( f* r* [2 k; V1 `7 c
                );
; J0 V5 \, ?0 m; H
" l0 V  W8 B2 H: d        try6 j+ X7 r8 k3 u
        {4 V; v$ a1 i3 O; L
                pLocalConnectPtr.CreateInstance(__uuidof(Connection));
) g5 M9 b; s, l2 X7 o/ K# e4 u                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);5 k* o5 P: H# J# r% J  L+ b
               
- U  \8 l) Z& T( b                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));& g: G- ^* ]2 ~+ T( h, Z
                pRstPtrTollRate->Open(pcSQL,# E( x9 S, a# Q1 I
                        pLocalConnectPtr.GetInterfacePtr(),
% Q5 }+ c+ F5 v& ?' ^* g- l6 {                        adOpenStatic,: V0 E" e% r/ A4 S- n- f
                        adLockPessimistic,* J% @  f- P9 F! `3 |4 p8 T
                        adCmdText);3 [6 j" \" R- h* O8 H+ L
                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));
" t8 E2 o& {1 y1 `/ \- H                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),: R5 W) w: z# Y2 h) N+ `
                        pLocalConnectPtr.GetInterfacePtr(),  U5 \/ ~0 h0 ]
                        adOpenUnspecified,
) r& ?7 ]1 e. C+ }" ?! }) O                        adLockPessimistic,+ C, N8 i' Q& b! n* O
                        adCmdTable);6 ^/ v9 n3 l# W. k( C* ?) _

9 i2 n- {+ s* |4 V                pRstPtrTollRate->MoveFirst();6 m8 c) a% [% a$ H9 H
                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)) Z# V0 P! T1 B; }/ ~! S* y
                {. `' g0 |- y1 [* z1 r
                        pRstPtrSplitWastebook->AddNew();1 q& [# M9 B& Z/ |; _
                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);, ^6 l/ C! X: o7 R$ @
                        ) F2 g3 a8 X' v$ P" t! v
                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);) C3 v! R  |3 I& [1 E( u3 y! w# M
                       
& o5 `: N1 F7 c# V- v, W( e: S                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);# h! f6 w3 v" i, K! V5 D- f
                        ; M6 w$ q! `0 |* I! d- H
                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);! |5 t0 [5 v' h; k& Z6 O3 t
                        3 ?! J$ i6 n: e! h* B
                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);
- ~6 I5 }8 c% [  B: B                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
5 \6 K$ U$ H5 O3 g3 G2 u                       
* k0 A0 v) x' \% x1 z6 l' [4 x5 P                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;( Q& Z3 D! Z: w; G# i2 ?
                        1 Y7 |' L3 T+ Y8 p# {0 i3 P1 E9 ?
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;
& u) O" D$ o9 X                       
% U$ K( T8 |& u) b' t0 r                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;
9 U" [3 U* E; }: C                       
+ j0 f% b; g( ?/ C                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);
; E1 t1 q, o- p4 F$ ?6 k                        ! Q7 @& X7 i: w. m7 l; Q% d
                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;( K* S0 o0 i2 r5 v0 g3 A
                       
5 |" m. U& m# e# W; L. o                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;
0 n# v( m- s! S2 G7 Y7 x# u                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;* c; `( C  y5 N5 C
                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;
- _. K2 l3 ^" Q  M  _% f                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;
0 ]) Y$ h8 R3 S                       
4 T+ T0 g$ I8 z: ?$ B0 m- b+ H: B                        pRstPtrSplitWastebook->Update(); ( X) j8 s: l8 @( x' P
                       
/ l4 Z9 U, p% `$ k9 G9 t* I                        m_nTableSplitTollWasteNOCount++;2 t& @8 B  W. u0 T7 n' S9 T9 H; {
                        pRstPtrTollRate->MoveNext();/ ^% D: Y, T. U; n
                }                2 Q2 ]( M/ y  _9 g) Y5 b2 @$ Q
                pRstPtrSplitWastebook->Close();- S' c3 h! c% {3 Y. y$ @9 v& y# j
                pRstPtrTollRate->Close();" N; N2 D8 o+ y4 W8 e9 c
                pLocalConnectPtr->Close();, k$ r: o' J8 L" Q0 }5 G  V
                m_nStatus = DB_OK;3 F, x0 q6 j/ k
                return(m_nStatus);        8 m' `  o& |+ b! V. b4 d
        }
9 C  Z/ {  `# W5 G8 W& Z        catch(_com_error e)
& n8 X" B% M9 n4 k/ i# y        {: T7 \5 E) b% 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());! K7 m& d0 p; {8 X0 ?1 c5 J
                g_ModelPrompt.Notify();        ) `- ^0 D% z; ?/ M
                m_nStatus = DB_ERROR;- O; W; _7 G# g% S7 _9 o0 R
                return(m_nStatus);        : t/ p, p" ^) D( G' a( k0 i
        }
! Z! Z2 z: k* c  P}
$ u# @* f" b) X+ e$ j6 S: }+ {
* W' P6 ]- C4 F! V+ H
' S+ b7 r$ k2 O  K9 C
float CObserverLaneDB::GetTollRate()
# O4 \! G8 k7 y- h9 E{
% L/ n- r1 ]/ P* G2 S, k% s: e& b+ ~( j3 `4 M  G- \
        _ConnectionPtr pLocalConnectPtr;3 e/ G! p. D+ l) V( \# U
        _RecordsetPtr  pRstPtrTollRate;
6 c! z( Y* e' m. Q( T/ {9 |7 U        _RecordsetPtr  pRstPtrOrganInfo;
( p) O" |( n. ?* h- G/ k+ h
6 t) M8 Z5 f' Y. J! j( _, t        float fToll = -1;
3 K4 V; K! d! E( R& T        char pcSQL[300];# i/ S. g4 \% j6 m2 S. B% k
        int  nSplitCount = 0;
7 X4 f2 F6 ]* o# h9 w" z, D( e        switch(g_ModelNodeInfo.m_nNodeType)4 u- @. b* p/ {' p  W
        {% O9 o, _$ Q5 a3 t5 G
        case NODE_TYPE_ENTRY:
9 q- h) A7 D4 Q4 N3 u' w                return(0);
, G5 A) }+ v0 q& w& t, H2 K+ b                break;) F7 I8 v: e, o; l
        case NODE_TYPE_EXIT:
! w7 _& ], @& D" D        case NODE_TYPE_MAIN_LANE:
  r& k8 n9 y: e8 i                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)
- b! a8 u6 L, g# b) T; }5 U6 {/ ^                {/ [0 d% F  A% |" z
                case VEHICLE_CASE_URGENT:
3 @4 @) v) I: ~2 S+ N                case VEHICLE_CASE_ILLEGAL:0 L& H% N# Z1 x% v( m
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:
9 o( l# T1 j! z7 p) ?# K8 H. z                case VEHICLE_CASE_MOTORCADE:# |/ P- W; @$ K
                case VEHICLE_CASE_COMMUTATION_TICKET:5 k7 A1 Z+ `+ e; D1 b
                case VEHICLE_CASE_ETC:8 K! b% r0 ~  m; m6 E
                case VEHICLE_CASE_BAD_CARD:( @  U0 R! m; u. m' u
                case VEHICLE_CASE_NO_CARD:
' B& O: m( S# R- l( l7 I1 t                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",% u$ e6 h$ z6 W1 f  A
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,6 L0 F; E. ?& ~: B/ r( p- G
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);! a7 c" V7 ~( ]. G6 X
                        break;
+ @& p$ l4 _* a: `                default:$ D  h! j6 }, x; }, o
                        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 ",
) H% {- [# [3 t/ W& A( L0 G                                g_ModelEntryInfo.m_PassCard.nEntryStationID,
3 z& V. M3 E7 \' v                                g_ModelEntryInfo.m_PassCard.nFlagStation1,
* @, j7 _/ u4 _( Z  n0 h' I$ t                                g_ModelEntryInfo.m_PassCard.nFlagStation2,& h. V: t" @! L: ]& Z1 _6 w
                                g_ModelNodeInfo.m_nStationID,
9 I# r+ }  d7 o9 a                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,' z5 F# v% @  {( Q' K$ I: @
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);; _. s, C- y; U* h. ]0 b5 m% G( S
                        break;
; W% p# P' f. b- A. ?                }1 {! T; a5 M8 F6 s; w
               
% u- G1 O- ^0 ^1 b                try5 v8 A- i( K3 D6 z% w9 Y
                {
9 k# l! a, \9 t6 ]" U4 U1 |7 ~                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
8 _9 X4 X  x# F( F. r                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);# |. M* `8 `$ N3 [5 _
                        & @  d1 J1 M( T. p6 K9 v3 W
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
! z% R9 c  w, b% b# h1 a                        pRstPtrTollRate->Open(pcSQL,% ?1 `% f$ [* k; j! P5 a! e
                                pLocalConnectPtr.GetInterfacePtr(),
& T; h8 J3 _$ [& ]3 u' }( k                                adOpenStatic,
  @' B; V' ^! f1 }' Z3 t                                adLockPessimistic,7 {9 @- }7 ^2 [; A
                                adCmdText);6 p; j: }: V' s- z
                        if(pRstPtrTollRate->RecordCount > 0)% ^% ~) N0 w; F  e: G% q( M
                        {) p& ]. Z, B% n0 _* `2 O/ {1 v
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;# p. `" B; ?8 r1 h
                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 ). s" l5 y3 ~( h$ L( n6 g, Z
                                {, M; S0 F% ~: O
                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));  Z& r. B) k$ i
                                        pRstPtrTollRate->MoveFirst();; ~" S" f8 P* M& _( k! e! P" f
                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)" @( M; t# E- E, U$ i0 W
                                        {& g- ?( `) C4 \' |! @
                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);9 H  [) Q* a  ]" d) D; z4 \
                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));
, h0 g5 X( y% d: _/ {3 q0 H/ p                                                pRstPtrOrganInfo->Open(pcSQL,
3 m$ ?8 G6 f9 Y6 M0 p                                                        pLocalConnectPtr.GetInterfacePtr(),
; |( \4 E7 Z* }7 t% t                                                        adOpenStatic,
- B/ ]/ |/ X. ?; b4 p' H) e                                                        adLockPessimistic,
; @$ M* x* K) w  a3 g                                                        adCmdText);
5 `0 B: P+ N5 v                                                nSplitCount++;
  m! d8 U3 ], m! M& d+ t  I/ W                                                char pcBuf[10];  H% b- [' g/ m% Y
                                                if(0 == nSplitCount%3)
1 b0 `' m0 E/ u$ b: ~' |, G0 y' P                                                {8 ]$ D8 Z3 V! L- R8 k; u2 q
                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);; y  V) C- J/ w' [% I! K
                                                }else" ]9 C. R3 f+ e/ ?
                                                {5 \1 ~. r) G6 P- U# c8 U( n! P3 s+ c
                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
0 _# J  x* a( j                                                }: r3 X+ n% W  v' s' [
                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);
% F) O4 g6 k. H7 W! I                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);$ V4 F$ i) k9 @$ p: C1 d0 c
                                                pRstPtrOrganInfo->Close();
" i3 U% E2 H7 }* o3 v                                                pRstPtrTollRate->MoveNext();$ @8 o/ |- v; A9 [2 C; a: x
                                        }
- F' I5 ~2 K( K$ s8 ]                                }
- k3 a; i2 ^3 I- i) O8 u8 z                               
3 x% H2 i1 O5 v! ?- v2 I                        }else fToll = -1;. X* @. M+ ?8 p1 o
                        5 ]* ~6 a0 R+ \
                        pRstPtrTollRate->Close();
8 s/ W3 ^2 x9 B4 H                        pLocalConnectPtr->Close();
4 Y( B% g3 q# K. j3 Q1 }; k                        m_nStatus = DB_OK;
% ^' P) N8 Q- Y                        return(fToll);
) o4 Z: J& W- J6 V                }
5 ?/ p: t% I0 V8 G! X7 ]                catch(_com_error e)8 b" M0 I- C1 y: ]1 l& f% T
                {
; ^9 e4 X8 @( w" Z                        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());
( G* H0 G  A  v6 _' L7 [* B8 R                        g_ModelPrompt.Notify();       
9 z* F: a! W6 p$ E4 i- M, U                        m_nStatus = DB_ERROR;
, P2 ^3 O. @" o7 G( U; T                        return(fToll);       
! `$ f. S8 Q. q8 |/ i0 N                }
+ H- i. B0 e" F( p4 Y8 O; i               
" i2 [  @9 m% H9 q4 }* C: v0 Z                break;
5 t) `  e6 F6 Z        case NODE_TYPE_OPEN:
! Y8 r3 p% ~, j3 ~                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",
9 T, t& F( P3 R1 E3 I                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass
: E, T" e1 q! |- w! O8 O                        );
) U4 V9 H+ {* r2 C4 B- [0 g7 T                try
4 Z% x1 O. i# d( M1 V9 \: t                {
  n& {2 h+ f; H- c                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
; n2 J) U4 \  [& M                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
5 o  Y. r0 q! {: ?                        3 x' F2 `6 \: V4 e5 n, s4 a4 t
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));1 O! B" _2 K' M7 ]$ E
                        pRstPtrTollRate->Open(pcSQL,8 |/ ?9 c' e  F3 G. j* C
                                pLocalConnectPtr.GetInterfacePtr(),0 U# o; E! @: y4 n
                                adOpenStatic,$ _$ j# C3 A( l4 r! F
                                adLockPessimistic,1 [0 L: W; c2 @" o# i/ e: k
                                adCmdText);! m3 O* g. W/ F/ t0 O, r! r, }
                        if(pRstPtrTollRate->RecordCount > 0)
* g8 o1 V$ ?# w& N5 I- O1 \                        {: s" P" ^# t4 ^5 z! q
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;& M; Q. k* E3 v" w6 X/ C7 y
                        }else fToll = -1;& i7 f% ?/ x  ?/ I  S/ s. c& v8 `
                        9 m3 O/ K. c% @& }  x1 N
                        pRstPtrTollRate->Close();# }: y4 V1 w) [/ A+ @1 w7 y
                        pLocalConnectPtr->Close();
, `7 [. |6 L& @& K& Y( [8 Y                        m_nStatus = DB_OK;
$ A1 D0 B3 l0 V; T  t- b4 k* M. ^4 S( }                        return(fToll);
5 T7 v. a2 V0 P) ~# G/ m                }. d: J. V  {( ]
                catch(_com_error e)% h& r3 W# G$ a, Q1 ]5 v& h
                {
+ T. S: p& X! Z+ B: @" h# ?                        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());
. O+ z8 s1 @3 g* i' E                        g_ModelPrompt.Notify();       
/ p% I) c6 R5 T% K  j                        m_nStatus = DB_ERROR;
6 a6 |, r1 e/ u3 U' L/ Z( T6 b                        return(fToll);       
8 p5 X- j7 f# K' s. W: ]4 v! r                }0 ?$ a2 I. L# Q. W1 k6 ?6 q
                break;* x3 Q4 J- j; a; R1 J( o$ w2 Q
        default:
7 l/ D/ B1 e3 r) Y) i                m_nStatus = DB_ERROR;
9 m) O6 I, l4 ^                break;$ A! i3 [: e- ]0 C/ T
        }, R/ _4 T) h, T. K3 }3 @
        return(fToll);        ' x; [/ u: U* Y! h
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-18 11:25 , Processed in 0.019662 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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