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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()  q* H( q8 A4 h% g: H$ y) P
{  S7 C1 A' Y  _" `& i+ ?
        _ConnectionPtr pLocalConnectPtr;
( |/ @. W( B% i7 n% n5 l        _RecordsetPtr  pRstPtrTollRate;( X, d5 f, U: M+ s9 j
        _RecordsetPtr  pRstPtrSplitWastebook;; V+ @$ x! S0 {, ~

+ I; n1 V# \# a  ]0 O        char pcSQL[300];( E) y' e$ v8 \/ E& G" [' j! n, g
        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 "," E  C  |  p0 ?: Z
                g_ModelEntryInfo.m_PassCard.nEntryStationID,
& H# b0 O* k: U" s! N6 y& T                g_ModelEntryInfo.m_PassCard.nFlagStation1,- W: Z+ t4 }! v1 O0 _; Z7 x
                g_ModelEntryInfo.m_PassCard.nFlagStation2,5 u7 E; I9 a3 N: k( S' Z6 p
                g_ModelNodeInfo.m_nStationID,. h' P+ ?& [6 `1 P! d
                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
2 r: m. D) o4 z% W% ]3 O! C                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase# |3 C% z; x2 g' j
                );' L- ?/ p' @. Y- j# H
* v/ Y7 ~. S& P0 l2 C6 W
        try4 B: N/ Q: h; i. ^
        {
5 T7 c9 i9 Z5 W9 J4 s8 e                pLocalConnectPtr.CreateInstance(__uuidof(Connection));. j7 D6 p1 z- k" H
                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
5 v3 L- i) t" C% g/ f/ l               
+ \) j  t1 Z! E+ C& L0 X/ p* X! @) S                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));' L; a$ O7 r0 H* `# S" C$ I: C* D  Z  I
                pRstPtrTollRate->Open(pcSQL,
$ A  ^4 v' M9 j                        pLocalConnectPtr.GetInterfacePtr(),
( E* K1 X' F5 ]6 V                        adOpenStatic,
4 [% i4 W- _3 v. f2 C                        adLockPessimistic,
" P! ^: v( L+ [, V) D" z8 f& U/ q9 s                        adCmdText);+ W( t# _) W" ?6 A" ?1 c
                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));  U7 y; ^4 ^. m; \
                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),6 y+ n/ H9 A1 z# Y
                        pLocalConnectPtr.GetInterfacePtr(),# j# G) v) ^% I6 Q9 Z/ r: T  Q/ \
                        adOpenUnspecified,% g3 `0 {: Z1 m- b
                        adLockPessimistic,
/ L* Y# [6 [; }* u                        adCmdTable);% P# i$ r" {6 R& Y1 q$ e) J

$ U  k2 C0 a# G  X                pRstPtrTollRate->MoveFirst();. U8 `3 D% @7 ], h! ?/ c' D
                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
# j3 {3 a, G5 @3 q& D6 K" x! i9 B0 K) a                {
- q9 _( v' P0 c" j) x                        pRstPtrSplitWastebook->AddNew();* G3 I3 b1 h! U4 B8 y' i* ~( ?$ F: k
                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);
  t6 R& f( k! B2 `                       
! k" T3 Q0 P: _& f  F  M                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);( `7 n) o  s; y" n
                        8 T9 H, Z/ ?. F
                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);
" l0 z7 z( F3 @& c  \9 I8 a                        % q+ c9 W; g2 g/ C8 A7 A
                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);
