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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()" j9 [7 u7 r' q" c1 `
{7 }/ Z& ?% ~8 o% X
        _ConnectionPtr pLocalConnectPtr;
& _+ H& D2 b* B1 H  _4 n  T        _RecordsetPtr  pRstPtrTollRate;
0 C' K9 a7 H) f3 r        _RecordsetPtr  pRstPtrSplitWastebook;
8 n" D3 p9 \5 C& t. c7 |; Z8 V* [, m& c( a
        char pcSQL[300];, r' N. i3 e. g3 K; 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 ",
( x) q) Q) o1 r, z. ~$ h  U                g_ModelEntryInfo.m_PassCard.nEntryStationID,
5 u( g6 T, x! j6 U6 l                g_ModelEntryInfo.m_PassCard.nFlagStation1,
# `+ S7 x0 d3 x) t                g_ModelEntryInfo.m_PassCard.nFlagStation2,- J% v& t2 ^+ L! H+ m2 v6 z  X) S- G
                g_ModelNodeInfo.m_nStationID,
  Q( I/ A3 h5 c1 \$ w; M  D, S                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass," i( k! G& n8 Z
                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase2 ?) m9 e9 @$ f  M
                );4 @3 b9 R8 ?, D  R/ w

0 v7 J; e4 \, r1 W1 g        try
1 J0 X' [. y5 v        {
% |" ~9 k" b% z" B( g- l' o# H                pLocalConnectPtr.CreateInstance(__uuidof(Connection));
+ ], n+ x) G! J. x                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);( F1 e$ Z# q  V4 t9 ~! {9 V
                7 n, L3 c, A  Z: ]& t
                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
3 Q9 H! W" r2 C; q# p) H& M2 T                pRstPtrTollRate->Open(pcSQL,' H6 E, `+ O  c+ I. W. r
                        pLocalConnectPtr.GetInterfacePtr(),2 z  J8 z" E# `3 Y' f5 V
                        adOpenStatic,+ ^. ?3 o+ H. l+ s- c. }
                        adLockPessimistic,
& C& F' X6 T  c1 U# Y                        adCmdText);3 G& U" N  H$ P" w3 ]9 [
                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));* F3 w. v4 i( G
                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),/ O5 ^3 `! n4 m8 a
                        pLocalConnectPtr.GetInterfacePtr(),
# n  M+ Z7 E0 v1 K8 w! N                        adOpenUnspecified,
# T1 v8 a" |; u- n5 `                        adLockPessimistic,
! T2 Z, ^  S9 L- M/ B( m/ A                        adCmdTable);) f! E2 j0 |2 z4 [  |

$ n* J+ y5 g, O: [                pRstPtrTollRate->MoveFirst();/ I1 t8 ~# o# N" v
                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)# k$ e6 e) U" d9 ?) i
                {9 P% J' J% [. H* u
                        pRstPtrSplitWastebook->AddNew();
% T" O3 N- s8 [/ D% t                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);
8 ], ]9 P5 z" ~( `3 }0 @                       
; i  T2 m, A/ U& ~2 `/ H                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);
$ Z8 e# m6 D2 T  c9 s6 {                       
+ t+ K3 s* f: x5 e4 |: D# c8 @/ W                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);
* p' s7 T* I& p. H                        ) O" a. Q; t+ Q6 y! O9 y
                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);) T% }( F! t: P! O& R: R" }
                       
$ D$ I! e' D& z( e" @                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);7 j) f" [) }; o( j4 l# U; j5 d! `0 O# D; d# i
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;
5 J) _( z, U  M9 h9 v9 ]                        $ `& D0 y# A! R
                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;
2 O( h; [% G9 \$ X( i" ^                        ) U7 r5 t( z" j8 p' w
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;
: L) B/ o2 e+ Z0 i. C                       
$ y4 z. m' s2 J                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;
9 C1 @+ v7 g9 U8 x. T) A# I                       
  \5 \* E) J3 Q* Q5 B$ |0 J                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);# W2 ]/ q, g6 N  A
                        ; R/ \3 G& d9 E: F
                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;
) t# g! `+ ?# P                       
: A, ~' A9 L. \, {- i3 G( ]                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;4 P3 V2 \% ]9 B4 @
                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;7 b" q& k( z2 i. r
                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;
