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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()
! e! H6 e5 V& i( S% e3 `+ c1 _{
' l7 F+ O/ a3 i+ z6 \: r" X        _ConnectionPtr pLocalConnectPtr;8 E3 b% S' q4 u1 [' c# H8 K
        _RecordsetPtr  pRstPtrTollRate;
: J7 _% q, k& Q/ r        _RecordsetPtr  pRstPtrSplitWastebook;" s5 k" c3 p5 q" ]

3 q5 u  D; A+ u5 ?1 m; l" N7 ]3 K: h& ?        char pcSQL[300];4 T% }- C6 G  \$ c- b* J
        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 H4 w; g1 o$ u. R                g_ModelEntryInfo.m_PassCard.nEntryStationID,
. n1 O0 a- r. ?8 x                g_ModelEntryInfo.m_PassCard.nFlagStation1,
. o7 e0 O8 C0 k! [7 U+ M- m                g_ModelEntryInfo.m_PassCard.nFlagStation2,
% U7 y  k& r3 t' c                g_ModelNodeInfo.m_nStationID,
9 e- |* }) t; u5 I$ j4 ~6 {* w                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
( E( J3 X% Z* w. _9 G. a                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase
) l- ]- [2 ?3 m2 T0 u' M                );0 ^: J7 N$ j* P: m4 s

0 [( w+ p, r+ Q0 N        try2 m- P0 B" n8 H/ M7 M
        {* B- o. k: H0 c; R+ }6 D
                pLocalConnectPtr.CreateInstance(__uuidof(Connection));1 W1 z. ~- [- O5 M
                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
5 p! V* B  I0 [6 J                5 l) P9 t+ H& r$ [7 F" J  O; _- N
                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
; [) {. h1 s, k( e! }                pRstPtrTollRate->Open(pcSQL,
  d; h% x. P9 g8 e& \0 b                        pLocalConnectPtr.GetInterfacePtr(),) E2 o% C  [/ u2 @
                        adOpenStatic,
$ f0 J+ ]( u6 h                        adLockPessimistic,$ w7 k6 N  E/ ~- \" ^1 r
                        adCmdText);
# B! W$ m0 a8 N% V5 \                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));
; r6 `* v' W8 O6 ?7 ?: A                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),) h& u( P1 T; c) e; x2 Q( ?, E
                        pLocalConnectPtr.GetInterfacePtr(),: L3 |: G+ _' O; X' w9 ~7 P
                        adOpenUnspecified,5 |2 h) R- w; n( U( \- u* y
                        adLockPessimistic,6 j: i" v! ~, T
                        adCmdTable);! k0 c# r% b3 ~3 h/ e0 E
1 X, A: b! M1 E% {$ _: T
                pRstPtrTollRate->MoveFirst();' r  X, i+ [; \! x
                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
/ M7 u5 O# u& m) M2 ~$ _                {/ `2 F& _! [8 g+ T9 k
                        pRstPtrSplitWastebook->AddNew();
! M& T1 U3 f* v& s                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);1 F* u4 b" J! @
                        8 s& P- J: L! C7 W/ X3 Q0 n
                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);
$ W5 x2 I# z6 L5 r/ t. D6 n                       
; x6 ?6 J& q2 W' X- {, E; y                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);5 X6 U* S+ {! _. S" B
                        * L* S% G- D3 S, s
                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);" A6 t( t! Z2 g* a
                       
$ x! Y+ Z6 O0 s2 E) L2 K9 M                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);: L2 D& W2 X  g. G+ ?
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
+ [+ o2 ]4 [/ o* I2 F9 V                        1 G5 v/ a' G$ L, X+ `% o" ~( H7 a
                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;
+ l- b# k6 A: [                       
$ c" J& u( K# y5 ^5 \                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;
5 C  Z7 ]8 A; E% \9 v8 ~' A                        3 S8 Q8 p, D: d1 K, Z. i
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;' s3 p8 f6 x5 n. A8 K$ U( Y
                       
4 V' d% S( Z1 a9 p0 s& |, O                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);3 o/ C% q  z, S) b+ m
                       
; L2 Z0 a0 E4 w7 w! P! O& W. W                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;/ g( H& }* u+ Q; [
                       
5 x8 ]  e3 K: H$ U4 d6 O' ]7 j" t                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;4 c1 u  ~0 J. T0 d& r
                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;0 h' P* `3 N1 D$ [/ P# |
                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;