" C( E/ f# b: e- X6 W) M                       
& j7 j4 n. p# J) ], B, i                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);! D# K: N, R! I; ~9 L/ v
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
! M; l, I( A5 q0 L, Z/ h% D                       
5 ^/ i5 |6 l7 H. X6 k                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;
# n4 I; v. Y7 j- ~0 ~3 p! n( a# t                        0 |! t5 D; s' r; S4 f2 v- Z
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;' n# D7 q. [2 E  x; V( `+ c& q
                       
( Z; W$ \. ?0 h: l                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;  z" z. k% u  T+ R" L
                       
- ~! `6 V$ M# p; Z' d/ W                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);: w4 m# d0 X5 d/ M( B5 R
                       
( u' }. a' `: ~$ N8 W1 J                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;
" h6 e" N" {# c4 Y0 h% s- ?8 G                       
7 x" }4 z9 {4 _# F. y                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;2 A) W& z7 ~6 b. J
                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;
! ~5 V/ h  V& c                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;$ O/ N. J# Y6 {+ G# {
                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;
  ?) X& z3 N8 a$ D0 m2 T                       
1 `) G  F0 m6 s* K* \3 t2 ]! Q! }' d                        pRstPtrSplitWastebook->Update();
* e; Z- e: m! ?+ e                       
9 f$ q9 G- K3 L1 g! L! l: l# ~- }9 ^3 m+ y; W                        m_nTableSplitTollWasteNOCount++;
8 `7 c( c" M$ J5 u                        pRstPtrTollRate->MoveNext();, B. S6 ?6 N8 W; r- h2 v' R% ^
                }               
. {7 w2 W9 H& P$ q8 |/ W& n0 `5 B                pRstPtrSplitWastebook->Close();
  D4 Q: s2 Y! N! d/ A% z                pRstPtrTollRate->Close();
; v' c; g) {, D/ {! x                pLocalConnectPtr->Close();
" d; z& p. [9 C; U+ p, U! p                m_nStatus = DB_OK;0 M0 p4 _& B; m# b6 ]8 s
                return(m_nStatus);       
! W) B( P2 [, V        }
% m' s9 v: P5 m: i        catch(_com_error e)
! E/ \( a3 D+ g& N- m8 ?+ q9 W        {3 F; t/ a# F7 E: 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());
1 w) D2 b& G" ?; Y6 T$ \" C5 j/ F                g_ModelPrompt.Notify();       
; @: Q* m/ \* w* y) e+ [                m_nStatus = DB_ERROR;, O3 c: J5 |1 W; V7 C; F
                return(m_nStatus);       
% K* [! B- Y0 B7 Y        }+ c- X+ @4 u  }3 ]6 s* T
}
5 P. O' ?1 {( r. ~# H/ S+ H8 A$ D
% M/ k0 Y' p8 z1 `: F) q
! q. F. Z0 W1 y8 h  z2 ]  c
' _2 \. _2 b3 P0 b" Mfloat CObserverLaneDB::GetTollRate()  X2 m1 m$ p6 t4 ~1 |- F1 J( ]
{+ A8 W8 P+ r$ _6 D1 D2 p
/ D- l1 g/ F( E& ]( R
        _ConnectionPtr pLocalConnectPtr;
( a$ g* `0 R& Y( i; j# a# _        _RecordsetPtr  pRstPtrTollRate;
0 _0 l3 k5 [, s5 R, V        _RecordsetPtr  pRstPtrOrganInfo;, l: {, [# C& k& E. v& t" b

7 ]/ o$ ]" l9 [0 R& x        float fToll = -1;/ W9 r6 l9 e( i) Y% h
        char pcSQL[300];( K, l# h% M' j1 G  o$ V# m# J2 U
        int  nSplitCount = 0;
2 \. M& o3 O& I1 q$ {9 A5 c; H        switch(g_ModelNodeInfo.m_nNodeType)- u7 D' h5 l9 P0 `
        {
+ A% Y4 w8 r/ l# K$ E+ e        case NODE_TYPE_ENTRY:4 b5 s$ c5 j) L4 X
                return(0);6 O: x0 P0 Q, V1 d* W
                break;+ I* j6 o- x: B8 ?; q  L( j6 e9 `
        case NODE_TYPE_EXIT:
$ T0 W, x( p' v: A        case NODE_TYPE_MAIN_LANE:5 p, q' n+ ^# h0 W9 q
                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)- K/ }. Q9 Z# [) y! Q
                {
# v: g& R1 I! u7 D6 F6 ~( N                case VEHICLE_CASE_URGENT:
! @6 _4 i# M/ Q7 Q. Q                case VEHICLE_CASE_ILLEGAL:+ H3 L% M# h9 p7 X  a6 C) ]
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:7 M; N4 o" _& M$ Z! Z& w2 K
                case VEHICLE_CASE_MOTORCADE:
  V: g0 f2 N8 v8 ^) P; ^; Q                case VEHICLE_CASE_COMMUTATION_TICKET:  m6 _8 [% N$ X" F6 A# k
                case VEHICLE_CASE_ETC:
  y2 Z  l6 R/ l5 M0 L, i7 }                case VEHICLE_CASE_BAD_CARD:
3 ~! l1 c4 v, v6 \                case VEHICLE_CASE_NO_CARD:
/ Q8 L1 M8 D, L5 B7 I0 L                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",
' j# e/ f; @, v* D& N4 |* l                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
( V  h! ~% i7 j2 i8 ?                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
% Y, V5 V( j, v4 K                        break;
/ }( ^1 [# k4 k- z- B0 }                default:
% S! H( B! E+ Z, B: a                        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 ",# j& O$ y- [  o+ i; l) o$ p7 i
                                g_ModelEntryInfo.m_PassCard.nEntryStationID,
3 \" [9 O' i: S: m. u                                g_ModelEntryInfo.m_PassCard.nFlagStation1,; _6 [" x3 R1 Z% U9 r  a  q! j/ l8 b
                                g_ModelEntryInfo.m_PassCard.nFlagStation2,$ X3 H) y/ v! |  M8 U6 i. _8 d
                                g_ModelNodeInfo.m_nStationID,
' o  k; V* x1 l4 m' y  D: g2 _# S7 _                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
, W- m( z$ t9 x- z: @" L8 ~2 ^                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);# s7 X" O/ F7 b0 \5 H* J
                        break;
5 ?$ N9 N% F2 v; R* Y3 A' m7 Q                }
( L( b6 J2 K4 n2 D               
5 T" ]( {$ j6 v                try  k0 S+ \1 I" h- l
                {: a, P& ]8 N+ R9 [' `) H( Y
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
5 A$ k& k; z! j4 T- b- d4 T4 p& P                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);9 a' ?7 V% l0 `2 r5 k4 ]# F
                       
/ V  }/ y) @+ C0 r9 g                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
4 e: W: c' g. Z4 M3 m                        pRstPtrTollRate->Open(pcSQL,
% d; j5 d% B7 P; t7 x                                pLocalConnectPtr.GetInterfacePtr()," a2 y4 m3 I7 c* F0 R9 k
                                adOpenStatic,+ K! @$ L3 p* z# v
                                adLockPessimistic,
# T. W9 T8 |; Q6 l& g6 F                                adCmdText);
0 M: {) }; y) Z/ Q                        if(pRstPtrTollRate->RecordCount > 0)+ M0 F: X7 s) z' P
                        {+ @4 G; Y( x, e5 U% M7 t8 h0 _5 i- \
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;! D4 [" A' h2 m5 r
                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )
$ D7 e  m  N+ \: T  `8 [                                {/ H0 t' y* K4 v7 W
                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));/ K9 s- U0 I+ V  i& i# s
                                        pRstPtrTollRate->MoveFirst();
* ^/ Q6 O& s. z" s$ F                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
( R' i# E6 l/ ^) [& S0 w* V9 i1 Y$ L                                        {
  Z; z3 M* @9 K$ |7 L  v4 D                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);
) i/ Z. v" x) Y( z0 @0 t                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));
  Z" M) s  O, s& n/ j% H, ^                                                pRstPtrOrganInfo->Open(pcSQL,
: V) }+ x9 X: c% g& j2 `+ |; V. L                                                        pLocalConnectPtr.GetInterfacePtr(),
: x; S" ~: y+ }" L1 A. l8 ~% ]. P) Z                                                        adOpenStatic,) ]& N* o6 Z( l$ _& \
                                                        adLockPessimistic,
6 ]( K5 g/ B8 ]9 G& y                                                        adCmdText);
* M2 r* c) W5 D$ Q                                                nSplitCount++;( x: V; ?/ B$ W5 _
                                                char pcBuf[10];
0 j4 G) ]* h3 U                                                if(0 == nSplitCount%3)
8 r) t! w' L4 v# [                                                {
$ R, Q0 _& d: B7 w) l& w                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);1 R; K( u7 H, c* V2 Z8 e5 u5 J
                                                }else
  ?8 k5 ]1 ^0 v* X9 A                                                {
$ a6 l( k4 {# F- ]. ?                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
2 p" v6 _$ |* b                                                }
* z# l5 A' w7 m" ^) N& P                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);& ~; F7 y/ Z5 M! T6 d. X" D
                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);
: m2 R5 f, K: Q$ W4 P$ u6 Q                                                pRstPtrOrganInfo->Close();
1 {. l/ _* B2 d' x7 E( X+ r0 E                                                pRstPtrTollRate->MoveNext();
, U* C! X  @. j" q6 I                                        }2 M3 e: X0 i4 y& z
                                }
# e$ w9 |" I2 }# y                                ( N5 e! m6 N$ T* \2 U
                        }else fToll = -1;
3 l) T3 _  D  O) {0 {8 p                        ) G% B; ^) d6 u  X' F8 x
                        pRstPtrTollRate->Close();
