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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()
; v) b( ^7 k) I0 y; j{4 h) B! J9 P* c) j9 t
        _ConnectionPtr pLocalConnectPtr;
* x# Y% \6 c" P, u( m3 J        _RecordsetPtr  pRstPtrTollRate;
2 i/ K& [- X3 ]1 X        _RecordsetPtr  pRstPtrSplitWastebook;8 k6 @# Q# g9 W6 Z/ w/ u
# d" @+ a% E3 U
        char pcSQL[300];
/ u1 ~$ U; k* a% y- W. Z        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 ",; }! r/ m) H* |9 ?  D) e/ `
                g_ModelEntryInfo.m_PassCard.nEntryStationID,* C  t8 H" b& Z0 }. `3 h4 Z$ n% ?8 h5 g
                g_ModelEntryInfo.m_PassCard.nFlagStation1,, V" \* G. ~: k4 B' k
                g_ModelEntryInfo.m_PassCard.nFlagStation2,  C+ z% P% E- `
                g_ModelNodeInfo.m_nStationID,
( N/ U7 o$ J! P8 S' d1 O$ b, J. G& u                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,1 x7 X+ B" _/ r! A# I1 ^. _
                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase$ p2 o4 E( \$ D4 R9 a
                );
. t1 u- i9 z% `& X; N) Z" B# d5 T$ w
        try$ r0 b& G( k1 }& V5 n
        {2 N0 H5 M" m: d4 w8 O' n
                pLocalConnectPtr.CreateInstance(__uuidof(Connection));( C3 z$ n* N- t* z' k
                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);8 b$ s/ a1 E4 y% N  C
                7 v- X3 t$ ^6 L, Z
                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));! v2 V- C# C% @) U& d" w
                pRstPtrTollRate->Open(pcSQL,
2 M5 _4 C" {7 }& K! h                        pLocalConnectPtr.GetInterfacePtr(),
) C  q0 \: |2 T/ R' o                        adOpenStatic,4 N4 h# F4 W& ~7 A% O. K% ~
                        adLockPessimistic,8 s, n( i" z  Y' `
                        adCmdText);
% G9 q) f4 c3 y* W% D) e) Y& B3 g8 y                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));4 ]0 [) Q& n: l0 t& P
                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),4 G1 w* v+ `) e* v
                        pLocalConnectPtr.GetInterfacePtr(),7 e7 e. p; \2 B1 f8 f! c
                        adOpenUnspecified,
$ m6 d! [) a% ?$ x3 t4 F% f9 A                        adLockPessimistic,1 A$ ~' a" v* b& t3 F
                        adCmdTable);
' ~/ s5 i) c9 {4 K9 A
$ I2 J7 D# h* o  C& b9 ?+ j# t                pRstPtrTollRate->MoveFirst();
  T+ q& q3 f  ~& l( p6 p                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)& \; D5 P+ J! i, R/ i: U4 y
                {! q4 M4 m# x1 F
                        pRstPtrSplitWastebook->AddNew();$ l- f4 a- o  W! E
                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);
# c5 o: J  G( h+ ^                       
- F, y4 C5 F2 B4 J                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);+ f! O; n, `* H/ t5 y
                        ' T3 y1 i# n9 g
                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);5 z8 X# }5 o- L' V! |4 s0 F  [6 C4 i1 b8 V
                        1 B7 h$ Y% \/ ?8 }: ^
                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);