( m* ?1 P2 k9 K* T5 O& w6 T                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;7 n; Z5 U) O9 U4 j: L2 l; g
                        % y) M& F! I; s7 i! o  q7 G2 g: V
                        pRstPtrSplitWastebook->Update(); . x; E, G. v" S: f( b' a
                       
( a7 U8 C% b# e: Y5 `. R                        m_nTableSplitTollWasteNOCount++;& G3 K  Z1 G1 K  x
                        pRstPtrTollRate->MoveNext();
1 `% V. z6 r; o- K( X6 F& t                }               
% M3 e* h# p7 }2 M# ?6 B                pRstPtrSplitWastebook->Close();8 a0 C# G% t1 w# T
                pRstPtrTollRate->Close();! z8 [$ L! Z( p" u* S# y. |- U
                pLocalConnectPtr->Close();6 L. v' B8 Q, h; Q
                m_nStatus = DB_OK;% ~% B8 o3 R0 G! r  U/ A
                return(m_nStatus);       
: j+ _' i! c+ D3 q8 g        }( z% p7 M/ A! X& {' Y
        catch(_com_error e)
1 v5 c6 Y8 {; y2 k; z9 T( b( _        {. R1 y0 d! z3 o3 M
                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());
- g* K) R& o- P( P- W                g_ModelPrompt.Notify();       
4 L3 ~% Z% k8 x9 q9 A0 |                m_nStatus = DB_ERROR;
3 T8 ]  F8 f8 z* k! p                return(m_nStatus);        $ s: G7 E& Z) Q  t3 m$ z% l( g
        }; H) q+ f5 [7 Q$ a; d0 s
}
' t3 C! S& k* [: I1 L5 F
# Q$ h* |! `- M8 j! Q% K: S2 f+ j1 m0 F/ L6 i: }. n0 @

1 J% X1 j; @) x7 c0 ?( g3 I3 @' ^$ {float CObserverLaneDB::GetTollRate()
. ~7 K+ q1 s% X: i2 {3 c! N{
' L2 o! L& V: h
. C) D) [' n& G5 ?2 ~) e' |. G        _ConnectionPtr pLocalConnectPtr;
0 v5 _4 Y3 F, p        _RecordsetPtr  pRstPtrTollRate;
* V9 ?" Y5 ]8 J- r& w        _RecordsetPtr  pRstPtrOrganInfo;2 g1 F, @: K/ H, f( S1 D: B' l

. O: a% L1 K4 \  q( Y# a/ L        float fToll = -1;* q0 z( s( `1 [% y4 C/ h
        char pcSQL[300];
: Q) `2 w" ?2 g6 w        int  nSplitCount = 0;: G2 x$ t7 W( c) }0 v9 k- H3 ?
        switch(g_ModelNodeInfo.m_nNodeType)$ g6 _3 V) J$ [3 Y3 Z
        {
* y; G0 V) N! q        case NODE_TYPE_ENTRY:5 b% A% w; I+ e' l
                return(0);5 J3 |5 N/ s5 c" W1 d& ~
                break;) w- v# i- K! E  I
        case NODE_TYPE_EXIT:$ r3 E0 Q& x: \: y" y: m) E% i9 ~
        case NODE_TYPE_MAIN_LANE:; ]( E7 ~: |; X4 f( }; n
                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)9 H$ j1 l# J$ d8 f+ y( w
                {
: L/ L1 S/ V" @. w3 f3 j                case VEHICLE_CASE_URGENT:
3 N5 K5 F& X3 E. ~/ r                case VEHICLE_CASE_ILLEGAL:
8 \3 j7 ~. s. Q7 S2 X4 Y# v- \3 I                case VEHICLE_CASE_OFFDUTY_ILLEGAL:
8 x; d: P  N+ l' t; w                case VEHICLE_CASE_MOTORCADE:, C. ^" G1 A# m' B% O3 B
                case VEHICLE_CASE_COMMUTATION_TICKET:$ v7 d  L) X# ]; d
                case VEHICLE_CASE_ETC:" X; w$ \3 B1 o0 ?% G9 {% \1 A6 W
                case VEHICLE_CASE_BAD_CARD:  K7 }, Z2 q! Z6 @
                case VEHICLE_CASE_NO_CARD:& Y1 j8 }, M7 f$ I6 _9 R! W
                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",. e. J' M8 @5 q* J9 E# k+ S
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,: d1 E* s, ~- u" Z& x1 \
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
6 T5 k8 W8 I$ q( @+ k" o                        break;* l# `5 J8 f: G, o, o& i6 F
                default:
/ i  x$ T1 e3 P6 }                        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 ",
8 h/ `2 z' D: p                                g_ModelEntryInfo.m_PassCard.nEntryStationID,
0 R% Q( p! S. ~$ \8 Z  y$ h                                g_ModelEntryInfo.m_PassCard.nFlagStation1,2 p3 H$ L5 _* x/ \
                                g_ModelEntryInfo.m_PassCard.nFlagStation2,# H, v$ _, l$ D& g% X2 {2 d1 @
                                g_ModelNodeInfo.m_nStationID,
) @$ _7 @/ z; n( J2 m' Q! x4 p4 S                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
6 y  G+ S. j% U                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
( z+ x; D1 q$ w& g1 M- y                        break;
- D% i5 t# E6 ?$ s$ [9 P; |                }
- [3 L. [0 v! n2 V) u: X, }               
6 j6 Y/ f4 N% {                try# C% H  U1 e6 ]3 @5 \7 a
                {
, g0 ?4 D6 f- f4 `  v- @5 g                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));3 u- q" ?$ {5 u1 Q* Z6 R
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
. b7 H' d8 Z# d+ g! q                        + m, h$ U$ w5 Z$ P
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));$ z% v- t7 V0 ]3 T4 K# X
                        pRstPtrTollRate->Open(pcSQL,6 ]. L6 R1 G/ l# U
                                pLocalConnectPtr.GetInterfacePtr(),
