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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()
# u; A7 o( l: |- E7 e# S; `4 B; w7 X{' u, b: w/ b; }8 k7 j4 X6 z
        _ConnectionPtr pLocalConnectPtr;
( ~5 R$ p* M4 R        _RecordsetPtr  pRstPtrTollRate;+ \" N8 T5 l" r' A5 j6 h; U
        _RecordsetPtr  pRstPtrSplitWastebook;4 W6 c2 \7 |( B
0 i4 L& q; l8 |9 w& P8 ]
        char pcSQL[300];
% j! K$ A0 I5 J2 E2 z& N& T        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 ",
6 ^8 J) c1 e/ u7 @                g_ModelEntryInfo.m_PassCard.nEntryStationID,
+ Z' m+ Z7 E8 W0 M" J                g_ModelEntryInfo.m_PassCard.nFlagStation1,
% p1 Q1 a" G9 `  y% E" I$ z                g_ModelEntryInfo.m_PassCard.nFlagStation2,
2 K' Q( h( W& O- B7 p                g_ModelNodeInfo.m_nStationID,
6 t' o$ y: r: v& Z0 V                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,( |  i7 b4 G2 @' g! S
                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase' F7 [, i. X9 v, L( E7 r
                );9 F' t# B+ s3 Q" |6 ?  U# S3 T
( \7 I% @+ }2 _2 w. r) \; L' C
        try$ J$ {0 P; m/ h8 C& J: B. Z: n- k
        {
: h& h' z- C0 b7 B% v                pLocalConnectPtr.CreateInstance(__uuidof(Connection));
) ?6 d% O9 c! A7 c: j7 N                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
5 J+ |2 J" N: S; m2 k# K                % g" d3 X# X, Q" \0 d
                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));, o: I8 r' ?5 A, z3 l1 M/ j
                pRstPtrTollRate->Open(pcSQL,
' p$ g1 X! }* @3 g7 z                        pLocalConnectPtr.GetInterfacePtr(),
1 A- P  v2 G" E& \& Q! I1 _                        adOpenStatic,+ m6 z2 {* |( ~: K2 N9 a
                        adLockPessimistic,' P' ^' a, O2 N$ a0 @% A# y
                        adCmdText);, d/ R, V% `+ }" B* |9 C
                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));
6 T5 ^" U% u! A: }) ^1 \* t                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),- K+ N: q; O3 A( ^& ?; g9 k% |$ H
                        pLocalConnectPtr.GetInterfacePtr(),
: m6 |  O+ j# t. t/ Z" {                        adOpenUnspecified,
4 W6 T6 P- y0 q  U7 E8 \# T                        adLockPessimistic,/ c; P" u" p5 w! a
                        adCmdTable);3 O% ?, N- |$ a$ a

4 N. g$ M: {5 u6 V/ C' B1 t9 v                pRstPtrTollRate->MoveFirst();  f) E% ?. E& g' x& d* Q7 P
                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
( `! c% d  h% X2 e1 T4 I                {; K3 T# i3 ]2 T: d
                        pRstPtrSplitWastebook->AddNew();
' e/ m. x1 ~4 z' ?" K% O                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);# A/ w/ ~! k* n& p9 u" T
                        : T2 m) T" d8 H! p, k2 u$ Y
                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);