* b; L- q; O: W3 U! A( h                       
' Q: b) ~: v3 W0 Y( n1 o                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);
5 F9 C& V& x# [7 }4 j$ y                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;: m3 I$ ^0 J1 e1 H& m
                        $ u) s1 R# A+ _% V( E9 E' b. t  J
                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;- O6 B$ R8 _3 `$ ~& l
                       
5 F, [3 p. D5 [2 t# S5 Z- M+ g                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;
' O$ Q2 e8 e- s; |3 [                       
6 _/ A  P, `& Y2 O7 F0 ]# |' p                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;
/ H2 G9 [9 v, d                       
  G( N8 }5 M# f                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);
. W5 s4 O6 P2 \$ H" Z! q! t                       
: Z: W0 K' J2 ]+ M, c3 x                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;$ n8 O" J$ P, ?1 T" R! D( M
                       
/ c. U: S, H( `                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;& ~8 x$ T# C5 D; e3 u6 ~
                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;2 |  C6 l. l" G/ @% O
                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;
: W' E7 b0 r2 g7 {- N1 H                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;
" w9 M: Y3 W* E' }/ _                       
9 L- ~0 j+ I5 S! |                        pRstPtrSplitWastebook->Update(); " X7 [5 w6 f  z5 |* M# `
                       
/ D3 J8 u- I: T: n" Q2 o# U- x                        m_nTableSplitTollWasteNOCount++;
* T8 R" C% [3 ^; r2 c                        pRstPtrTollRate->MoveNext();8 {  @3 ]( `" ]- g
                }                + @  z# N. m5 Y+ C
                pRstPtrSplitWastebook->Close();
) k8 o2 l5 i7 D                pRstPtrTollRate->Close();
$ a- `. Z$ D4 h; h0 L) v+ \2 U                pLocalConnectPtr->Close();
5 U% a. K( {) y, H( G                m_nStatus = DB_OK;
3 o! w1 g# B- ~4 ^8 e                return(m_nStatus);        : c. L+ H7 e2 e1 n
        }; S7 N3 a  [# n
        catch(_com_error e)! w5 Z( q: c* O/ _
        {
. m7 w3 a4 p7 c+ {' M8 Q, S  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());0 s2 l- y! r! n5 J" n& L6 S
                g_ModelPrompt.Notify();       
+ `. \$ R2 G# u  M! B# }4 d- y/ T                m_nStatus = DB_ERROR;# o  X5 ^2 g0 J8 k
                return(m_nStatus);       
8 ?& m- K( q9 p" n  _        }
0 E5 R, q# n. M6 w& l}9 G, f: P' g9 A/ q  `, k0 J) L
$ r& u# p& g* U+ |" w: n5 [2 Z
& \. W  t+ q* c: X' d6 h
4 V$ {+ v+ F4 r- C" [6 V& y
float CObserverLaneDB::GetTollRate()6 b: j+ _7 W3 \0 w& o( K3 e
{
% x7 f/ A* E$ m$ W1 _) m: C5 G( ]3 M
        _ConnectionPtr pLocalConnectPtr;
! a$ @+ h+ q  L7 ^" p        _RecordsetPtr  pRstPtrTollRate;! s9 o3 E1 L9 V  T: \0 I
        _RecordsetPtr  pRstPtrOrganInfo;! j& Z& u; I( C9 O

, ~! t3 p" [2 s1 @# A8 I        float fToll = -1;( Y4 e3 K, x! y5 c$ H" f& o( c
        char pcSQL[300];) G) k" }4 t0 X' J8 E- b$ r
        int  nSplitCount = 0;
: G6 ^: S. N1 |9 |5 S) z# S- H0 R- v        switch(g_ModelNodeInfo.m_nNodeType)
6 y% U6 p/ ~. Q6 N        {
) g1 f' q9 ?, p4 O        case NODE_TYPE_ENTRY:. x9 i7 {* g2 g8 `
                return(0);; ?2 r% v8 O' {
                break;3 w; Q3 N2 N- N* ]1 Y
        case NODE_TYPE_EXIT:
  b$ f- G! f6 n' A/ j        case NODE_TYPE_MAIN_LANE:' Z3 l, q* R! R6 P3 c2 Y5 U- _
                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)
! X7 E+ t" y4 R2 x( n9 U% n6 R' p                {" u: b$ U1 _& V8 T/ f0 J; Q) N& K
                case VEHICLE_CASE_URGENT:
, F  I+ Q- g& Y2 g, O2 p' B/ l                case VEHICLE_CASE_ILLEGAL:9 z' @# q+ q3 b1 Y, D+ U
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:
; K. T3 ?* G  Z5 a3 u                case VEHICLE_CASE_MOTORCADE:: |7 T$ Y! [  B/ U6 M) L
                case VEHICLE_CASE_COMMUTATION_TICKET:1 x5 @, w, f1 U+ }! y
                case VEHICLE_CASE_ETC:
