找回密码
 注册
查看: 4047|回复: 0

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()
5 L( F7 H' F" R0 t% z( ?{
! @3 `6 B% C$ @; X9 z4 k        _ConnectionPtr pLocalConnectPtr;/ _7 l% o0 q& t; _( W7 L
        _RecordsetPtr  pRstPtrTollRate;
" ]9 y" x; o# ~5 ?* a7 ?( d  Q        _RecordsetPtr  pRstPtrSplitWastebook;( S2 S7 N: @$ R, q5 g+ v( |' `

) E* }8 G0 x4 n  o# Z. K0 m. t        char pcSQL[300];) L0 F0 i) o7 v0 Q: M. n  T$ A
        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 ",9 s0 Y% `: g3 l0 t3 t2 y( g* J9 I
                g_ModelEntryInfo.m_PassCard.nEntryStationID,3 d9 l. |2 O, J; J9 q4 c
                g_ModelEntryInfo.m_PassCard.nFlagStation1,
/ H# J0 T2 O2 Z1 F                g_ModelEntryInfo.m_PassCard.nFlagStation2,- u7 b2 i  q7 t  Q2 y6 \# p; f; |
                g_ModelNodeInfo.m_nStationID,, q4 v, }, c# P
                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,+ B9 r" w9 S5 u1 m/ b4 k9 b" s' P
                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase# S/ Q- L7 s- i0 b' J7 A
                );
8 g; i( N; n3 L3 r. ?
! s* y& E* {- @) [: m  d        try5 x5 A4 p% R5 I! L% Z2 k. V
        {
3 x: i5 z! ?% p& d- z                pLocalConnectPtr.CreateInstance(__uuidof(Connection));
/ }5 Y$ n+ L) t  Z2 P6 ?                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);5 N% g0 Z3 I: a) H) R( @
               
2 H9 }2 n3 h  W                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));$ ^: F  X) @$ \1 w; J7 L
                pRstPtrTollRate->Open(pcSQL,( @) c- J4 O* d& K/ d* ]
                        pLocalConnectPtr.GetInterfacePtr(),
  D7 t; P5 U. k+ C                        adOpenStatic,- }, Z7 V( Y/ n; D# h2 ?& d& a% f
                        adLockPessimistic,
2 ~# T' {$ _7 u4 f                        adCmdText);
1 f' `6 g# \7 I1 p8 Z7 t* f; P3 o                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));
" Q; ^: M& z4 w% d- F2 Z                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),' V& r9 D  T! u* g1 B* G5 d
                        pLocalConnectPtr.GetInterfacePtr()," d# O; K/ o3 W, A$ F# y
                        adOpenUnspecified,, d  ~+ z& ~3 l0 m
                        adLockPessimistic,
. E' p% r- s" _7 d* L5 a                        adCmdTable);
7 u! L1 V3 X+ x; T% U
8 g3 I' O2 z/ }                pRstPtrTollRate->MoveFirst();
5 {) o9 o) {9 x) j                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
. h& R* p3 G* b1 D' b( u, P+ B                {: f( H; Q6 H0 z' V( x
                        pRstPtrSplitWastebook->AddNew();
8 }8 U: Z* A* C1 `, R5 d                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);4 U+ b6 k: b  V2 L3 f, C' Z
                        , R, Y4 O2 [$ ~
                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);
$ {% M5 p4 E* t: I: d( W                        # O% p+ ]% }6 O- E. h6 m. l
                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);" v9 ^* n  H7 ~, V) W6 K* j
                       
. r* X2 C  N+ [- ~# T                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);) b# d, T. V1 N/ x/ H3 W: q8 G9 q
                       
2 M0 [- O1 z; M                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);
+ c) k' w- J+ H5 s: O                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
1 {  _9 ^, [- f7 d' T( Q5 o, Q3 [                       
5 {4 T: s% P9 C1 X9 t5 v( C                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;; Y- @; _& U; ?
                        , V5 D- q2 b& p) I) `
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;
% W) n( f# P. N0 ?                       
1 L: N  `0 a1 W& o                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;
1 j% h1 t0 M1 @% A                       
- ~" z& o' \5 X/ [$ j" g                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);
. j% t8 ?/ p* B) b' ^/ U                       
0 y& I- E  x$ C: {8 N+ T+ K                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;
6 I  U. Q+ H+ b+ E3 |                        # u% _6 l# J3 y% }
                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;9 P# O# Y+ @8 M4 Z) t
                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;4 ?; R2 `/ T! E" m) {- e2 m' Y) P
                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;
3 D6 [9 m' F! S1 v                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;1 r7 N( v$ s# u, E2 \+ H
                       
6 K9 G# t& Q, l7 ?( I                        pRstPtrSplitWastebook->Update(); - e8 H. r* I* X% C% ^+ P" P. k
                        , [  h- x) r( a( s2 C
                        m_nTableSplitTollWasteNOCount++;& I6 _- J8 y) c* @8 R/ G  t
                        pRstPtrTollRate->MoveNext();
6 w' k8 w$ A8 F( ~                }               
! q4 I/ q3 j; U% D! A- I                pRstPtrSplitWastebook->Close();
. C: E' L9 z( V                pRstPtrTollRate->Close();
+ A- A. A+ e4 u                pLocalConnectPtr->Close();0 K/ X! t# F8 m" t. l! G9 f
                m_nStatus = DB_OK;
% w! x+ n( r& B4 P8 K) U                return(m_nStatus);        # N, n' Z( @0 {
        }* |. g- G* {0 |' q5 a3 k, \9 R
        catch(_com_error e)
; ]# o+ o9 X8 I& ]( ^        {
: S# u9 T3 F3 b                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());" d% y4 x: L+ r/ }' |9 J: S. Z
                g_ModelPrompt.Notify();        2 j* W; n* ~$ V  r5 B- X+ n
                m_nStatus = DB_ERROR;. g+ r6 l# S- ^* r9 |
                return(m_nStatus);        0 b& _$ U. m6 E# b; v- _9 u. e  w9 V
        }
+ c# B( q$ b, n) I/ u* \$ ~}
3 h6 W' W8 w; a. p+ x: f! O
  |# ?1 H# ?2 X. J. O( V, |$ }1 v$ J: q$ r5 t

4 k- a7 z4 P" p$ A& ufloat CObserverLaneDB::GetTollRate()! x: K" H4 R- m- q& e- U
{$ |( o$ k3 I5 a2 U: Q5 h3 v
3 Z! ]) }6 o: ~' b8 w4 t8 K  \
        _ConnectionPtr pLocalConnectPtr;7 O  L3 s" n/ p) F0 q1 F' n
        _RecordsetPtr  pRstPtrTollRate;& X, W7 m: p; x  Q% m% \1 b+ h
        _RecordsetPtr  pRstPtrOrganInfo;
