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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()# r9 m4 V6 U. S) R% F$ r3 w
{" O( o+ u0 e( y. Y
        _ConnectionPtr pLocalConnectPtr;; S* n: O+ u% Q1 {  [6 S1 N
        _RecordsetPtr  pRstPtrTollRate;( X( y8 D. _7 K
        _RecordsetPtr  pRstPtrSplitWastebook;
0 D  P+ p4 ]  a0 F/ {. F$ d8 F/ G7 }3 @9 S) c; J
        char pcSQL[300];
$ D  A" |0 U! v* G9 |- 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 ",, C6 N0 X$ K" i) K" ?
                g_ModelEntryInfo.m_PassCard.nEntryStationID,
" d# P% I* Y* T0 ?                g_ModelEntryInfo.m_PassCard.nFlagStation1,0 [3 W# l7 D" n
                g_ModelEntryInfo.m_PassCard.nFlagStation2,! d& S7 `3 s9 J. L: ]% F
                g_ModelNodeInfo.m_nStationID,
9 Y7 L4 L" H) W6 \* y                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
; u" b! _! L2 o2 z( j- X                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase
- }) q8 g; b! F/ q% m1 }' i. B) [                );
0 V+ d9 z% B  v+ ?
' v( I. u6 @% Q        try
' l( T2 i1 O+ q) S        {. [' ?6 b' [3 T1 n. E+ ^
                pLocalConnectPtr.CreateInstance(__uuidof(Connection));/ W* F% H& [4 `& }3 e
                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
  }6 O" A" t. r; t0 h                7 t' ?7 P: N8 k# e, v
                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));( J. G9 U, B3 o% U; X: ^, ?. _
                pRstPtrTollRate->Open(pcSQL,
$ N5 \9 }  u! ^$ v# N) C& d                        pLocalConnectPtr.GetInterfacePtr(),
' p" S. ?% x" Q                        adOpenStatic,8 a5 _- a) _1 N9 R4 q2 G
                        adLockPessimistic,
! O: x2 a4 C" c) M3 m                        adCmdText);
% m0 \2 d6 L3 F; Z) w* f                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));5 F! n" h) X$ w/ J4 A- E
                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),
# ~* e) i: G6 A9 O: O. n8 v                        pLocalConnectPtr.GetInterfacePtr(),
' T# \, r: d5 D( l1 Z, }                        adOpenUnspecified,8 F/ T4 Q" U* F+ W4 n
                        adLockPessimistic,
% `& s3 y/ r) _                        adCmdTable);
- J# ]/ |1 f; k6 I8 L5 Z2 {; _% Y! l- [  l' T+ t; T
                pRstPtrTollRate->MoveFirst();
, l# Q' G  K5 h+ d1 C* h( W0 Z                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)% n- w0 t2 h5 D0 Q/ F# A$ [
                {/ i6 }/ o( W& v% q! I) m) w
                        pRstPtrSplitWastebook->AddNew();) g" ]6 P  `6 H2 k: J$ }1 y
                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);
* E8 I# c5 [+ _/ E, V                        ) Q* u/ m% b! X
                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);0 K. i3 f5 c/ G5 C: h
                       
8 c8 H) _& c; T1 `9 M+ |                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);8 B$ \" ?5 Z8 p* y% q
                        ! F5 A) a8 t5 [: ?' K. `/ i+ _
                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);+ v5 ^+ ~9 S, L) W9 d9 a
                       
( G0 j( ?% I$ Q) s" {# U                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);6 @" o6 ?8 Q5 t" r6 k
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
7 r. z9 X0 ]' g) _9 X                        - z% X- ]0 Y! _" a
                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;6 g# f9 B" P0 h+ Z4 D
                       
7 U9 @0 T' C+ F% I$ m                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;0 {" S8 k, }* E, g0 v2 G1 N( h
                       
& v; K3 H: V9 y3 E4 K                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;/ B/ u) j8 o9 i! w" W6 g& l7 l! Y! k
                        2 w; f: B" J3 e- e
                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);