" N& C& ^1 m( _2 k' @                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;
7 k+ q- j( f2 V. a1 L& R                       
  X" p$ u$ R9 a& z/ ~                        pRstPtrSplitWastebook->Update();
- j8 r" w3 w4 {' U% f: z                       
3 C* S/ T) N0 _8 X" l: x1 h                        m_nTableSplitTollWasteNOCount++;
! x8 s  h4 }7 y                        pRstPtrTollRate->MoveNext();
6 A5 d+ X/ k, F0 j# Z1 N                }               
% \3 q1 }/ `9 k; e6 M( v/ e                pRstPtrSplitWastebook->Close();
9 s7 i2 r  f8 q- {                pRstPtrTollRate->Close();# E: N+ c9 d1 h4 N% M4 {' F
                pLocalConnectPtr->Close();
! i# T% h, H2 O' s# S$ K                m_nStatus = DB_OK;
1 v& b6 w2 E" P3 z  U  V5 N                return(m_nStatus);       
9 o/ A4 o& j. z' l# S        }
% C2 b+ j7 n  p8 r" H$ {* A7 a( H1 \        catch(_com_error e)) S9 r9 l3 A8 d" G
        {, c5 s" I; X( J! a) P$ W
                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());6 T8 X& q8 |5 y) e/ N" ^
                g_ModelPrompt.Notify();        " `& X5 r6 R8 H8 _0 ~0 k
                m_nStatus = DB_ERROR;
" d' i# d* S% c& t1 s9 o, J+ k                return(m_nStatus);          ^/ o  w8 y% u' @; v; p
        }
" f6 [' k" W/ T% l, _! H}
& l5 E: }, i& R; G1 V# _; q  r+ d( Z

! w4 s8 ~+ m5 ^$ e% ?# X  K% d5 n2 N7 S/ H. c6 M! Q
float CObserverLaneDB::GetTollRate()
* }% o4 y7 l* Z. i  @, n{" X! p0 i1 m' L/ q
& n& I2 O3 \' B' e: a: c
        _ConnectionPtr pLocalConnectPtr;
" X: G- _$ U3 `9 M  @        _RecordsetPtr  pRstPtrTollRate;4 E& `9 S# `+ }$ F  D
        _RecordsetPtr  pRstPtrOrganInfo;
4 n+ H) p/ y+ {5 K/ e
+ w* W6 o* C( _0 [/ n: w1 U6 f$ h$ x        float fToll = -1;
. X6 ?# {; }8 w2 S/ U' E4 X        char pcSQL[300];
& i- i1 Y* j8 V8 O1 B5 J9 D) c; K        int  nSplitCount = 0;1 l1 Y0 k/ c; O+ n
        switch(g_ModelNodeInfo.m_nNodeType)
6 l, r4 U0 y0 b$ E- _; B  c        {
3 T' q0 l; @7 p. c+ ~        case NODE_TYPE_ENTRY:
- U% k7 i8 }  d+ L2 O2 K0 _6 `) i                return(0);& A% v6 e0 p) |  j3 Z. O! ?
                break;& e1 j5 \# H( f& @" }5 P
        case NODE_TYPE_EXIT:
. n2 r8 K- I6 g/ U/ T/ w+ l$ v        case NODE_TYPE_MAIN_LANE:/ u$ o1 Y! z3 k
                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)
' c% ~0 ~& y% s; X/ @8 M9 X                {* b# O' ~# O8 r& b8 u# S
                case VEHICLE_CASE_URGENT:/ H3 g1 z1 B& ^2 @; k
                case VEHICLE_CASE_ILLEGAL:5 N3 d& Z8 o( w9 q6 S' s, a' @7 t
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:* R" _  M# e2 ]/ T% _, W
                case VEHICLE_CASE_MOTORCADE:
& D! {' [" ]5 s                case VEHICLE_CASE_COMMUTATION_TICKET:+ D( z# J, V, a1 c3 x; Q5 }
                case VEHICLE_CASE_ETC:
" x  h9 t# O1 P# ]) r                case VEHICLE_CASE_BAD_CARD:
! u+ B3 {# Z5 p6 ?* {                case VEHICLE_CASE_NO_CARD:
+ K: K& S: M& a* b                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",
& p$ u2 t0 a6 p/ U1 p! q" u6 I& o+ q. r                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,  U' a- [4 J0 g$ m; z9 c
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
2 K4 l' S7 K! V                        break;! o# H. z% Q! i6 |" l: w- _
                default:: C& O5 z+ A* W" e& L  `& {. h
                        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 ",
5 D) w) G1 R3 n; P$ M1 J6 O0 \7 n# i                                g_ModelEntryInfo.m_PassCard.nEntryStationID,
7 P) v, U" J/ j% m0 E                                g_ModelEntryInfo.m_PassCard.nFlagStation1,/ M: a0 q2 e) X7 t& q& g. g+ d
                                g_ModelEntryInfo.m_PassCard.nFlagStation2,& v/ M* }9 g9 k# l
                                g_ModelNodeInfo.m_nStationID,