5 u$ t; y+ k/ Z. P. K: `# O  _, y4 f& V
        float fToll = -1;  s6 n' B% V" Y' }) Q8 D
        char pcSQL[300];
( M& T4 b7 J6 x* v, P8 E" L" t        int  nSplitCount = 0;- s) m5 ~+ p% R& }& Q5 ~
        switch(g_ModelNodeInfo.m_nNodeType)5 E/ e; N( r& J* S
        {7 p3 x' e4 O$ b
        case NODE_TYPE_ENTRY:- ~4 D1 M. B( N; s- _/ y; Z  E
                return(0);
$ L6 j- U" W5 s% Q) [# \  M                break;5 N* x! M4 @  _, B( y$ Y' S5 T
        case NODE_TYPE_EXIT:$ ^9 P' ?6 C6 a2 L
        case NODE_TYPE_MAIN_LANE:# _! b' r. O! a$ s" O
                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)
9 [1 d, C. O8 y( n                {
1 N! I, r1 s1 R$ ]0 _" v: v                case VEHICLE_CASE_URGENT:' L3 F3 U+ {: _3 j( v& }* g4 ^" l- M
                case VEHICLE_CASE_ILLEGAL:
* f4 }, R1 q% ^, F: U                case VEHICLE_CASE_OFFDUTY_ILLEGAL:3 r+ l& J" N( F% G) R
                case VEHICLE_CASE_MOTORCADE:
$ s+ N( y6 q/ y, t                case VEHICLE_CASE_COMMUTATION_TICKET:
7 C" L# @; R4 a: Q# K, u                case VEHICLE_CASE_ETC:
! ~9 b& Y4 C* y# D# l0 i                case VEHICLE_CASE_BAD_CARD:
; d/ c# y: a0 r3 }# Z7 a8 m1 ~1 O                case VEHICLE_CASE_NO_CARD:" W2 `. M6 l3 n0 m; r
                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",