% f4 n3 i9 z! ~  |                case VEHICLE_CASE_BAD_CARD:
) Z7 U# I$ o- \. }- t. k, R                case VEHICLE_CASE_NO_CARD:6 U! Q; ?8 R1 |6 h9 B% A# h
                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",  H" M0 i; Y: [/ x
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,0 ~+ A) T) d" _& |7 t+ x
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);' w% `) B% H- ~& s, x# F! i
                        break;/ s1 {" Q' y/ [0 t* W" j3 g
                default:
1 Y" {1 u% I( i$ ~) w                        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 ",3 j1 k  v: q" @, w6 T8 B7 t. h
                                g_ModelEntryInfo.m_PassCard.nEntryStationID,7 b$ l5 v9 d8 Y
                                g_ModelEntryInfo.m_PassCard.nFlagStation1," t' k# t  R1 s; \- e5 _* R& T/ G
                                g_ModelEntryInfo.m_PassCard.nFlagStation2,; P1 d) s" X2 V' ?0 k& l
                                g_ModelNodeInfo.m_nStationID,
0 }5 g+ N' U7 t* i                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,$ k8 U, L6 S+ f( v' _
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
, V$ {8 S. X% J' k# \                        break;. ~& |6 c0 l0 _8 z+ c5 E' U* ^4 R
                }) z5 c( A% ^5 f9 F+ f# R0 v
               
  h6 x/ P3 U+ R) x+ T0 L                try5 B" a; A$ D1 C9 S, }& D( E: i
                {
( E: S0 y" H% S& ?                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
7 s$ W* N2 g, k5 n                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);. K, N3 q, m' W/ V" K0 v$ z  G- i
                       
& W8 o- y; L5 q% ]5 _! x; v2 `) Z                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));  g" Y' b8 ^0 j' q' x8 J6 o
                        pRstPtrTollRate->Open(pcSQL,0 ]0 `6 t- j9 r( G- X
                                pLocalConnectPtr.GetInterfacePtr(),' d1 a( E9 E7 y: J- i
                                adOpenStatic,, {1 v. b# ]) z( o+ j3 f  S& W/ l
                                adLockPessimistic,, @+ ?2 U; M  v
                                adCmdText);
) k+ Z: C, x( ~- Q4 |0 i                        if(pRstPtrTollRate->RecordCount > 0)2 @% \5 ~) b0 ]7 O; Y
                        {9 {- p7 f3 f% ?6 a/ Q# R7 A, d
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
# y3 M: v& \/ p' X$ ^9 Z4 ]( `& C# @                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )1 y" d7 Z$ E) p. ]9 H+ M
                                {
, C3 m1 n8 X: N# q' y, X8 X4 \% K                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));5 Y; \: h: o2 ]  C$ J
                                        pRstPtrTollRate->MoveFirst();& l& j4 U3 Y- f, t1 ~0 i4 q) z8 X
                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)# [* \- q$ ]0 U) F$ j$ Q6 @
                                        {
3 {  o; s- v9 Y. Q% A                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);6 J* }6 i( ~7 Z/ d1 t
                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));
' q& C+ k. d) d! R5 \$ ^                                                pRstPtrOrganInfo->Open(pcSQL,
# L* u; n3 u* b6 r* k; T' S- L- B                                                        pLocalConnectPtr.GetInterfacePtr(),
# U  V, ]- a8 ^3 v" w' L4 |                                                        adOpenStatic,5 r4 K" o5 h: f1 ^* C0 |
                                                        adLockPessimistic,
2 a5 y8 t4 n' w  U5 L                                                        adCmdText);, Q( f) F* A- G' Z5 L: r
                                                nSplitCount++;' Z1 [$ {% }5 i0 ]' j3 d+ @
                                                char pcBuf[10];
5 T& Q: r5 E0 M( |+ S' d                                                if(0 == nSplitCount%3)& U$ T# ~" H& k) ]5 |9 `
                                                {
# g- {' D& S, @# [                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
$ V( g3 v6 P0 i; J) M- Z0 d                                                }else+ j1 S. ~5 M* H9 ~! d
                                                {5 ?! u6 `2 V4 m# {% _
                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
9 T: N* q) v( T! F                                                }
" Z& E8 G, [5 }2 Z- x, @+ l: ]                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);8 r3 @. Z; p: `1 i0 a) q4 J
                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);
# h1 d1 P8 @; s4 ~0 c* }- g                                                pRstPtrOrganInfo->Close();8 J2 h$ |% i) a% w8 s: a$ U2 ]
                                                pRstPtrTollRate->MoveNext();$ [( Q* @& r1 \+ t6 Y, F2 t
                                        }
! b! j6 V, Z. p( O% I  ~, j$ h                                }7 N1 l9 x: E: J1 o. M
                                ' X/ b6 ?, O- }9 o( @. p- T+ J1 m0 W
                        }else fToll = -1;