" B2 W/ Q- t0 S- c; f                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,9 d& b/ z. W6 e. H5 [
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
: B# x- F8 I8 Z: p                        break;* |) g7 g* ?- N1 u) W
                }
9 m( G; s) C6 `8 P7 `4 }               
  \1 x$ r% [: I                try
0 ?/ u0 a& T$ ~3 U2 h6 D                {
8 U- t$ Y1 j0 v                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
) x5 y2 c" ?1 A- ~- `- J# `' Q: W3 L* G                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);- G! ^9 n8 h3 Y2 ^3 C
                       
- D' V3 K7 `5 u5 ~9 M0 Z+ B" D                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));
. f, h1 [8 {( V" g( V                        pRstPtrTollRate->Open(pcSQL,3 z/ w" v8 A. u1 E( D6 x* c
                                pLocalConnectPtr.GetInterfacePtr(),
  m' y4 u# E+ d7 _                                adOpenStatic,
8 f- ]  R2 ]7 g) g5 d                                adLockPessimistic,  P% f" X& F" F6 @4 I, Q" X& t5 x
                                adCmdText);6 J* T8 `/ j/ ^- K( g, }
                        if(pRstPtrTollRate->RecordCount > 0)
7 }* Y6 ~$ n" p- X% K, ?                        {
: T+ I0 s. Y  Z                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;! M9 o2 E4 r' b. K! ]
                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )  X' u' _; C9 }
                                {
" W0 y9 I( {+ w7 x6 M+ Z                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));$ t: m+ K8 u0 D6 X- l" N# D/ K5 j) M, H
                                        pRstPtrTollRate->MoveFirst();
" j: L- u% _7 R- j3 g4 W1 c1 O2 j                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)  f$ |0 }6 {& ?) [  N6 l9 c' q. C8 A
                                        {
/ p2 }6 m6 d) {% P5 d                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);
' V, a! {4 u+ N- n, ~8 H                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));  C( n% j. A! V, O7 h2 w2 V
                                                pRstPtrOrganInfo->Open(pcSQL,
: [' M" M: S1 o3 S% [& h* l                                                        pLocalConnectPtr.GetInterfacePtr(),
  ?, I! X  E/ L0 I4 O: k                                                        adOpenStatic,# N3 ~7 \( v; g
                                                        adLockPessimistic,
  w2 C2 T1 k) a7 Q# }1 F" O                                                        adCmdText);! v+ c8 @" j6 O# i/ k& S2 C* G
                                                nSplitCount++;
+ _9 N/ ^% L) z  G7 F                                                char pcBuf[10];4 n* N/ P2 T* ]1 j$ |
                                                if(0 == nSplitCount%3)
! w' i; H4 u' c1 v; R7 H* ]                                                {
: V, Y% S& |2 `" Q& D                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
2 Y- n. J+ V; G  e( G                                                }else
3 u4 l7 z2 ~6 k0 N                                                {8 |2 Q0 t# C, ], q
                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
- T: G4 g( `! K2 M- {                                                }; X7 b! x9 f  i9 q) B
                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);
. k$ B7 Z' C& E5 i$ A                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);! L0 x6 Q1 i' F8 }4 c
                                                pRstPtrOrganInfo->Close();
0 U5 E% {  Y# v" K  `5 K% P                                                pRstPtrTollRate->MoveNext();! T" [0 H( p9 _
                                        }% q0 P$ g+ o2 G% L7 F8 P, @
                                }; V  A. [: f9 m- ?: E
                               
3 T+ v. j3 _( o3 y. C                        }else fToll = -1;  B1 T& r4 a0 ]) d
                       
