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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()
8 Y7 W  W: x6 S/ l' q/ O4 T{6 x3 b8 n" G1 f( D
        _ConnectionPtr pLocalConnectPtr;- O% f+ y. o; y" i; r  m
        _RecordsetPtr  pRstPtrTollRate;
/ n( w" H. J8 Z' A' o# c' Q' m        _RecordsetPtr  pRstPtrSplitWastebook;2 p; j1 T: x. h8 z. d4 o
# e, F9 J2 {4 M" p
        char pcSQL[300];+ h' v8 U4 `) D2 E8 G# T7 s
        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 ",0 r! J8 m3 ^9 b0 k
                g_ModelEntryInfo.m_PassCard.nEntryStationID,. r% G9 C# c' G, J) ^
                g_ModelEntryInfo.m_PassCard.nFlagStation1," |/ W9 S2 k/ c0 n0 \- {
                g_ModelEntryInfo.m_PassCard.nFlagStation2,
! e% `  }8 c5 P& ]                g_ModelNodeInfo.m_nStationID,4 F% U. P. b0 A* e9 Q$ a: ~
                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
2 Z- R+ R! s9 }: \& U- ]. p$ n- z/ U                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase
6 ?+ G- x1 F2 |  p1 T9 Y1 l/ s                );* p6 ?3 i1 J" u
4 p3 ?2 X3 j( L, k: S0 ^% U
        try
* s) u" E( ]- ^2 s2 t) _5 ?        {
. s) t% {) T' r                pLocalConnectPtr.CreateInstance(__uuidof(Connection));
' c- r6 D% I& v- L; q$ c" }                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
$ f0 [) S- I" c/ Z8 A$ }                3 F  p. r4 f. {
                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));- `' k0 G- Z4 Z
                pRstPtrTollRate->Open(pcSQL,
0 |1 H5 }, D- `                        pLocalConnectPtr.GetInterfacePtr(),- K4 y6 ]! V6 g* X7 R+ O8 s
                        adOpenStatic,4 M; d2 B$ a$ f5 F! s, }
                        adLockPessimistic,- I  r+ T2 `1 l6 j: m) H+ O3 E$ U
                        adCmdText);5 X& ~4 Z7 y& d7 K9 |7 u% v$ T
                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));' A: H3 q$ h  Y: N! D$ G1 l
                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),
- G; s) c8 y9 N4 W0 e: a                        pLocalConnectPtr.GetInterfacePtr(),
! t, S  q: b  b1 J# H( H5 d8 X                        adOpenUnspecified,9 F0 H+ m  O  T0 e8 f
                        adLockPessimistic,. Q: ^: G/ `) t" \  l* B
                        adCmdTable);
% |& q. \: c. T) w, F# r! T/ z# U8 i
! h* P( g( C! a9 E% A6 ~4 {                pRstPtrTollRate->MoveFirst();9 T2 X" }7 ~) H
                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)4 o9 d  C( ]; f1 m- p
                {. g5 G! B3 v2 b4 K( ~. \; s. {! J  i0 p
                        pRstPtrSplitWastebook->AddNew();8 `3 P1 T! m7 G1 T* _
                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);1 X1 }" b7 o0 ~4 y! Z$ i1 W1 J
                        - j8 q! M$ @1 \% u4 F. v: A
                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);
8 m" O; j4 z; t/ C5 ^" o                        4 `' z% ^3 g5 Y) X2 ]! J/ {8 v
                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);6 D7 H3 b: H$ a5 W- j1 p; e* {
                       
+ `8 ~$ M+ l6 P) T8 W$ q9 Z                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);
* y, m$ K5 N$ F                       
; T5 F7 I$ M9 z$ s                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);$ x" y% w9 w+ @2 k8 y9 l
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
& U7 ]- {* c4 U5 W) f( m$ A2 D/ ?0 f                        ; O# {5 B: P& g+ w4 O
                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;. z* o. l9 Y$ i( r5 g/ S
                       
$ w( e) w) t- b' S: ^( ~0 m) D                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;8 K6 @  k9 ?: }) {, [
                       
: `2 @$ o' s0 I5 C2 p; I7 Z                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;
) b, p2 h. J9 \; c                        9 G! G2 c1 R6 p0 ]2 B% P( @
                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);1 f) k/ W1 B" y+ m. a! n% s
                        $ n; y/ p* u9 ]( z5 J& r' F) A
                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;. S+ [" Z4 @6 j' P
                       
0 i+ S3 _: W+ ^4 T+ d5 I6 u                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;. U4 {" I, p7 ]3 z2 [, r6 x
                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;9 y3 U$ @- t' p6 F( t
                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;
; F1 B* a$ h" g                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;
7 ?! I4 f9 `. O* w0 ]7 r, M0 P! O$ O                        ! k, V  J! T7 b: L7 [) y' e6 G; Z9 u
                        pRstPtrSplitWastebook->Update();