1 n8 q: W4 |6 Y! o4 ]0 F# q                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
$ K1 i+ E. u, m% e4 A/ H                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
7 H/ W1 D# ^( e: w8 t/ N1 M& \                        break;
+ p+ `$ Z1 g, M& l: a                default:+ r  z( o/ M. 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 ",) s( S8 R" K$ u9 E0 g( `
                                g_ModelEntryInfo.m_PassCard.nEntryStationID,- K; H1 Z+ r; k+ O$ d7 f, s
                                g_ModelEntryInfo.m_PassCard.nFlagStation1,
* z1 }% V5 d3 w8 j) h                                g_ModelEntryInfo.m_PassCard.nFlagStation2,
5 ]/ B$ u# |2 {& N$ U. q                                g_ModelNodeInfo.m_nStationID,% Q/ w+ ]5 w1 P, H  S
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,( z  ~8 ~' G6 Q$ @7 G: ?/ i
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
8 w9 Q! l; J, z& C0 c, `( L                        break;
7 _  X# C* p0 _: F$ v& c7 b$ {) i                }' `5 y: l: u. l, S) p) }5 X) b
               
: k7 k. `4 H) q: m/ i  E: R                try5 O7 X% I, H2 f
                {& m2 g0 }) `) I) K
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
4 M! a0 N8 ?& o# v# O0 N. v                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);  X7 A% O2 s8 a+ U6 e+ L
                        2 {: Q' r0 J6 R/ A) s' X8 v0 E$ q7 W
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));' v# p5 U- g+ O: a
                        pRstPtrTollRate->Open(pcSQL,0 D+ f9 M  D) j; @+ I7 p
                                pLocalConnectPtr.GetInterfacePtr(),
3 }2 i& t0 I; V$ R                                adOpenStatic,
4 \$ c! s9 K, k: ^  C9 H                                adLockPessimistic,1 d# N3 [6 [* O/ Z. y9 S
                                adCmdText);
: x; b/ I2 n# ^                        if(pRstPtrTollRate->RecordCount > 0)
2 N. ~/ f, ?7 D+ Q! R                        {
/ s# o* V+ _+ x. r. {; ^" W  Z6 ]                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;0 V$ D  n$ H9 {# y. [8 M
                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )/ n& V1 {0 N; H# @
                                {: t- h+ a$ ~, t, W' R
                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));- R1 L( L# Y9 |" x! \" P$ t
                                        pRstPtrTollRate->MoveFirst();# h4 W5 _0 G3 M* ~: Q
                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)4 w( }# [9 n  e' D$ a% O
                                        {& |) T5 b- q9 F
                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);5 w! L5 W, K* U0 U
                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));
! v' W& F* a' a2 N: u1 p: [                                                pRstPtrOrganInfo->Open(pcSQL,
5 c2 V9 W( F9 |2 h* I$ n9 t                                                        pLocalConnectPtr.GetInterfacePtr(),; l$ @& R: L+ Y9 O+ x
                                                        adOpenStatic,& w' T) n/ b( ^( |2 x# `. @& s
                                                        adLockPessimistic,) z. ?# P. H2 p$ q
                                                        adCmdText);5 t' S9 w5 S; e& z& W
                                                nSplitCount++;
- A$ g; Q. ]( I/ [+ T                                                char pcBuf[10];
+ F+ a. T. M# d0 X  G9 w                                                if(0 == nSplitCount%3)! @0 {7 G2 U% e( b+ g$ E) x
                                                {
* B& Q. c' E! V! T6 Q  L                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
- B( w1 Q9 u5 @3 O9 ?                                                }else; ?8 o" R3 \0 }& y+ i
                                                {
- S7 m0 O5 @) ]2 x& ]$ _                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);1 _. d( r' s$ ?& V) T! m* T0 F
                                                }
" ]8 J6 N# X) B$ z: h! n0 F                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);
8 k2 @4 q" a1 ]8 Z& h8 D                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);& y% {' E# ~* }3 r6 I( }1 D" }
                                                pRstPtrOrganInfo->Close();
- T7 D; Q) h! U4 f( C. ^% l: g                                                pRstPtrTollRate->MoveNext();
! ~* S- W" v7 i  Y" Q% }5 \; Q1 y                                        }- T$ E' r0 `6 j
                                }