. t/ m& c; w8 u2 g                                adOpenStatic,
  I+ r# T, r. Y                                adLockPessimistic,5 W) i; j/ A  P' Q: V
                                adCmdText);
, i# M1 C' o# @! O4 Z4 B                        if(pRstPtrTollRate->RecordCount > 0); ]7 _% C/ Q' l, ~6 h  D! M, ~. m
                        {
8 C% q: Q  j/ r3 O+ t5 a/ C; G                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
$ M* k7 E) r! v9 D4 k7 \                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )
9 j) @9 B4 q$ H3 F5 I                                {1 q+ G* z3 P5 G2 F5 V  u  L
                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));
- P% v) B; f% u0 N, k5 R                                        pRstPtrTollRate->MoveFirst();
0 F% W, j( j2 j. \. g5 x7 l                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)& l6 R" ^) p5 s
                                        {
: h5 v( ?$ \# l) v; S6 [8 x                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);
$ K& E+ z( Q5 ^2 Z) L) r; k                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));
( n. @! G2 ]" @, G; z1 n/ l9 i                                                pRstPtrOrganInfo->Open(pcSQL,
8 j4 {- U: J# @* c                                                        pLocalConnectPtr.GetInterfacePtr(),
4 w3 J! h9 F1 t9 M( I: K                                                        adOpenStatic,  s" [/ P; x" P" p2 Z
                                                        adLockPessimistic,
6 W2 M& m4 o' @$ v: z                                                        adCmdText);
7 k8 L+ ~/ i0 X, N7 P; H; o' E                                                nSplitCount++;
& N, \' `3 P5 {8 Y                                                char pcBuf[10];5 X& M5 z9 T; Y. {% v. ?3 N$ |
                                                if(0 == nSplitCount%3)$ R  D2 ]0 K0 V) j& Z
                                                {
" M$ G0 I  A  V9 K5 H9 T5 \- r                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
& z9 v9 j( @0 `2 ^7 r                                                }else
: f1 B* p6 G0 Q# {0 ~7 x                                                {
# ?" t# u3 j: q* b                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);  G! v" o9 ^1 r8 x! h
                                                }
# f* a, q5 u6 t2 R                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);- [$ k/ u- ~3 Q4 t' B
                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);/ F5 @9 _6 c0 u: {
                                                pRstPtrOrganInfo->Close();' }( |9 Z6 l! u3 k1 Y2 k& u
                                                pRstPtrTollRate->MoveNext();
! P+ ~7 Y" U+ F                                        }
1 B. A, R" [) O1 H4 w                                }
" G( o6 L) B0 C" I                               
4 G8 j6 _. E* ?( d/ q# Y5 i                        }else fToll = -1;
& c# ?4 r0 D8 X  z2 I2 R+ ~. d                       
1 D4 O! H# A3 y) F+ p2 y# l                        pRstPtrTollRate->Close();
0 {. P# m+ p  `0 v$ c# y                        pLocalConnectPtr->Close();+ C3 L1 x) h' L5 }- R
                        m_nStatus = DB_OK;+ S  W+ P) i4 y, Y9 ~
                        return(fToll);; g8 q# P5 W/ i/ `5 u$ z
                }
) ~4 o8 l+ W# Z; U                catch(_com_error e)* t/ z% p8 D, f' B
                {
/ j$ l: Z* p" v, u# V8 z6 \                        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());
- F2 T% A6 h6 Q! e1 J, |7 e                        g_ModelPrompt.Notify();       
- P. B: r4 Z' P+ X: ]. W8 F. q                        m_nStatus = DB_ERROR;
5 m/ q8 o, D2 a- @1 E* K. K5 w$ u                        return(fToll);        ) H; P( U3 m  ]$ p5 X7 e2 A( B# Q
                }
