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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()0 q6 M7 k, `; {" r
{
0 c* ]: n$ f: V" g! t6 S1 }2 I' F. z. u        _ConnectionPtr pLocalConnectPtr;
# ?& z4 p' C8 w  b! [2 |        _RecordsetPtr  pRstPtrTollRate;/ a6 L. S! w- R( Y& c: Q
        _RecordsetPtr  pRstPtrSplitWastebook;
$ u# ?# z3 Q. r4 k8 ~" m; ?/ [# `* s/ e- Z
        char pcSQL[300];
! G% ]7 E- @. l; U8 M        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 ",5 y4 a" ~+ _4 v* Z
                g_ModelEntryInfo.m_PassCard.nEntryStationID,
( w  o7 k6 E/ A% n8 o1 f4 Q                g_ModelEntryInfo.m_PassCard.nFlagStation1,
3 s3 y3 |2 z+ v1 H                g_ModelEntryInfo.m_PassCard.nFlagStation2,
+ m. K- r/ X& N$ C* I# c# G                g_ModelNodeInfo.m_nStationID,. U' W1 x, c/ s: b; v
                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
+ x" ]: v* K+ i5 J5 S                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase3 H2 y+ B2 u/ l% L8 t$ x
                );
; p! R+ t$ m5 H: f1 V$ o8 U2 p+ H* p: b
        try
- J3 W$ x: l& v        {
: ]7 m2 j9 w" t$ Y$ z1 V                pLocalConnectPtr.CreateInstance(__uuidof(Connection));9 s/ x/ p5 ?9 F! R/ Z( W' O
                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);& A8 j5 h) B2 A' E9 L0 N' g7 u
                : o& k$ B7 L3 x$ x  Q
                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
2 B5 v$ B6 X0 g/ P$ G, j                pRstPtrTollRate->Open(pcSQL,/ b! e! Q! N8 h
                        pLocalConnectPtr.GetInterfacePtr(),
1 \' E! ]/ _" }5 I: [) Z6 |; U! b                        adOpenStatic,
0 k8 L/ }, K7 y$ G; f7 k1 }5 w                        adLockPessimistic,
+ l& B) v; `. m. c2 v" y2 e                        adCmdText);
: J- p& t3 P/ O, E9 G. E                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));. \( ~$ J: C7 M: N% v
                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),4 ]' ]$ a6 l" G
                        pLocalConnectPtr.GetInterfacePtr()," W( z: x& f. T$ X* ?
                        adOpenUnspecified,
4 }* [$ {: x$ c" u! ]* p                        adLockPessimistic,
( g7 k5 ~2 {+ ^; o2 m                        adCmdTable);
# a7 p" A# Z0 G$ P8 I7 `) M5 c" u" ~. C5 L) d+ u
                pRstPtrTollRate->MoveFirst();
4 p3 E5 a9 K# q$ `                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
% I7 \, o! c( g. @                {( v8 {3 K# d6 w8 z
                        pRstPtrSplitWastebook->AddNew();' `8 K5 ]  v0 k4 x! p  C' b9 l0 W
                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);
% s: z' N; u- A  |8 m                       
/ m& R. F& X) D, c! @                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);* \$ {  Q, {; K' k
                       
! k4 g  l; Z, z0 r( m4 G                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);
# C/ F& c9 X; q3 U                       
0 a2 |. D8 R2 I4 ]3 J                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);/ Y' s/ n# E8 K
                       
: j+ @$ t) v+ e% Z                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);
! n) Z/ L7 L$ `1 f                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
) e7 _8 e- D& B5 o# R) |                        3 x' R+ ~4 ?! V7 B6 ]; j9 c
                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;$ _2 \0 n6 v$ [9 \9 p3 M
                        , Z# m* H0 D. Z3 m
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;7 E( }& @8 y" Z" b0 g; s
                        ; h" k" E' u6 I  `; _
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;
: q1 Z* v7 R7 O( N1 h                       
6 E; V: A4 ^1 G. w                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);
) ^' e; `8 o# @                       
0 Y' W; A7 F$ R  B& B7 Z* R                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;; r6 R! D: O  L" P
                        6 ^- x$ ~% L1 q  Q4 k% e8 t
                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;2 q  M1 r7 k; q% O, o$ R7 E; t
                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;4 V7 H8 e. o. A
                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;% a. ?1 X6 i  ^- Q' [* P; S* f6 F
                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;- w7 ~$ n, O, K# t8 I+ A: T0 C8 `
                        # I$ C" F- A8 c& l2 {- h( g8 w
                        pRstPtrSplitWastebook->Update(); / o+ e9 A! U: M1 ~2 ^
                        ! O  C# R. q0 {. o% `5 o8 m# _1 j
                        m_nTableSplitTollWasteNOCount++;( K/ k' E: L$ o+ H8 i
                        pRstPtrTollRate->MoveNext();7 j7 F' P  s* y  V' o4 H/ b* r
                }               