1 H" f" l/ e8 x& U, p                       
6 R7 y8 {7 U4 A, W! c                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);
( b7 A. u6 T' M& S  p* q' z                       
% a$ q, T' Z! g2 X                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);
/ g4 L: ?& X% ^9 ]3 g& [7 y( ^                        8 a# b* X: B) Y/ b& O( n
                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);
8 }+ e0 T7 I  [( ^3 [: P' l/ m                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
$ i* J* w$ v. l& f1 [- \# j                          j. P5 h* @4 [" c, ^
                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;
3 m& T4 Y# p& u- W$ Z4 y2 Y- S( Y                       
4 u) `( {0 W2 N% `' T                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;
$ I1 G5 \4 Z$ R; T; P, n9 N& u                        ! |9 w$ y$ T: f8 ^/ T' ?
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;
5 i+ q0 ~. a% R; T                       
; g( ?- u' X& G7 p- S' c+ H                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);
- i3 b# O' b: n6 [4 H+ M+ B                        + O6 L% F* ?8 @% h
                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;
; g/ ~' U3 F( Q' z+ K2 r                       
# z% |! N% r% j% F                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;
6 I" x7 l: r& f                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;
/ V+ x, x8 Z( S8 d0 t/ v0 i                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;
/ X) U3 s5 d3 e                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;0 Y- @! Z1 h$ b7 R
                       
, X) E  Q, F: O6 X& E                        pRstPtrSplitWastebook->Update();
3 A0 H4 |, g5 H; s1 N- V                       
! j4 U. W! N. V& a, `- Q) F                        m_nTableSplitTollWasteNOCount++;; k, j0 h+ Y: C0 ~) E! n0 q1 D$ |8 J! ~: c
                        pRstPtrTollRate->MoveNext();
0 E! `! g( Y$ y& a2 S  B                }               
- h8 Y! O& [( ^' T; Y+ q                pRstPtrSplitWastebook->Close();
' k- l4 e( M  E" U& K                pRstPtrTollRate->Close();$ T) c( R: o- `$ ]6 G  \
                pLocalConnectPtr->Close();
4 c, K% V) j9 Y2 n% U' ?                m_nStatus = DB_OK;0 M# i7 K, s8 [) [4 F0 I+ ^" L- R
                return(m_nStatus);       
& j2 Q1 M2 F! x$ H( p  q# f        }( C7 F0 k  u3 X% B2 M  x
        catch(_com_error e)+ ~1 M# p) L  h) E
        {' A( F; T+ L4 e2 x* S' J; 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());
* ]5 |* }+ Y1 M/ E* X1 o                g_ModelPrompt.Notify();       
- n% ~. H2 z- o+ f                m_nStatus = DB_ERROR;
6 U0 Y; L9 @2 h: j( @; N                return(m_nStatus);       
. H' {; L9 w( L" U4 I+ _+ y# m        }
4 I3 C4 m! @8 b+ q2 ]# @}
. [( b( `; Y9 G3 C$ ~) r4 @1 E
3 _7 D# }) k# d  W8 l# }( J3 H
0 k* i/ [) ?/ }+ F# p: t/ Y" v- p6 D7 j/ M1 M6 @
float CObserverLaneDB::GetTollRate()5 S+ i: r: e: b( \8 `1 S, o
{# K$ j8 n2 e: L! [1 \8 W
7 I4 P: `2 ^) W
        _ConnectionPtr pLocalConnectPtr;
3 X1 ?/ u5 l* ~$ O        _RecordsetPtr  pRstPtrTollRate;# f# Z! F! z( R& D$ N) V
        _RecordsetPtr  pRstPtrOrganInfo;6 a0 I  u4 q; B6 f  V' W: z

. p% O( [' J- f5 l. y6 o2 Q        float fToll = -1;
- G" H( S4 g- v8 |! j# Y        char pcSQL[300];
7 X- Q1 Y3 F0 t  ~/ l% C        int  nSplitCount = 0;1 t0 |) O: }- p
        switch(g_ModelNodeInfo.m_nNodeType)! h9 H0 A- X6 u5 F# c% n- q$ y
        {
# W+ P$ \9 w' I4 w0 P        case NODE_TYPE_ENTRY:
' ~! C4 f/ u" b: Q7 B                return(0);: I# h9 [& d" `! N  E
                break;
/ v/ ~7 I! R- K- O& O        case NODE_TYPE_EXIT:- p8 j5 J! S. ?( U! k
        case NODE_TYPE_MAIN_LANE:
7 i5 t! q% P+ P! r; H* F4 L                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)
( Y; t& x% \+ l/ P, }% U                {* U% L/ u# C7 g, ~! w3 ^! |
                case VEHICLE_CASE_URGENT:
7 [3 x- O" r* T6 F2 h9 {, {- ]                case VEHICLE_CASE_ILLEGAL:' q# K6 k8 ]3 r$ K$ I
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:8 Y; n) f, w, S* G
                case VEHICLE_CASE_MOTORCADE:( \2 v# K" K  k, J( Q8 w
                case VEHICLE_CASE_COMMUTATION_TICKET:# c) l. _4 Y( O* R+ w9 P$ I
                case VEHICLE_CASE_ETC:2 @) t$ x3 m1 J4 ?9 F0 h
                case VEHICLE_CASE_BAD_CARD:
, N7 g# y8 o9 _! M                case VEHICLE_CASE_NO_CARD:
. ~% [( c* e4 N+ L' b, F3 E                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",
! `. K' W0 Z7 J# b* x$ U/ N* C                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
' h/ D8 T  @8 C2 p2 T                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
0 M- Q3 I. R: W0 f; k% Q4 }                        break;
, X' i% y0 F! R' n                default:, ^- e: j3 D! \1 D
                        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 ",
/ \: j2 C3 I' @2 x1 \                                g_ModelEntryInfo.m_PassCard.nEntryStationID,# K1 z/ c: G- \( k! v3 B
                                g_ModelEntryInfo.m_PassCard.nFlagStation1,
, j1 E7 U  n7 U# W                                g_ModelEntryInfo.m_PassCard.nFlagStation2,' I- D$ O; C, b5 R% v9 i; P7 b0 G
                                g_ModelNodeInfo.m_nStationID,# c! J: b2 J! x* M, I
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,  D' @5 k- \/ _; o
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);0 ]7 K& t4 D8 x! A" n8 w
                        break;
/ o/ r, C) }8 C1 v                }
4 O. x5 F( ]' c1 J               
8 o' D4 }( V  m  Y                try
, ~( ?$ T0 G7 d4 [1 @: ]4 Y- n                {0 \/ F6 d) {1 L9 d  d4 ^
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));, ?" j+ d$ k2 K
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
! K7 n4 _- R* |8 p                        $ s. h4 o8 d' J9 p8 Q
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
8 {: n2 \- e/ M1 R% K( b                        pRstPtrTollRate->Open(pcSQL,! a' ~/ G1 d6 N$ ]/ P% h
                                pLocalConnectPtr.GetInterfacePtr(),
7 b3 c/ ]3 M6 u# E                                adOpenStatic,
% T0 Z. f& o! N" e& {5 O- f/ _                                adLockPessimistic,2 x5 L7 d( ~5 t& O4 w% S* N
                                adCmdText);! P* k3 o- [: k4 i/ n
                        if(pRstPtrTollRate->RecordCount > 0)% ?  H+ s0 T4 h1 P6 {7 o
                        {% Z% m$ n3 a# T" F8 q, j
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
$ O. n3 i* M' Y* E5 c$ S8 s                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )% I. E/ H* {8 p9 K
                                {  p) k: ~1 g. O1 q% W
                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));
# ]- k8 X# T) G                                        pRstPtrTollRate->MoveFirst();% D6 K* h# C5 w4 U9 H  z2 k* D/ N! h6 a
                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)4 Y0 {) f, t9 h6 e" t
                                        {
3 U0 w/ j, u; b5 e! x                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);5 Z  S! k, s; y6 o8 Q* B( K: Z% ]
                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));
! Y! N( r9 c( B( c                                                pRstPtrOrganInfo->Open(pcSQL,
# C; Z2 Z& u3 B& h% o# X                                                        pLocalConnectPtr.GetInterfacePtr(),
& @- c8 ?5 k! N& t' O1 X. r                                                        adOpenStatic,
" \" L3 \: N% J. ?/ a# N                                                        adLockPessimistic,
9 v/ @! v5 F* L! L                                                        adCmdText);! q( s$ Q, T7 R/ y6 N
                                                nSplitCount++;
4 z7 f% g# W$ t, h+ u! T                                                char pcBuf[10];" x) j! W0 U5 M& i& r6 f" m
                                                if(0 == nSplitCount%3)+ U) K# C; {5 Y. A) g2 M
                                                {- _, J7 I' Z5 z1 ?
                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);8 Z2 x  Q6 W2 {( j
                                                }else0 h9 P7 ]7 A* ]0 |! @% I/ A8 o
                                                {3 B+ l+ V# A2 b$ f0 u) c' Z- ^
                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
& W5 i, @5 W: q                                                }
+ ^: t* m, h8 F                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);
) b! m, ~; X  N8 |  X+ X                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);) u( l4 h/ Y7 d6 z9 u, `
                                                pRstPtrOrganInfo->Close();
! D/ B% A3 r# J9 ?; h                                                pRstPtrTollRate->MoveNext();
$ c; H( ~; t7 U  f8 k                                        }) k2 a9 T3 }+ x: V2 ?* \! g
                                }
" [8 [: j! S, ?" T5 V& |                                $ F: x. e: A! i1 x: r/ I2 w
                        }else fToll = -1;+ z: d: A7 W) V$ I- z
                        " M& ]9 a6 [% v! D7 \8 q& o
                        pRstPtrTollRate->Close();
1 [% Z4 X9 h" q$ o5 I                        pLocalConnectPtr->Close();6 _# |. k8 Y4 @  C  E
                        m_nStatus = DB_OK;
0 }- O# a, `. e" _                        return(fToll);
, l2 k7 H' ^/ z$ c. z: L* e7 D0 Y8 p                }
# R' _4 l$ e5 B) y: l                catch(_com_error e)- e; v* h0 V/ h  C
                {
6 z9 b1 O9 T& m% C+ a7 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());9 |  \! j( {3 Y* I# @( K. r9 _
                        g_ModelPrompt.Notify();       
. u- |' F: S( y/ G7 }( a                        m_nStatus = DB_ERROR;
  v$ r9 n& B- I                        return(fToll);       
5 L3 i  N' a. g  Y, r3 f                }
, Q+ H4 I' T$ E: p3 A               
& M1 C+ i. G% s' Q                break;
: l- l, s9 b% B: r, b  q! r        case NODE_TYPE_OPEN:
4 {2 E+ ~/ Z" H! q9 ^                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",0 B8 f( ?$ ^, k  i
                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass
" X. E" j) n8 ]9 H* i                        );
) e$ {: Z7 T: t  \4 l                try4 V2 X4 m+ N! }. _
                {' f5 Z. i7 m+ U
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));/ G* u/ }; ]1 x3 U
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
4 k: r8 `9 v& ~  Z3 O: u( \" X                       
  A4 T" j; ~) r6 b, j( S0 u# u                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));8 `" c! x( |% p  {6 c) M; N
                        pRstPtrTollRate->Open(pcSQL,; w2 ^) s9 ]* w+ p9 B& p
                                pLocalConnectPtr.GetInterfacePtr(),