* x) B' ?4 V: x5 L* M1 I+ n               
5 m& T6 d& H* B* b                break;
+ u& X$ I( \- l' X, N        case NODE_TYPE_OPEN:) Z& R% D2 S: [# e$ t
                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",+ v3 m/ Y; c: f5 A' Z! X
                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass
; i" s& N* F" O/ }( I2 U0 B4 O9 p' r3 K                        );
8 d5 r$ N- P$ O% P* q. R+ D                try
  K3 h( P4 i  b4 p                {9 V& a7 v# g7 P" m% ^* S, c7 x7 M# H
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
% E8 h# C( n. J! l* m# ~  S                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
) r* y- g! q3 d                        7 F& r4 Z" e# y1 }" ?
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
8 B1 Z& j1 b- `/ z! d) o- {                        pRstPtrTollRate->Open(pcSQL,; c7 h" f/ `- f3 m
                                pLocalConnectPtr.GetInterfacePtr(),7 J. K- F, c; h1 z: @: _8 y
                                adOpenStatic,1 y% E1 D: p* |
                                adLockPessimistic,) w6 t! t/ B) u* i9 I- Y; z: |& P
                                adCmdText);% X& l/ o" G5 A3 D* w( B' b
                        if(pRstPtrTollRate->RecordCount > 0); m1 W5 i5 |( {; R& ?/ k. t1 \  _6 v& e; d
                        {
. F% q1 \3 N" Y8 Z/ V                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;7 ]- }! O8 x9 H( R) W
                        }else fToll = -1;
  }' S7 I5 H0 l1 P  G0 a6 P5 x1 [                       
$ V: B+ g3 \- a                        pRstPtrTollRate->Close();
# b" @; @$ ]' b                        pLocalConnectPtr->Close();" s/ C7 b' l: E% `! P: w
                        m_nStatus = DB_OK;
! G1 b% y. s  p: Y8 G) x2 [( |5 _                        return(fToll);
, M- e9 x/ X$ {; h                }. j, T( o4 T: r3 J
                catch(_com_error e)
$ a; [( y& q( i  k                {) t7 c+ R9 {7 Z. M7 G& G+ 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());
/ @& l+ X5 _2 f0 A) x                        g_ModelPrompt.Notify();       
4 K, t% s  ^/ D4 r* L) H                        m_nStatus = DB_ERROR;
7 g2 F- |2 D7 H% H0 [; \                        return(fToll);       
( |. H7 B: P0 J6 I                }
- O: O/ E1 O3 o  V; R: V                break;
5 v+ w4 x3 O# g, {& f4 w) V        default:8 |& B0 o/ p" h9 l+ l6 ~
                m_nStatus = DB_ERROR;8 }# X, q( Z0 z$ g/ `
                break;
9 G/ [) }1 `7 {7 |, n1 s        }
: L7 B: B2 n. V! c6 P- ?6 |2 n4 |2 L: u        return(fToll);       
8 R. y8 K1 H. x: E}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-18 11:11 , Processed in 0.017242 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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