$ l0 @/ K0 V, U8 U# {                                ( w/ _' U( X( C0 B, w9 n/ n1 V
                        }else fToll = -1;! P+ o7 r/ K% ~" H/ U0 F
                       
3 K4 i  q& Q3 b( A6 }                        pRstPtrTollRate->Close();( F$ j  p( Z- H! I( N: B
                        pLocalConnectPtr->Close();
. k0 {+ T0 H  F( B* \4 l6 I" X                        m_nStatus = DB_OK;
- z) h9 G/ B- S9 H+ A                        return(fToll);
/ s* a5 C" s* A                }
+ v& h/ z8 A/ ], n# E9 j                catch(_com_error e)  x7 A/ w0 a+ B9 U$ V# }+ f* Y- h4 T
                {
, ]- _* O$ i  _+ j3 ^                        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());
. I8 g0 _9 [1 V7 x1 R7 t" b9 i                        g_ModelPrompt.Notify();       
' `4 ~, G' ^$ I7 S                        m_nStatus = DB_ERROR;
; x* Y: ]6 @5 Q( \" Z                        return(fToll);        $ V" a0 a" [0 s1 ?5 r
                }/ r' e1 t, [& L$ G0 x
                % ?& K2 Z! W# q! z  c* Y# d- L
                break;
5 e5 G, K- X- R' F2 T        case NODE_TYPE_OPEN:. e6 _! u* P; d. [4 [9 L
                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",0 K% E5 I8 `" y; [  }1 ~, A1 C
                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass! x' W  N" @4 I  d
                        );
# U( j8 Q6 F, u' J                try. D) f1 H1 j3 j: \0 n( Z3 T: G; O
                {# h; R; [2 E4 v# o$ a( U- C3 G
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));- R( }' L3 t) p* b8 x$ |
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
5 L+ P: T. M0 {" u! a8 Q3 i: L                        / O2 L- K$ Z- d( r' Y2 k, s* p8 W
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
& m5 b% c, ]' a' u2 A                        pRstPtrTollRate->Open(pcSQL,
) N2 k% s7 `0 I; f8 B# _                                pLocalConnectPtr.GetInterfacePtr(),8 T. ]3 i! ]+ w0 u: g, J/ Q/ f7 b
                                adOpenStatic,
' _8 z- h( a1 u3 S                                adLockPessimistic,
8 X9 }! ?9 i3 R8 q' f                                adCmdText);6 \4 B5 t9 k# r! [$ x
                        if(pRstPtrTollRate->RecordCount > 0)
$ D1 ^. }7 C9 |  A4 |5 q& s; {                        {- `' M* j' @/ ]0 {
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
9 r+ W+ L; e: c% j                        }else fToll = -1;  y2 u  W5 Z4 w7 s# e& V/ z' P
                        5 x) Y  `0 ~" ^- W$ ]
                        pRstPtrTollRate->Close();
7 M' F: ]$ @8 F- w/ {$ D                        pLocalConnectPtr->Close();7 u  z; `, O: v$ w
                        m_nStatus = DB_OK;
( y* Y' C% f% N8 R) J5 u. {                        return(fToll);
& R$ s( M# v5 Z' m9 `6 Q" B                }( A: }1 j# N  p" w2 A
                catch(_com_error e)+ D: k6 u8 [8 N
                {" d0 g, M& K9 [1 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());* n9 \/ T: S' {4 ^+ \" f7 ?
                        g_ModelPrompt.Notify();       
% O# N2 r4 f6 [3 L3 u                        m_nStatus = DB_ERROR;' m, V& Z4 e5 M* Z6 {+ y
                        return(fToll);        / k% v/ D2 y( I# n7 j
                }
& R3 z: P" P2 U. H: V3 L                break;
# M. h. g/ D$ o; n4 u        default:
/ j: Z' \* H% h6 W4 J                m_nStatus = DB_ERROR;
3 [/ j  O3 C2 }" x, t                break;, `. M% C! N  k/ T- a+ t
        }
2 g2 b" N$ z2 u3 v# z6 P+ ~6 ~$ ^- |        return(fToll);        9 {( I% n8 q& B" d7 ?4 C* E" y
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-10-2 02:38 , Processed in 0.020046 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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