1 P- k; [) a3 }1 s8 r5 ]8 ?% s& R                        pRstPtrTollRate->Close();
0 n5 ?& e) i3 S5 D5 N                        pLocalConnectPtr->Close();! e- _# H) L( V8 k7 O
                        m_nStatus = DB_OK;' p# C" h7 ?# H& W, z
                        return(fToll);" `, P* j. w8 L4 L6 F
                }
. `- C# e5 n8 `6 c$ o                catch(_com_error e)" _* S& {) i8 N* w' Z
                {( y) q! |. f7 _* b% 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());* j# R5 _8 T- d- N- z
                        g_ModelPrompt.Notify();       
2 R9 m6 @/ p# [3 P8 F: h- J                        m_nStatus = DB_ERROR;0 ?' U* ~7 O) f' w. b/ H4 b6 q
                        return(fToll);       
: v  T+ Q& U) w% z0 M                }
. U; I. K- g: Q8 r& Q1 K2 V& b               
, A. T% y/ R$ V/ U# s+ k                break;
/ U  j  Z  Q" f7 n9 t% v; h        case NODE_TYPE_OPEN:5 C4 v/ b2 Y4 o9 H" L9 o
                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",/ V1 e7 s; k3 j9 O, J+ x) R
                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass
; c! ?, _" a% z4 K0 k' g7 ]                        );
  d8 n+ ~' h& q  p3 W                try
4 @! x) k% V2 M/ |+ f$ L7 U8 T% k                {$ \* K( j4 k4 |. z* W6 g  n8 y
                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));8 k# e! w- u; [( u  o
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);* g" P1 P# c3 t' ]- d
                        9 i3 B4 q8 E- F$ m! C; n
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));- R+ G  n) j$ w
                        pRstPtrTollRate->Open(pcSQL,. e$ Z( j8 H/ ]  \; l7 _- u! S1 M
                                pLocalConnectPtr.GetInterfacePtr(),
1 h* O7 b$ R; @- L$ @* h                                adOpenStatic,- N: U  O, x4 q! w" q. u
                                adLockPessimistic,
' H$ G# K& T' c' n' c- g( H7 g+ l                                adCmdText);2 v+ \0 l; v" x# M# o
                        if(pRstPtrTollRate->RecordCount > 0)
; B' J: ^, n  D# v7 H& x                        {4 Y! V" v& r4 p
                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
! n. e% j% U* o7 ^( [                        }else fToll = -1;
" v+ o8 `% o2 N2 O% o0 I                       
' @7 \+ p% e  B8 q& ~6 `                        pRstPtrTollRate->Close();
. R- x& W. K. X: O9 U* H$ a5 u, h" f                        pLocalConnectPtr->Close();" b( G- l6 r, T* P8 s# h8 J
                        m_nStatus = DB_OK;- m7 w' g) K3 E' M' ~% F$ e
                        return(fToll);& H) ~( r  d5 U0 P+ I
                }
2 l! c7 b; R* f. f  ?5 N                catch(_com_error e)
: i6 _. p' z4 m                {
+ s. U0 u: D& e7 G. E' g                        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());
+ D  b! p8 ^8 c  @4 Z                        g_ModelPrompt.Notify();       
9 |% `+ t: B2 ^* W5 \! Z( J6 i2 {                        m_nStatus = DB_ERROR;
& n* g; o3 o; k2 {& W$ S- K  s                        return(fToll);        : l7 ]' J) a3 P! L, A
                }/ x% J: l6 y. J( x: v
                break;) ~/ W- }( ~8 u# o; ^
        default:. a2 W( W  m# ?' I, q0 h
                m_nStatus = DB_ERROR;
' s+ I: ~; q: N; Q+ t" X0 ?6 l                break;
/ B' |# ]5 \3 {$ m5 M        }: T  S, [3 ]" M, X! ]
        return(fToll);        / [% z7 l; D8 I6 u3 q1 I7 u
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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