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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()
% B+ B9 E$ N, E# o. p; i, v{
4 R7 W/ e1 ~4 V8 M- N# z        _ConnectionPtr pLocalConnectPtr;
' V  b9 C# h% d( M) [        _RecordsetPtr  pRstPtrTollRate;; ^8 ?5 A: o4 B
        _RecordsetPtr  pRstPtrSplitWastebook;
$ ~# k, V. ^4 t1 x1 m+ |- d3 S8 K: O& s8 d
        char pcSQL[300];7 m" T: q& }8 Y! j. @1 ?
        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 ",
: q5 k5 ^3 Y! G, i* Q+ N                g_ModelEntryInfo.m_PassCard.nEntryStationID,
! N/ z7 Z  M' Z5 e" y; X, t+ w                g_ModelEntryInfo.m_PassCard.nFlagStation1,4 h0 x3 Y" T+ f. w9 {3 J# t4 L
                g_ModelEntryInfo.m_PassCard.nFlagStation2,
5 a) r% E+ M+ Z; Q                g_ModelNodeInfo.m_nStationID,( _& T8 W: j/ F1 j
                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
* I$ _% H0 D& b) l' ?% ~) Q                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase
! L) ?- H& h+ U                );
5 M; Y4 [: h3 I  `9 \* Y2 s+ _. v" W- f& B+ @! G
        try
/ S8 |3 ~: B6 e        {. {6 T% w" g8 @% p% [) J
                pLocalConnectPtr.CreateInstance(__uuidof(Connection));9 Q5 k5 R, J+ T0 T
                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);* `4 x3 @6 R- \( {# Z
               
. b5 _. {2 ?5 X                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
) k/ C3 c; G5 C8 \3 d                pRstPtrTollRate->Open(pcSQL,
% A2 u& r" h, a                        pLocalConnectPtr.GetInterfacePtr()," E( H& F; {1 x
                        adOpenStatic,
, L2 S- ?8 ~6 {% w7 W/ x/ b                        adLockPessimistic,: ]( C1 ?* s! n3 A6 @' P; V. y3 N
                        adCmdText);
  |' }; z& `0 z$ J$ l                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));
; z0 ~! `  v$ B! s                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),: i$ l! i* Q1 t0 A
                        pLocalConnectPtr.GetInterfacePtr(),
; B4 Q" \- u) u. [                        adOpenUnspecified,
' x& f9 v& C' t3 k                        adLockPessimistic,
: S% E6 E% A# N& X% ?                        adCmdTable);! O* K+ ~) s* J9 Q- Z$ Y

) s, f4 v6 h, @/ p2 F% V" l                pRstPtrTollRate->MoveFirst();2 @5 U( \4 Q5 C: m6 I
                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
2 Q9 y% Y9 O7 E( `                {
) r& q! _9 p9 W8 X$ j9 Y, W  w+ r                        pRstPtrSplitWastebook->AddNew();
% v1 ^" L- O# H' h                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);( _$ c" {/ s5 [9 W% R/ ?$ [
                       
; ]3 w( \0 ~! ?1 E7 r                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);
( u4 A/ H! q- K7 i2 o                        1 ~! i" b* \" k' G( u
                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);( F: C/ n( p% v- X4 o
                        ! G  d8 n6 T. n( Q
                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);3 X4 |! i& h0 c* ~) A4 |( W. b
                        6 M# t2 ?8 E( ]8 W1 m# H
                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);
' s2 t( \$ \; P+ N3 W  y# H9 \  }6 @% |                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
; B$ U. y) ~+ M5 s4 |; H                        6 n( D- b6 l9 v' H. d
                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;3 ?8 B0 K. `4 V& G( @/ k
                       
, W! `' p; o9 j0 A' w9 P                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;6 Y( G; e. G3 a$ B# t
                       
! F2 q+ A# c3 l6 I9 k- t8 ]                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;( B% Y' E' N  q6 R+ p
                        & J! J* r% Y. P8 L/ y) W5 M
                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);& [/ d0 J6 k1 i, e, t/ i5 k! d/ z; ?+ p
                       
7 R) O8 w& J2 i9 R0 @: I                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;
8 ]3 a' t' M1 U! ]* o# h* Y                        ; i1 v2 p% C" w8 n) \
                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;
& R% W; K5 |4 s6 R$ P# b                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;
; }8 v. X6 M. _) u                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;* [; j7 ^6 c1 |' m- l% L
                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;5 c1 t8 Y) j$ {9 h1 I" w
                        8 H8 d# G1 w8 R% F# Z+ k
                        pRstPtrSplitWastebook->Update();
/ V! p( R" H- z                        0 K3 N( P2 {. m7 x+ [8 H
                        m_nTableSplitTollWasteNOCount++;0 g# v2 N; ~. ~+ |& z
                        pRstPtrTollRate->MoveNext();- D9 l: E7 e3 Q$ R, r% g
                }                % q" y" t$ }) ]# y5 i- T/ B
                pRstPtrSplitWastebook->Close();
9 ]2 Q; R8 e- K' j# Q                pRstPtrTollRate->Close();
( Y$ ?' W9 T# y4 {3 I                pLocalConnectPtr->Close();" j; _% r5 K0 `6 x
                m_nStatus = DB_OK;" ~$ Q2 Z% T. u! z) i4 F
                return(m_nStatus);       
! J. L% [/ V# Y7 T; f3 l! L1 q        }
/ \( D% @- G: V& i# k# h! w. b        catch(_com_error e)$ {: z" m2 k& T5 J: T# L7 i3 `( I
        {
3 v/ C! n; G/ U( }! w5 d6 e                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$ ^, D; I+ o+ z/ t$ _: E" w
                g_ModelPrompt.Notify();        , J; F# `4 o/ a( L8 d9 ]* R
                m_nStatus = DB_ERROR;0 ?" m# s  p/ I$ d" r
                return(m_nStatus);          @# Z( E1 F$ A9 i
        }& C- w+ Q) T' F
}0 t$ G- H+ }2 ~" O
" P3 o( u' d4 l& w* x2 t' X
8 \( X* U  L, ^
8 M. E) q3 _" D
float CObserverLaneDB::GetTollRate()* U  ^4 H5 R6 q; L
{4 g+ q' y( v6 w. a' h$ U6 u
4 d; X: t, U3 C0 n
        _ConnectionPtr pLocalConnectPtr;9 }6 q$ Z: q9 V* j% D7 b
        _RecordsetPtr  pRstPtrTollRate;
% `9 \- B+ _/ [/ l+ R0 l- Z* O% j0 m        _RecordsetPtr  pRstPtrOrganInfo;5 o( h! {) s; ?, q: e3 T+ y7 N. V
$ _# ]/ O! L" w
        float fToll = -1;
; e7 }) T# w/ ^5 m) z( X        char pcSQL[300];
/ P  Y  b! x# o' c        int  nSplitCount = 0;
/ I  i% T: u$ K" p) a        switch(g_ModelNodeInfo.m_nNodeType)4 y" Z2 t5 F: C# K0 f0 v
        {7 p% T1 t, }" q
        case NODE_TYPE_ENTRY:
% B, k7 X7 P# ?# d, i9 M                return(0);& \2 \/ j' I9 c7 Q! y  u
                break;1 V% B" U" }$ G! [
        case NODE_TYPE_EXIT:) c& B& w9 w/ h
        case NODE_TYPE_MAIN_LANE:
% S! A( s1 |: R0 ^. @7 {' _1 R; p9 w                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)* D$ i$ C, R3 ^& H: j" f/ v/ l
                {
$ {- F) g5 @, z3 D                case VEHICLE_CASE_URGENT:
. [# V2 V+ Z" r) a. W) b                case VEHICLE_CASE_ILLEGAL:" `0 T7 \; X0 G0 U. l
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:
4 V) l0 g3 l5 b7 i* ]) H                case VEHICLE_CASE_MOTORCADE:
* ^: g$ y; q# b) B6 R# e                case VEHICLE_CASE_COMMUTATION_TICKET:8 M- y# E( U! q0 t5 ~7 |1 i
                case VEHICLE_CASE_ETC:4 d( u, A1 H' B/ x, L% q
                case VEHICLE_CASE_BAD_CARD:
. w% X& H/ s- V% n! J% y% W; m                case VEHICLE_CASE_NO_CARD:
$ A5 o8 N0 y* \* e* `$ e& W- t% K                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",% ]# P" D2 Y: d8 ~( v  r
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,5 p% E( [( e9 T7 z* h& U6 _
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);) }; s) W+ e; q' B: g' }$ J2 }
                        break;
( n8 `: _8 F+ Z$ H                default:
) S. T9 W6 A5 Q- n: {! A& ^" 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 ",
& E5 L4 x5 `' V. W: p. E                                g_ModelEntryInfo.m_PassCard.nEntryStationID,
3 C* \! J  c! t$ C# B                                g_ModelEntryInfo.m_PassCard.nFlagStation1,
1 w- U. |  ^& p5 ?$ ^. V" P+ r2 ]& @                                g_ModelEntryInfo.m_PassCard.nFlagStation2," n- @6 a/ N3 u8 r
                                g_ModelNodeInfo.m_nStationID,. ^( ^$ ~$ q. L0 d: x
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,- \* I: S  i3 E9 d" `1 I
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);; {" c7 [* t3 E
                        break;
7 P& l5 R& e8 a4 w7 \) K                }
4 e3 a4 K! Z/ ^8 f8 G               
0 b3 U5 V" i; X3 t" _                try) _7 K/ `0 Z$ A* h- G8 P
                {" D$ j/ [6 H) r2 ^
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
1 }8 P0 j* W5 n3 ?+ h8 }                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);, Z9 W0 H( _5 p' p. c6 C9 g- V
                       
8 T9 d" S# A: D/ j) r, C) A# \8 U                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
8 b. W9 e; a* T9 A& Y8 n                        pRstPtrTollRate->Open(pcSQL,7 X4 e5 z0 |! W1 f) ~& E% P( j# t
                                pLocalConnectPtr.GetInterfacePtr(),/ n( m3 \' }' n6 z4 x
                                adOpenStatic,
8 j9 ^0 i8 f$ y                                adLockPessimistic,& \# O1 y5 i7 |: [+ V: l
                                adCmdText);
! x- {- I/ o$ w                        if(pRstPtrTollRate->RecordCount > 0). i; ]4 @( Y' c. }8 n* C
                        {
' r  K& }( C! p) A/ s                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;1 T  R4 L  O* A- d" V/ ~
                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )0 U0 o+ m+ r5 h$ R) [, }; B  [6 X; g
                                {# K; @0 Q$ ^8 D$ {% y
                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));/ ~  t" M2 a3 _, C3 W
                                        pRstPtrTollRate->MoveFirst();5 j* e  K" O; }* e
                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
3 |9 u4 x2 \* ~( a/ F# \; _                                        {
0 b! g+ `# }2 X% H                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);, x$ k7 y/ ~3 a( M. A
                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));6 E9 U* A) t: B* J
                                                pRstPtrOrganInfo->Open(pcSQL,
; y" f2 i9 ?6 g8 T3 q* ]                                                        pLocalConnectPtr.GetInterfacePtr(),
, K+ K, i' a# v% x# @/ p                                                        adOpenStatic,) ]" j( D3 A# i2 `% W/ }1 k6 n, |
                                                        adLockPessimistic,/ {# R- ]7 h% g) I% y% j
                                                        adCmdText);
/ D4 k) Q% J  }- [1 \                                                nSplitCount++;
) {: h+ A4 [$ J' l- e) r& Y3 K( @: |( V                                                char pcBuf[10];
( f- ^( _# j6 b4 ?* ^4 t; t                                                if(0 == nSplitCount%3)
' V6 s; m5 M2 p6 @  T9 I                                                {5 B9 c, B% t" S
                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
% M1 R9 _; E3 q7 }                                                }else; X( r: e  g1 m) C8 E8 d( m1 |
                                                {" ]  R  J* P2 a/ ?+ i, F
                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
, Q  P. q+ ^' D6 |7 I& S! Y1 c) o                                                }
; Z" W6 s- o! K$ A9 }                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);
0 C& G& ~! @: a                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);  \7 c+ Y6 N4 h
                                                pRstPtrOrganInfo->Close();2 Y, z( Y% L! k, x( \. a
                                                pRstPtrTollRate->MoveNext();6 G: p- e4 j; y# B- n
                                        }
$ X9 Q  O; G7 y0 ~# e                                }% b- Z% i3 j& t7 l
                                6 x* y* g( q+ k5 k/ e" d( Q
                        }else fToll = -1;
" b* T0 m+ ]% r8 z                       
$ J. R! `4 L( L$ W( n; P7 G/ N                        pRstPtrTollRate->Close();+ b! J( T0 P* u" Q
                        pLocalConnectPtr->Close();% ~/ s( k( G9 }/ ^
                        m_nStatus = DB_OK;
; W% r5 u* I- k% F3 ^! V/ l7 c                        return(fToll);
& u' c5 s$ t  g, Q8 d* \                }
  q1 x  w' [9 [0 W6 d                catch(_com_error e)) _3 v! A: W6 X- j9 w
                {3 A8 u7 T9 C3 P
                        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());% D2 T0 s; x0 @
                        g_ModelPrompt.Notify();        ! K6 Z' N( r+ @) a/ Q
                        m_nStatus = DB_ERROR;
* ?9 C6 b. N' s7 ?1 F                        return(fToll);       
* Y/ W/ v- l/ G/ s) \                }
% C+ `6 e# V* m, \+ J: Y               
+ U6 X- {8 S' }                break;2 p" V) ]6 \5 c9 j  j
        case NODE_TYPE_OPEN:
& M" k6 O# m7 a( Z# P- B                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",) @1 w' S+ C! [( e# q/ a
                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass6 Q& a& |1 i2 Y, r' m8 R& G" _( s" c) m
                        );; |! m; F+ |: S2 b! t
                try
8 r7 T9 T; P# \, m: u                {
9 Q1 Y+ J1 J" P: X' g; I" \                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
+ Q6 i" E. }& K4 D  w3 @                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
1 V/ M+ J5 _+ i% @, X% ~, l  Q4 |                        ' W. V' a: p0 A6 o2 B/ H- i
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
- j! N  o2 |# O8 C+ }9 a5 s                        pRstPtrTollRate->Open(pcSQL,4 X6 W5 i, w$ Q: F; a
                                pLocalConnectPtr.GetInterfacePtr(),2 S8 L/ D( c% e: X! B! I. ?' t
                                adOpenStatic,+ d4 D% x6 ^) r: Y5 q7 q7 Y  \: G% L
                                adLockPessimistic,
& b! r' `% |0 ^) A/ G' J                                adCmdText);) m5 W9 u+ u* ]0 j- \
                        if(pRstPtrTollRate->RecordCount > 0)' ?6 w1 R9 |5 q) q* C- e
                        {
; G, ?- Y- [3 A* A                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;* G1 J+ L# v6 j, z, j1 h8 S
                        }else fToll = -1;
' B7 x. k& W' y& O. F1 L                        6 C4 q' Y# N" \% }' g/ J
                        pRstPtrTollRate->Close();' I# z, ~; `1 |, O% |9 n! V
                        pLocalConnectPtr->Close();
$ t* j1 m/ y; w3 T* ^: s" g- m                        m_nStatus = DB_OK;
, {" t' ^! ~3 z) J) o  a                        return(fToll);
! i! A/ A( I) s) e/ n+ u/ n! N                }9 i4 K& Q' G6 j
                catch(_com_error e)2 k  |4 j5 _3 ~) B
                {
; z4 @" M3 S7 r9 G. n% L3 E% |8 ^4 |' 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());& K0 _, |9 O9 p
                        g_ModelPrompt.Notify();        / {6 E. t) |6 @* X6 B2 ?; J
                        m_nStatus = DB_ERROR;
1 t' N3 D" K& s6 \                        return(fToll);        ) L2 u+ h& t/ u) N
                }/ Q5 o9 T& w: u6 d- V2 }, D
                break;2 {- i1 P  h- K: G* u6 J' y
        default:8 W" B2 D+ E& J/ i, s) d2 E
                m_nStatus = DB_ERROR;! c6 X. h6 l% ?. Z! U4 C
                break;4 t- m0 }! T  a+ V0 k$ m1 O% h& S
        }
6 V# q0 \0 T" S/ K9 u        return(fToll);        , }# F) r. V5 D$ T. |: X# S1 N9 U
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-8-9 04:06 , Processed in 0.037156 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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