% i- G3 n8 }' |  e& I  ]* l! h                       
2 ^2 m6 }% t+ p                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;
* |$ Q$ _* y% L8 z3 W$ @9 j8 N                       
4 j- J& {! s3 a- |3 |5 f' O/ m                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;! L+ l- O4 c6 ~5 q4 w
                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;
% ~- P: e0 b; i' Y: J, J) \                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;/ O7 U  |" s+ ?- I5 R
                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;8 l- r; X; x# r: Z  X
                        : c5 o, M2 {* k6 i3 m2 I5 a7 v
                        pRstPtrSplitWastebook->Update();
. F" M# Q$ n: t9 g) j7 U                       
  ?0 Q1 l) R! J: m                        m_nTableSplitTollWasteNOCount++;
( k$ u4 \; H" G2 B7 |* J  g& w4 a                        pRstPtrTollRate->MoveNext();$ i5 a. o% o( V; B3 `& F
                }               
* }( g$ W9 F" r# t, _8 d& u% {                pRstPtrSplitWastebook->Close();
  V" L4 I1 u9 S( I                pRstPtrTollRate->Close();2 z% c2 D2 z( P, U* d  ]& b# {) _
                pLocalConnectPtr->Close();, D! U& r/ ~, X9 A- Q
                m_nStatus = DB_OK;
# L' M, ^; s6 ^* e3 P4 N7 S: h! b                return(m_nStatus);        , h* ?; G# G4 k% O& u" ?
        }
9 A8 k$ Y* R3 S( X6 e: p* L4 t9 n3 Y        catch(_com_error e)1 w  l: P# ?% L2 _  B$ M1 Z* H) ^8 z& d
        {
9 }+ y8 R4 O) Z& a$ L                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());
* b, A$ y: i5 f% x/ s; O  ~8 O                g_ModelPrompt.Notify();        7 b3 s2 W4 D! ?
                m_nStatus = DB_ERROR;) }+ u5 k. [/ a5 ]1 q6 j7 g; h" d
                return(m_nStatus);       
6 a; L: q3 k* B7 G( n        }- F3 ?5 S3 w* E9 e, K# t( A' W+ J
}, W; E. L- K- A# t
( A; l: R8 r; R  _* X5 l( T" u
& y9 Q' N/ t8 _; w+ y

% G. x# Z9 Y6 G) Q- W3 @float CObserverLaneDB::GetTollRate()
: `- R3 H8 u4 q/ M! \' F/ r- ]{
# U# `% L+ I! v  P/ W  T3 N. G2 x2 p- u
        _ConnectionPtr pLocalConnectPtr;+ o! L9 `" N4 x2 L/ D5 d# @
        _RecordsetPtr  pRstPtrTollRate;. L. ^8 T& Y8 ?4 f
        _RecordsetPtr  pRstPtrOrganInfo;5 u3 v5 g; K# i' l4 M: H
9 C, ]9 y) M% V* p) s" O" L
        float fToll = -1;7 X  p' X4 x7 W  X# ?/ H
        char pcSQL[300];
' w) ?9 L0 H+ U7 n        int  nSplitCount = 0;
  C# l8 G) n7 d7 l6 v1 G        switch(g_ModelNodeInfo.m_nNodeType)
# k' {( L; P9 l        {
" A' i  L5 a) M, G. R: G# Y' }* ^+ s        case NODE_TYPE_ENTRY:
( \5 j2 q2 Y. o2 ]: _3 d                return(0);
5 _# }/ A. g$ x3 m; }                break;  [" x6 P. ~% N3 `
        case NODE_TYPE_EXIT:
% E, }1 R) a- G        case NODE_TYPE_MAIN_LANE:
! Q# E7 n4 l3 S. B1 X! m1 c; a                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase); w7 P) s5 ^0 f
                {
8 R7 ~5 u2 L5 g9 N' @& r                case VEHICLE_CASE_URGENT:, K5 ]  c" ]. y$ t8 n
                case VEHICLE_CASE_ILLEGAL:6 m% M+ ?$ B! @( n) [; ^6 t! }0 J
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:
& j  b" I. G; Y! B8 y4 \' z  e: I                case VEHICLE_CASE_MOTORCADE:6 e5 f4 F1 t4 V2 H
                case VEHICLE_CASE_COMMUTATION_TICKET:
+ w. ]( Q5 D4 \: H                case VEHICLE_CASE_ETC:
* D. F: ^9 t* ?, |& E4 h5 v                case VEHICLE_CASE_BAD_CARD:
" H. }1 l) C: o2 T4 D                case VEHICLE_CASE_NO_CARD:
' o! w  r% G- {3 v                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",
9 k! Y% W# `- `' U                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,0 J* p: n8 s, U% M9 q; c: m
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);0 H5 N9 _% J- U0 ~6 I4 N
                        break;" k9 |7 c: I8 R  @/ v" R8 h
                default:/ s* k6 N6 d3 F% 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 ",