: z: M3 `, M* B) v8 o: F, o  D7 h                        6 G( |9 m( ?2 m' t3 X
                        m_nTableSplitTollWasteNOCount++;7 E1 ~2 j9 [0 e
                        pRstPtrTollRate->MoveNext();- m' T+ u* k, E2 P, u: X
                }               
2 ?9 f6 `8 Z1 t  S+ D                pRstPtrSplitWastebook->Close();' A! _, }5 S& O! b
                pRstPtrTollRate->Close();, M* z) I; q$ c+ r0 o/ D9 i
                pLocalConnectPtr->Close();+ }; V! L) ^0 j- P- F
                m_nStatus = DB_OK;
7 ?; k0 }/ o( W* n  ]- |                return(m_nStatus);        , j! n, Z" N( V# n# R, ]
        }
4 V* N, k7 V" }+ _* |        catch(_com_error e)! q: p+ C+ ^3 }) F* F: O+ {
        {
# [0 H; l# s/ \* I6 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());* ~# `' c+ F! |, m
                g_ModelPrompt.Notify();       
* ?. M4 x0 t+ w$ n                m_nStatus = DB_ERROR;
4 A3 }) O+ Q5 {3 r1 n                return(m_nStatus);        9 \) ?  E( r. d1 u4 g
        }