& P9 }. ?; J5 g7 `                       
& k0 n8 ]1 b0 Q+ G                        pRstPtrTollRate->Close();
9 p+ |* R7 [6 R, \5 _' f& W9 U                        pLocalConnectPtr->Close();
; L& `" c1 a5 f( D+ X0 q2 u; f                        m_nStatus = DB_OK;
9 I5 P1 s/ ~/ p: u                        return(fToll);7 k; d. i+ K2 G% o, n- m
                }
. a7 O7 u% N/ w- }                catch(_com_error e)
* O; {: B  ~; v8 z9 D                {
7 K5 _" T# v) |( V- x+ T' u/ g/ J                        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 J& e2 d3 }% B- E                        g_ModelPrompt.Notify();        6 y, q9 }$ ?  D/ K
                        m_nStatus = DB_ERROR;
$ J6 z3 I- m  \' A4 X$ w( p                        return(fToll);          C, ]3 @* W( b! g/ U! b
                }
. D1 L8 X( O; o6 `4 u0 I               
5 t6 D7 T" K* Y! l; T* e+ J                break;7 N5 s2 K3 o! A7 T% P2 }
        case NODE_TYPE_OPEN:' Y" I9 U! w9 ^+ g% d5 H7 s9 {3 W4 y
                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",; E, Q; q4 _9 O) r
                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass7 @' A- I4 `; p5 G* l1 U
                        );! ^5 c1 j9 E- E0 l
                try
4 c  C" T! b' W' a+ g. w                {
' i2 Q3 L! {+ k  W3 ^                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
$ u# A2 R! B, C: M                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
0 e2 C+ C8 Z( J4 x. \, r: ~                       
$ e& i$ k% O* _0 P, e, Q                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
, Y" V2 @7 g0 t2 H( R4 Q, f                        pRstPtrTollRate->Open(pcSQL,2 g" N5 t* @4 m  e; V: O0 \
                                pLocalConnectPtr.GetInterfacePtr(),
1 A1 H, s9 U+ L: ~- F  l- y+ \; C                                adOpenStatic,
$ b8 J% t3 k) c8 c8 H                                adLockPessimistic,2 X$ ^, D; W+ P- o8 _2 w
                                adCmdText);0 H" X: Z/ k1 w; p5 Z1 E. K
                        if(pRstPtrTollRate->RecordCount > 0)
3 R3 x5 K1 X+ U7 f8 }# B                        {( ]9 B& P3 Z/ M6 A
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;* o( a" x! l1 }4 |/ A" N* P+ q# N$ y
                        }else fToll = -1;0 V) u4 C7 {% U6 @
                        8 g4 T/ j& ?" l3 n- G
                        pRstPtrTollRate->Close();
& s4 A- [; }& P! }                        pLocalConnectPtr->Close();
1 y& A5 `; G9 H! @2 ]' H" a                        m_nStatus = DB_OK;2 b% M/ g: E" v3 X6 s+ r8 j1 P: J
                        return(fToll);
( z5 h/ f/ [; I" w+ m9 a& B                }( `, i' ?4 p  E$ i2 T
                catch(_com_error e)
/ I6 z) G1 I: i0 d7 r" c) X                {
) j' o  [, ]; E  X' r5 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());
# ^; Q% T0 \/ i" P3 ~                        g_ModelPrompt.Notify();       
5 P1 x: o  j- c1 }* W                        m_nStatus = DB_ERROR;6 u& u! N5 q- v* t+ D2 z
                        return(fToll);       
$ Q; I2 r; @7 Y; F# j                }
% ?( a0 g# S& d. F; [# j( n! m/ U                break;. ~: S( O( w% u3 M& }6 k
        default:( T) {8 x) V$ t6 H% j
                m_nStatus = DB_ERROR;- x0 M, Q4 j, s, w) M' A/ a, q
                break;* T2 R  Y. l8 t! x1 m0 i
        }
8 x  s; Q# s3 u2 R* `- `        return(fToll);       
/ S3 |8 Q3 q$ H2 p6 I2 j& o1 Q& o. H. t}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-2 10:36 , Processed in 0.020616 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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