1 Q- ^( d  Q  k2 X2 J                                adOpenStatic,  g( A9 G+ V4 I1 e# G' E
                                adLockPessimistic,
& {/ w5 S3 b" ^! K8 a; t8 H                                adCmdText);
+ W' m7 I8 s) I                        if(pRstPtrTollRate->RecordCount > 0)  k7 p  B, S$ E2 a
                        {
: c% {# k* M8 @; y& G                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;: `& ~- J) w( A: ?. C5 n
                        }else fToll = -1;% w7 K" F* |! f) l4 d
                       
4 s7 {' |3 |  y# H                        pRstPtrTollRate->Close();& f% z, T! @3 h2 V
                        pLocalConnectPtr->Close();5 f: N: H  a8 _" X3 K
                        m_nStatus = DB_OK;
6 n: H4 }$ ^1 u0 J" {% Q8 f                        return(fToll);  N0 I* l& i. ]# z: u; z
                }3 {5 ?; \: n" m! \4 W
                catch(_com_error e)
6 j5 [8 x1 Q, L: h9 K/ ]                {* N) O* u4 A. B% Q
                        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());) Q2 z) S8 Q8 }
                        g_ModelPrompt.Notify();        ; i0 x( a5 W8 o/ Z- z9 j8 d
                        m_nStatus = DB_ERROR;5 q% D$ g! O  _2 x8 F' o
                        return(fToll);       
5 w: [. a% X! v; I1 g) I                }
! v9 z* j) n3 t* n& d                break;
7 t1 N3 p/ f0 q3 B        default:
. X# u7 C0 a( E8 X$ `                m_nStatus = DB_ERROR;4 n* P+ B6 G1 X& `9 V
                break;2 v" Z9 ]1 w: G9 _
        }
1 A6 I: d9 g6 J8 e9 R        return(fToll);       
1 R! E$ e, q# v3 |$ @2 Y% V) V}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-10-2 03:40 , Processed in 0.017054 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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