1 ]3 H% }/ L) T' R# O9 A; V                pRstPtrSplitWastebook->Close();" ^* \& h: q( L* D' ~
                pRstPtrTollRate->Close();
, z/ T3 Q& R2 L, {/ e' t# s6 d  ?                pLocalConnectPtr->Close();
0 Z8 Y8 A2 `5 w( e* O: z; M/ s; x                m_nStatus = DB_OK;
# B; \) P4 J1 z                return(m_nStatus);        ! b, p" P; c( D
        }
- w/ b( [4 `3 _' l" p" I0 @        catch(_com_error e)
. E1 q/ N9 {5 ^$ o1 Z        {; A) ~3 ^! R4 [
                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());2 Z9 N, u5 H* ^$ q4 N
                g_ModelPrompt.Notify();       
/ }) n0 c) M" }4 s3 j2 p) F                m_nStatus = DB_ERROR;
$ B. O2 R; K* I! H0 ^                return(m_nStatus);        ( p  W2 d. |1 F; V8 O# q
        }( c. }. f# G# o7 D% L, S. `
}+ Z$ n8 b# U  p8 ~8 Y

7 @# M+ I6 ~* R4 S! R9 v
0 d3 g5 f- J% I+ }* S
" r; V) U- M6 y6 N+ l8 Q0 ffloat CObserverLaneDB::GetTollRate()
# B+ Q; X% b- U0 e" K) W{- J) e  D% F9 n
/ Q! S- u6 |* K& w; N6 J
        _ConnectionPtr pLocalConnectPtr;
8 N  i  N1 ~$ j( @  ?' k3 G8 j        _RecordsetPtr  pRstPtrTollRate;
# y7 Q- S) D0 f1 ]# B: @1 q5 I        _RecordsetPtr  pRstPtrOrganInfo;
, Y# J1 c  B5 [! n9 X# D, V9 F' g  h
        float fToll = -1;
8 x! h' ~9 H' r! M5 e4 s  @3 g5 t        char pcSQL[300];/ y$ k: o4 T  m" u  ?2 G
        int  nSplitCount = 0;
$ ^$ X7 L  G$ o6 B        switch(g_ModelNodeInfo.m_nNodeType)
' `% m2 @$ m* [! [( E        {, L! `' g% e/ T$ v9 j) M
        case NODE_TYPE_ENTRY:. o' }0 k3 O- v
                return(0);
' y3 U0 y& j; W                break;4 Z7 X* i: ~4 y- {) p# X
        case NODE_TYPE_EXIT:, f& z& U2 w9 J% w9 X& R3 j. t4 c
        case NODE_TYPE_MAIN_LANE:- {0 r' c+ p; C* K# p0 J* u0 ~
                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)8 b, G) o% V8 E+ [% K- G7 V% ^4 @
                {
+ J# u( s8 j; D( W, J6 x5 D                case VEHICLE_CASE_URGENT:* z" A! T% n7 A$ t
                case VEHICLE_CASE_ILLEGAL:# r9 @5 r# V1 L2 F( t) _
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:6 W$ S/ O& ^# L/ H) E6 e
                case VEHICLE_CASE_MOTORCADE:
) E' ?: s1 ^8 L  i7 `                case VEHICLE_CASE_COMMUTATION_TICKET:4 i# j; t8 _9 R' r' O
                case VEHICLE_CASE_ETC:& h8 O5 X+ \  }4 \
                case VEHICLE_CASE_BAD_CARD:5 \' l+ V8 _2 }6 ]
                case VEHICLE_CASE_NO_CARD:7 ~% `. Y: |! V3 O$ j
                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",& m, k+ Z5 T( j0 O9 P; T  p
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,4 `9 c! ?9 D0 ]7 w8 Y' n+ ~7 ?
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);& t7 o* X2 y) d; _6 o
                        break;