9 i: q+ a% ~. e1 A) b                                g_ModelEntryInfo.m_PassCard.nEntryStationID,
9 [0 \" Y# H! M8 _                                g_ModelEntryInfo.m_PassCard.nFlagStation1,
* L. |7 q* n: i                                g_ModelEntryInfo.m_PassCard.nFlagStation2,
5 i- C' E- z5 T& r& j  d; b, [2 t% f; S                                g_ModelNodeInfo.m_nStationID,
" q; w0 h9 G/ S6 D7 u0 V                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,- E+ m1 z0 }. M* f9 R7 P, Z
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
; p: ]5 N' R" j* l. ~& S                        break;. O6 L0 Q, T/ `6 B) f
                }
7 \; v8 X2 F: f  w6 _# p               
; i+ h5 J4 t7 j, g# f                try
. X5 L2 X5 L& q1 o' Z                {
2 Y- @6 s& s! S  K                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));* {% Z# C) g. j; c) g$ F
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);4 T+ g; R  q- a+ F0 S$ P6 }3 q& n
                        5 c0 L2 ]7 M: i$ k8 C
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));& o7 l% j4 l3 Y3 L( [9 X. q) }
                        pRstPtrTollRate->Open(pcSQL,, M9 f4 C' F7 ]2 d
                                pLocalConnectPtr.GetInterfacePtr(),
5 U' D1 l9 B9 J+ H                                adOpenStatic,  L9 ~. a  I% N; Z6 k/ V# ~
                                adLockPessimistic,
( e; v& k0 b' P% }                                adCmdText);  k1 L; p: H* j9 L9 q( K* j
                        if(pRstPtrTollRate->RecordCount > 0)
6 e$ [! J( F5 t2 V5 A' R' z3 U0 n8 R                        {
: {/ X. k* i/ s6 P  V- E  X5 t$ a                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;) g) O# L" a, I5 C6 `7 w
                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )
: Z% `4 g+ R/ h0 q! @' a                                {9 i6 ?) [+ d. O2 }! n& u/ F* r
                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));
% ]$ o1 C# ]; ?+ h, S* l                                        pRstPtrTollRate->MoveFirst();4 G) G# H$ P! g0 H( v3 d4 R& K) {( R
                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
7 Z( d( p% {6 N% X7 y                                        {
! f9 f+ Q8 y) @. `2 {" O6 N  c8 _                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);+ v: d  C' b' C/ k4 h9 J8 d3 e
                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));: p( I1 T; T! @6 U6 w3 e
                                                pRstPtrOrganInfo->Open(pcSQL,. K$ j( y% z2 O
                                                        pLocalConnectPtr.GetInterfacePtr(),8 W1 a3 G5 f" J* Z
                                                        adOpenStatic,
3 p  z" @, {& Y1 ~: |, O. y7 m                                                        adLockPessimistic,8 B2 j" X- c: S4 L* ~! a
                                                        adCmdText);8 W; y% a/ ?& c8 V0 b
                                                nSplitCount++;& J3 E7 `% Q! i' j5 s& P
                                                char pcBuf[10];
  S% O" R5 {5 L1 N  {' v                                                if(0 == nSplitCount%3)
/ P5 ~/ f9 Y: d" w                                                {3 q/ _+ z1 T1 w7 c7 x
                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);1 O/ b% _; c; U( G
                                                }else- _" P3 ^0 ?/ F$ j  F  K
                                                {3 n) \& m7 g& P1 c! D
                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);* O3 e4 n2 D: T
                                                }- `4 V3 \/ ?; R/ H" W& X5 h- b$ J. G
                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);/ `4 ?) X" E# s2 _) G& T
                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);
: ?( R0 W4 J5 T  E: T) Z4 w; }! g                                                pRstPtrOrganInfo->Close();
; f: p* w( c( J% n2 B                                                pRstPtrTollRate->MoveNext();& L# L$ }9 k! {) E
                                        }
% K) B- O; w; _* Y) E0 U+ M& C                                }. C: ~8 [0 g4 @1 `3 b# u
                               
( s/ {4 r! i8 l- B                        }else fToll = -1;& i1 {7 t/ r7 l1 [) E' Z5 V, S% S
                       
% e0 t1 C$ p9 h) o3 ^+ b3 y; E( z                        pRstPtrTollRate->Close();# v$ O. s! C( p) ?
                        pLocalConnectPtr->Close();
8 L+ P4 i( Y5 K# q                        m_nStatus = DB_OK;
$ D, o0 o! M' z/ R: }2 V                        return(fToll);
" z4 c8 g* ~" D( i4 H1 I                }' |! R4 E: }9 S* H
                catch(_com_error e)3 T( u9 O1 U) i+ P( C: H
                {
: t7 @$ S9 Q5 @6 A0 ^& U+ y5 ~                        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());
9 Q& w: e+ |! @% o8 P; K/ u4 s) l                        g_ModelPrompt.Notify();        ' S8 p1 P* V8 y3 t, T  y  U
                        m_nStatus = DB_ERROR;
* p, x4 ?& g) g6 a; P  p                        return(fToll);        5 b9 n6 O' d. G- O
                }* u* F! K8 u2 v! q7 w$ U* c
               
0 I9 j. g7 g+ ^7 w                break;, V5 v: R6 ^# K' ~8 h/ j. k
        case NODE_TYPE_OPEN:
0 t. y# ^9 @; {0 [$ B& x                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",, p, F$ ~6 |! K0 I$ U& ?) M. Z# L
                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass" L/ u5 B2 `! _
                        );# e8 _/ s9 b7 Z! c0 `5 e: n2 ^
                try
: B8 x" t7 ^  ?7 R" I! m                {! y# J1 n  |  h8 b& X) N4 m5 s
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
& C, j# h6 V) {% g, ?( h7 _: N                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
- D2 [! m9 b$ _7 v. e* L                       
0 o+ ]- h* c. |/ O                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));# _9 Z8 W/ [. {* w2 d. {
                        pRstPtrTollRate->Open(pcSQL,
; C, U% `  w/ H; m5 x9 i8 S. U                                pLocalConnectPtr.GetInterfacePtr(),8 ]$ t; A5 W. c$ ^; H# I
                                adOpenStatic,
2 B, C& J6 Y" E& H. G2 j                                adLockPessimistic,
3 p. |+ c: \& o                                adCmdText);, u$ V7 l  I: o3 `4 h0 u
                        if(pRstPtrTollRate->RecordCount > 0)" ^4 s% X* i5 f; g5 k' q; L1 N) W
                        {5 E, P" z* a. d
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;; B8 b, U. _/ f4 r7 \$ @- z: I
                        }else fToll = -1;
) \( g3 o# a. D, E  \, Q3 _& \# U                       
9 N5 Z! R% r" D- \4 B7 Y# l; ]                        pRstPtrTollRate->Close();
3 u7 u' C" F+ `$ F6 g& X                        pLocalConnectPtr->Close();
/ A' r- v# o- j8 F$ v1 x  O: F* h                        m_nStatus = DB_OK;) f4 r0 `5 B& @( T# i* J
                        return(fToll);
  E5 s/ M+ p! s! u2 a                }
. e( V% e4 r* M8 t                catch(_com_error e)
6 T; F4 D+ K  ~4 s. ?9 A- \1 t, E" b                {
, M) {+ n; }+ S0 V                        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 C; P% ^7 ]  l0 u7 h- F
                        g_ModelPrompt.Notify();       
3 n- ~0 O- u% o* h# F5 {! Q' |                        m_nStatus = DB_ERROR;
# h/ V+ v0 M$ Y% U* O2 W& s1 @                        return(fToll);        ! b# r9 L) H% ?! [
                }
- M* _+ m% Q3 N( I                break;
8 Y; J% k% ?) Z6 i/ `        default:6 L, ^9 [; U, ~) K; `: r' A) R
                m_nStatus = DB_ERROR;
* D$ P4 }4 |6 p) Y0 @) a! q3 O                break;+ X* s3 q( D1 A0 O
        }" t; z& \0 ~& C; B7 b
        return(fToll);        / J. L5 Y+ `/ |  q3 O
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-14 16:31 , Processed in 0.020199 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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