& F% M6 F. _8 M  o: X/ s                        pLocalConnectPtr->Close();- E1 g; d: A; z) m2 l, M7 P) u
                        m_nStatus = DB_OK;
' P& X5 U  u; w* X& ^0 O                        return(fToll);
1 v. E5 G( E  e, E                }
: ~" w# ]4 k" k# ^# z5 j                catch(_com_error e)# f" j, q/ {" @3 h: A) c# @* ?
                {
0 n. T# N/ s: k/ [! M3 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());
; r5 F# q- }$ U- W  X                        g_ModelPrompt.Notify();        ) J/ y; _8 G# I" l$ h; T: E) d2 x
                        m_nStatus = DB_ERROR;3 E2 n% h7 F9 r
                        return(fToll);        . K+ J4 w2 U2 n. e
                }9 p: F* ~. ]& p5 e1 Y1 f1 c4 g/ M" f
                ; t! j% A6 _4 @
                break;7 n+ \, U, |$ K$ q- ?0 X
        case NODE_TYPE_OPEN:
, z2 z' H. N- y                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",
/ [" ]) U% t  E+ a                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass
% |# V5 q0 q9 w/ M: q% x                        );
# L/ w8 }: J2 e8 h. M. X                try9 t5 E4 }7 K' B' V! Q  |5 }1 [9 n* N
                {4 q4 x! a: U3 ]0 E  N
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));- N  T0 h& ~8 H9 G. f" ^2 E1 g* w
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);4 t$ C9 a) o7 n% D2 [
                        # q6 h. q3 l6 F" r. B- N0 N
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));  B0 F: A, Y3 C: n* i! w
                        pRstPtrTollRate->Open(pcSQL,- Q+ n9 k, q1 X1 i, p7 B
                                pLocalConnectPtr.GetInterfacePtr(),  O% o" m5 J) {6 D8 y/ B
                                adOpenStatic,) L2 N1 H  u: @1 F
                                adLockPessimistic,% ]% l4 [, \- @" E; T/ s" {
                                adCmdText);0 d. O, R8 w8 e: A# B+ _
                        if(pRstPtrTollRate->RecordCount > 0)3 X' V$ t$ _; f$ @. w( B! k
                        {
1 ?4 F# E% |" o4 R5 A) w                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
/ \: O4 Y' s( k6 x9 g; w, x# A                        }else fToll = -1;
, r$ z7 v& m9 F% L                       
1 z# {7 F, o1 A                        pRstPtrTollRate->Close();/ `/ \0 M$ I  H. S1 R8 q1 H
                        pLocalConnectPtr->Close();
  g, Z0 X( o1 G6 _6 m# D# j                        m_nStatus = DB_OK;
& j& X+ V* b- B6 F4 m                        return(fToll);
4 ^" K1 L7 ?) x+ A. E9 {                }+ o% G4 U  `& W( Y" o
                catch(_com_error e)% c; c9 j) x( I; R) Y; p
                {
" ?8 A  b: f& g( b2 a7 y2 m3 n& Y                        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 Y+ C3 i. o6 P' n/ L. e
                        g_ModelPrompt.Notify();        7 b4 T7 n# a, Z: ^& Y* b6 a+ ^. Y4 o
                        m_nStatus = DB_ERROR;2 N; W% w8 W) S! `
                        return(fToll);       
; W/ E8 x& `, _' f& O1 l                }
2 L% _" a5 i2 V. [8 b" z* P- I7 W                break;7 y% n0 V) R9 l9 \
        default:) }* Q+ v; D& p6 k$ E  h) h5 K
                m_nStatus = DB_ERROR;
, `) i; k/ ]8 I, K, B9 r& S+ I; I                break;
2 _0 b0 v, a8 _; \+ [        }
0 b3 `# B! Q; B  \% L" d        return(fToll);        5 ^1 j$ y+ ~) T: l4 }9 H7 j4 G$ ?+ I
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-18 10:19 , Processed in 0.019191 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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