& o$ Y0 Z5 B) b- m5 @}& `& M" P- z% u% S3 P
( I& S2 w0 b- S' B2 V6 C

0 M* Y4 ?4 e" P6 c0 s) b/ F: ?0 |* E
float CObserverLaneDB::GetTollRate(), S: J6 X5 u4 w* O: Q
{6 b: m' r6 @: B

2 I- ^8 {! Y" m9 R& X+ U, c1 E8 l        _ConnectionPtr pLocalConnectPtr;1 D, `, q' o1 K+ |' l
        _RecordsetPtr  pRstPtrTollRate;3 Q4 \) _, k& b: a; y+ G$ H2 {
        _RecordsetPtr  pRstPtrOrganInfo;
7 w" ~1 a7 x$ G5 v9 o# j4 A5 P: H6 \/ B7 F  `
        float fToll = -1;
1 b: m9 S/ |- m' b0 u        char pcSQL[300];6 _# w. ~/ e, k
        int  nSplitCount = 0;
1 j: G/ ]: _( l7 `6 `7 l  r; @9 y        switch(g_ModelNodeInfo.m_nNodeType); i( l! G  {, @7 ~0 E- m4 G$ l/ d
        {
: L- u3 u9 u) y* O/ T6 R        case NODE_TYPE_ENTRY:
3 A) L4 X- K: z                return(0);
* t5 q5 m6 T; ]9 |                break;
/ M' x3 G6 q/ d6 ]        case NODE_TYPE_EXIT:
8 e' w9 k, n* q; @+ Y        case NODE_TYPE_MAIN_LANE:' u7 F# C3 v3 X( }( c) D# b
                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase). a9 v, b7 ]( ^' @( R& Z+ f3 k( H
                {
. ~- [6 b: o  @                case VEHICLE_CASE_URGENT:
/ `  E  J5 J$ Q& E# e/ E                case VEHICLE_CASE_ILLEGAL:/ R: z1 {: H9 h/ M5 E
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:
  N, R5 p+ i3 n- T9 ~                case VEHICLE_CASE_MOTORCADE:8 z, w- p( T7 u
                case VEHICLE_CASE_COMMUTATION_TICKET:9 ^; x9 K3 n/ o& |# ^
                case VEHICLE_CASE_ETC:; S7 D3 {- O4 I" ^
                case VEHICLE_CASE_BAD_CARD:
' F2 g& S% `5 |                case VEHICLE_CASE_NO_CARD:
9 _( s! ]: u0 d( H. n9 ~2 X                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",
1 {: u8 f# b7 M) D; `4 l+ V4 g                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,7 s9 o! K, j3 f" r- q$ z! l( F7 `
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
9 s3 E. j7 |$ s: E# P2 F                        break;8 Y# R2 F/ u3 u: Y0 v) j
                default:
' R1 T+ U2 [$ I( Z5 S                        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 ",+ W/ o, _7 Z' Z- O
                                g_ModelEntryInfo.m_PassCard.nEntryStationID,- Q$ |( }5 o& p* V
                                g_ModelEntryInfo.m_PassCard.nFlagStation1,/ G0 u; ~* B7 W  J* Y: G
                                g_ModelEntryInfo.m_PassCard.nFlagStation2,
% i& U( J' }! C5 z1 _! W                                g_ModelNodeInfo.m_nStationID,( ^0 F+ ^. c+ P
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,! T0 h+ `7 ?. G! F
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
; |& f: W. d; i, D& R7 X* x: D2 R                        break;8 \( @+ T1 K7 P7 b% ?
                }
3 D2 w6 r# ?, N                & r2 _- o, y: m9 X1 p: G* d
                try3 v: ~# i* _% i+ S# ~4 t) Y4 ^
                {; X9 [3 C/ B" o% P  X$ l4 ~# Z" k
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));. ?( K$ o7 }5 K7 c3 i; E
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
( f9 S1 F/ k3 g# }                        ( ~0 _6 C/ k, H. G
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));  e! ~. @$ H  o7 z2 K3 u9 V
                        pRstPtrTollRate->Open(pcSQL,/ j0 `2 j/ r" R! x1 X) G
                                pLocalConnectPtr.GetInterfacePtr(),5 w# c* s, ?5 ?9 k
                                adOpenStatic,
0 f- E9 T3 g" a' u( M6 u                                adLockPessimistic,
  K. y$ _$ B3 W9 ~& O+ s                                adCmdText);
0 F" d! k, ^+ N  g( f" t' n: H, x                        if(pRstPtrTollRate->RecordCount > 0): ]: p$ Y6 B- p* y5 e
                        {
2 \8 H7 |. x6 m: Z) Z- j                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
& f2 P( v; U; T                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )3 v' _, R" v% S
                                {
: b0 u/ s8 o; m) A) D                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));4 }9 J9 v+ {8 I% V& [0 g
                                        pRstPtrTollRate->MoveFirst();
- G+ h' i' q& ?6 M' X# G: o0 k' e0 m% I                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
7 j+ v9 C# Z+ d4 @8 ^" W                                        {
- G3 b$ p1 p  }- \. _, p; Z/ Z. {                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);4 S5 `* l% E  f0 E( o( }8 k
                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));
3 X9 Q( y7 y1 m* B5 i5 M                                                pRstPtrOrganInfo->Open(pcSQL,
/ W5 }3 V+ b' @" y                                                        pLocalConnectPtr.GetInterfacePtr(),; p# l& J3 W$ c7 @: _4 G. W8 p1 x
                                                        adOpenStatic,
7 p, ]0 H. k# v1 x                                                        adLockPessimistic,- X9 I. _) ^/ l0 J0 K8 E
                                                        adCmdText);% V6 S/ O3 ?3 N" W6 \+ K
                                                nSplitCount++;
. X( n# x" ?' V) q! q                                                char pcBuf[10];1 x" E! q0 [" v  Z2 b
                                                if(0 == nSplitCount%3)
% T/ ~  S# K# [! u+ @7 Z# n                                                {
$ Y+ L$ l' ~. ~% W. ]                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
+ c  h: y( S$ ~0 [) N" y6 P: s                                                }else
) l" d9 N6 B2 B6 O                                                {
/ y% Z9 N7 I$ G4 g                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);+ T3 y, S+ z5 w; |
                                                }
+ f( a$ }  v' A4 E3 X                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);8 |  K* [, k4 ?: M5 ?
                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);
! x$ R# o/ p4 M# e0 g9 [# ~% ?                                                pRstPtrOrganInfo->Close();
- P* r) I! V, x# J9 Z$ z                                                pRstPtrTollRate->MoveNext();; @8 ]' g2 B$ H9 M! @) r0 y
                                        }9 g. y$ I& o" q+ {+ l9 G, K& A! X
                                }6 o7 X2 X9 _7 \& }; y# j6 e
                                7 j% p7 T4 |$ z5 {' B6 E
                        }else fToll = -1;
3 Y- j0 U( R- j/ c. q2 }3 N                        4 L. i; c* z+ E1 a* g$ b
                        pRstPtrTollRate->Close();
, p* W7 F* J8 ~                        pLocalConnectPtr->Close();
4 b6 U$ m, ?# n) f" }                        m_nStatus = DB_OK;: Q5 L8 ^1 Q% {) N. ~6 C8 H' }* @
                        return(fToll);2 I% Q; ~$ l* z7 }2 \* \
                }
7 d, [/ w" `* w( t7 \. f                catch(_com_error e)
5 t2 C5 b9 ~. p# j  N9 u+ H) s% V                {9 U- K8 C4 F  s) q4 ?
                        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 M/ y/ v0 U/ [2 @$ `, r6 i
                        g_ModelPrompt.Notify();        4 |/ E" o6 n0 I  L* B4 ?3 s$ j
                        m_nStatus = DB_ERROR;# h# Q6 V- q, v; x5 G; S" q- Y
                        return(fToll);        1 w; f& S8 b+ @) J/ a
                }
" E" x  I3 U: }  k  _  [                ! g0 W8 R  m; F4 ~6 ]7 x( I
                break;
9 P5 g$ Z4 y+ G4 q3 n- @        case NODE_TYPE_OPEN:) H1 H# x2 Z+ B+ F/ _! M
                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",
& t5 e: k  _! X) d2 D# E' d' C9 C                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass% H. [) b( @' U
                        );# @6 z) Y, a& y
                try+ a% e. ~3 f9 }- |2 D! a
                {7 g- U  J8 n$ [$ G* g
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));& b- {7 f3 |# H2 A' P- q4 a) R7 D
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
" L8 }( i, _- j6 ]                        ' ]3 J; ^8 q; S0 ^' Q$ b$ V# Q) H
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
3 m0 m! r: u# a4 e0 a/ s                        pRstPtrTollRate->Open(pcSQL,; J* W& N( L* z
                                pLocalConnectPtr.GetInterfacePtr(),6 i& w$ X0 P; f# L
                                adOpenStatic,
; v" W& Y  a! g) T. j+ X  A                                adLockPessimistic,' d6 d# q2 E5 v/ S, {4 j$ Q3 {; e
                                adCmdText);# `  W8 p/ ~# t% {: I
                        if(pRstPtrTollRate->RecordCount > 0)
' n4 Z' k- _; p) s8 a  j* y                        {* n; l1 g7 H! d, M9 f
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
( o1 X9 a" ^. w9 S                        }else fToll = -1;
; r' T- i: E& M8 A) t5 j                        # ]* {' F% h/ o! i9 T. _$ Z
                        pRstPtrTollRate->Close();
; Y/ {' D+ Y# {- z                        pLocalConnectPtr->Close();
- u( e3 k- h& o- {3 W3 y" }8 q                        m_nStatus = DB_OK;3 R$ P4 a- I  g0 l/ i
                        return(fToll);
- j; R+ G% ]1 B# W/ l                }
+ ?2 u) |5 z% D" |$ _: x                catch(_com_error e)& [2 `/ b+ y& K+ g* y' ^
                {
+ j% q8 }; p+ h* D                        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());
7 x2 |" N- }# ^) n" ~4 V                        g_ModelPrompt.Notify();        4 ]  b# t0 ~3 B+ r* R
                        m_nStatus = DB_ERROR;' ^: I4 P4 Z! }) h- {/ ^
                        return(fToll);       
8 b0 d/ p4 ^  s. G4 }                }- E+ o; g2 A; x3 [) T- ^$ g6 l
                break;
: Z3 C  z% T# U5 L( C: N' P        default:/ e& w: c; {9 L# Q% p
                m_nStatus = DB_ERROR;
, F9 U; c$ ?$ G# h% J                break;
6 S; ?; f* `6 z0 T1 y1 T8 e# O  y6 [        }. W$ |; w% g" y
        return(fToll);        $ E/ c+ Q& h$ l8 S! \8 @
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-14 00:54 , Processed in 0.021731 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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