8 v0 ]3 q7 m2 D* _/ W: @$ _                default:
2 B3 q7 Z* H: _& [                        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 ",' q: |3 \/ d3 j5 T" |3 o6 \8 A. W
                                g_ModelEntryInfo.m_PassCard.nEntryStationID,
5 U  w: F3 h! ^: m9 f                                g_ModelEntryInfo.m_PassCard.nFlagStation1,  N+ i  G+ U; N' i
                                g_ModelEntryInfo.m_PassCard.nFlagStation2,
0 H/ ?1 K2 {* @                                g_ModelNodeInfo.m_nStationID,$ w( F; J1 p  p. w* n
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
4 f8 {4 B. I! B4 e                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);8 t  D4 u: g5 f, k
                        break;4 t* C) N8 `0 B- X/ E: T2 n3 \
                }
8 p+ m2 G0 a2 \. A5 l7 v  H4 r, P                % s) M8 `) q4 ]4 F9 x: o* a
                try
- P6 y  s1 s; x9 d                {
1 Z+ I. O- u+ D  w                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
7 J( T  l3 j3 K4 _                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);8 R! K* F/ k0 w+ ^- |
                          @! Z" H. l* ?! d  ^3 ~
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
2 c9 O& s. s1 z9 F5 |                        pRstPtrTollRate->Open(pcSQL,  y5 Q) g, a. u" F" n: Q2 A3 J: s$ ^
                                pLocalConnectPtr.GetInterfacePtr(),# z, ?" x* a7 R; K) I
                                adOpenStatic,
% O3 y3 |( Q# B                                adLockPessimistic,7 W' |8 b0 R) S# ^, Q3 [2 V
                                adCmdText);
" R: _2 c' M- A                        if(pRstPtrTollRate->RecordCount > 0)  U; [; S& \4 k5 D2 N. L
                        {/ I' c( C2 R! i% V+ v' e, D0 S" e+ Z
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;" _, u! m/ m7 o% b  ~& y. ]
                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )! `( m* ]  m+ D. Y/ L
                                {/ `4 c( H2 A+ D9 {0 Z
                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));
4 `9 ?7 m  C, Y5 P+ T                                        pRstPtrTollRate->MoveFirst();4 [# v* q  a+ W6 Y7 ]0 U
                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++), Y/ L" s& i" H
                                        {
7 k3 A8 {& i" u$ O4 t! v7 F                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);- y3 {$ R* A% h4 P+ r+ L4 D
                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));
$ \$ h  A* m3 Z; w" M% b  f+ F                                                pRstPtrOrganInfo->Open(pcSQL,: g6 U0 L, }6 o3 u/ |, h# F
                                                        pLocalConnectPtr.GetInterfacePtr(),: s5 b0 O6 q' ], J) D/ R7 e
                                                        adOpenStatic,+ o& U. F6 ?* O) r
                                                        adLockPessimistic,
* \9 @( i- X  y9 n                                                        adCmdText);
" j' [/ u$ T  U  C: L7 H+ O                                                nSplitCount++;
! u6 U: |8 K5 J                                                char pcBuf[10];
, d0 B7 l% A& z% [+ `5 ^, {                                                if(0 == nSplitCount%3)3 A5 o; h5 }$ H& h' u
                                                {& h9 s# p0 b; b  h2 r1 |7 e
                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);* J: Z$ [+ v/ e. _6 b  W7 A( I& o
                                                }else
0 F3 r$ T8 D, b# V. J                                                {# Y7 m: K" U) x
                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);* d7 r# H4 C# a7 K
                                                }
! Y9 f( ^+ s) R                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);  t7 G7 L3 j4 z$ {* J& `0 B
                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);% E, c0 y# s+ ^' @. j
                                                pRstPtrOrganInfo->Close();
! g% C4 f8 J( u4 ]9 y' h' I# \                                                pRstPtrTollRate->MoveNext();) T  [- ^  `3 b
                                        }: V' g) a: `$ B- t9 {/ t' m
                                }
0 i9 ^/ i5 X; ?) l: y                               
4 m5 \" U! @4 \& m4 p* Q9 H6 m# @                        }else fToll = -1;$ m) A* T& O; R
                        % x" ~6 G  C/ a* N0 B& ]
                        pRstPtrTollRate->Close();
7 |4 u1 j, k- `- }- C                        pLocalConnectPtr->Close();# R5 P* T6 ]0 ~6 h2 l
                        m_nStatus = DB_OK;+ `! r( y4 m9 K6 f+ ?
                        return(fToll);9 |! Q% u& n+ \/ N
                }, z- e! i7 U+ S: O& T  q
                catch(_com_error e)
8 q  F  \9 d" U                {
$ }/ b9 b8 k  H0 _8 [! L& 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());
; J4 n' w) K$ _# |- ?                        g_ModelPrompt.Notify();       
4 [2 q- M( F9 v5 ?, O! `                        m_nStatus = DB_ERROR;% }7 E% |( |) d  h# l7 ^2 V) d& K' {
                        return(fToll);       
: R6 H, S2 s9 m8 W                }, B+ @% ]- x! q
               
% Q( w0 z9 q1 r% w- a                break;
- {% O3 O+ Q9 x: h% b: \6 [, {' I        case NODE_TYPE_OPEN:. Z! L! M4 S! k( u1 f7 K
                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",$ M- p+ b( d$ `( z
                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass8 Q; `& @/ N$ y4 a5 y. w9 y" A1 N3 q
                        );
- K; m4 N) y' b' C                try
* i  F7 K3 @7 t7 m% \7 k- {7 O                {
# R1 |$ U# @" P2 @3 `' J5 u                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));  @2 m1 ^5 |8 I1 s  J6 A
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);) F' Y5 c6 p3 f# U, _5 `! f
                        ( C& X! A6 c( c8 V* s
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));3 {) w) X6 U  ~' @0 T. y% n% x9 m
                        pRstPtrTollRate->Open(pcSQL,1 Y. |  {. o& i* x6 ~" `
                                pLocalConnectPtr.GetInterfacePtr(),% C9 p$ ^% S, A- T% Y
                                adOpenStatic,
" p9 y0 V3 l7 p6 |# _6 t) [                                adLockPessimistic,  C' L: @8 l2 p# a
                                adCmdText);( E6 C: C' y% M$ G- D
                        if(pRstPtrTollRate->RecordCount > 0)
: b" c! ]0 ?8 q' V4 v                        {; \) D  Q( d& U
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;. D9 _* t$ r' U0 J6 |5 k
                        }else fToll = -1;
% j; w1 b2 E$ F5 M9 ?9 {                       
% M+ p5 A) O) n5 A% R" s                        pRstPtrTollRate->Close();) J  N8 a4 o4 S: }/ ^4 T5 \' t# z0 P! U4 X
                        pLocalConnectPtr->Close();
" R9 Q. J  Q' W6 C* I; F                        m_nStatus = DB_OK;
; d$ E& K# A# q- @; b1 N                        return(fToll);
+ R. l: M# y/ \+ O                }
9 i# D, M$ R2 S5 d+ U4 j/ X                catch(_com_error e)  W/ d: w  q+ a7 t6 H
                {
6 J. s4 q3 F9 m) K1 E& 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());
0 E' [: `! {6 p2 L6 S                        g_ModelPrompt.Notify();       
, s& ?  S" u% s; B4 {' j                        m_nStatus = DB_ERROR;
2 @% c, m+ \( s& t! R3 X. `* i                        return(fToll);       
; n' n: a6 Q0 }  V4 ?                }) n! G  O  q0 x2 B9 [& M% y! N, F/ K- Z
                break;
3 p6 f$ ?- p! F4 F  _+ ?, R. C        default:
1 _# P. y/ Q" ?2 X& ?                m_nStatus = DB_ERROR;* B$ W, V1 i" n% ]: T
                break;) _5 q. n* |& Z  y! u! {
        }2 }5 R& W* P/ |, b% N
        return(fToll);       
& I) i) [8 u" s/ U+ q/ W. K8 \}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-13 22:46 , Processed in 0.019906 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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