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

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

[复制链接]
发表于 2004-1-28 23:41:39 | 显示全部楼层 |阅读模式
int CObserverLaneDB::InsertTollSplitWasteBook()4 j* w8 F6 V+ _) k* c! Y
{
1 q! Z# e5 J; k# D        _ConnectionPtr pLocalConnectPtr;+ V" ~2 B) @2 v2 V+ b* Z
        _RecordsetPtr  pRstPtrTollRate;5 i2 g" k" d- L1 ]- G+ `
        _RecordsetPtr  pRstPtrSplitWastebook;  H7 J% R3 I3 z0 @2 r+ T# C" H
; ^" O# s/ t% [4 ?$ w% Q) h3 o, o; |; e
        char pcSQL[300];
$ l. w: [8 T! e$ \        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 f6 j- a; e* x  z- Y, N
                g_ModelEntryInfo.m_PassCard.nEntryStationID,% B0 ]0 E" D7 E% ~* l& R1 {" K- N' k2 ^/ f
                g_ModelEntryInfo.m_PassCard.nFlagStation1," N# H0 p* }$ d7 q
                g_ModelEntryInfo.m_PassCard.nFlagStation2,6 a. E& h$ e# U& a
                g_ModelNodeInfo.m_nStationID,
5 B8 i" T+ o% W; n) C                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
3 W/ V# G1 C! q                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase  ^6 Q: M& B) n/ |. }2 `7 P5 f
                );
  |+ w: K' X7 i8 f2 N" w7 x4 n6 H
        try
2 {  U8 ^, q* B        {3 i2 G6 z, z/ t) I! ?
                pLocalConnectPtr.CreateInstance(__uuidof(Connection));
$ l4 m0 ^7 f/ N# U) x8 {5 y                pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);& M# _" q' h/ t/ y2 C
                % S3 ~5 H; \. T3 z6 b
                pRstPtrTollRate.CreateInstance(__uuidof(Recordset));3 I: {/ M. n+ P. x( Q' `1 x
                pRstPtrTollRate->Open(pcSQL,9 w7 L& d; q% f4 m
                        pLocalConnectPtr.GetInterfacePtr(),. |6 `6 L1 P* t* n
                        adOpenStatic,
. F# [9 ~* b2 Q# O                        adLockPessimistic,
/ P* _3 \3 B* K" d( Y2 j7 k' [                        adCmdText);
  T7 w/ D) d* O9 C6 l+ q                pRstPtrSplitWastebook.CreateInstance(__uuidof(Recordset));
# d! `5 `4 G8 w. l' }" I0 P/ l3 u2 q                pRstPtrSplitWastebook->Open(_variant_t("TBL_SPLIT_WASTEBOOK_CURRENT"),6 d- V- r$ N, o8 G, c7 Z" g) j
                        pLocalConnectPtr.GetInterfacePtr(),! c( K9 ?( U' a
                        adOpenUnspecified,$ j) t6 ^; Q) a* y
                        adLockPessimistic,5 L. H- c, Q; o6 k5 @
                        adCmdTable);6 ]( y6 R. S& a/ p5 i* {$ |

7 u. K: f8 |1 |2 v: d) P8 w                pRstPtrTollRate->MoveFirst();4 }" ~$ y. Q+ W% `& [
                for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)  T  `* c3 |% N$ q; o
                {# m. N* d1 V# K5 ?9 O! u
                        pRstPtrSplitWastebook->AddNew();
3 ?+ B! _% r9 S) H8 y9 A                        pRstPtrSplitWastebook->Fields->Item["NET_ID"]->Value= _variant_t((long)g_ModelNodeInfo.m_nNetID);$ u9 g# ?3 W& b) v+ c' @9 p6 Y
                        * D- p' O1 X0 X: H/ A. h, y
                        pRstPtrSplitWastebook->Fields->Item["STATION_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nStationID);& L8 Y' N4 Y, A! J0 f
                        + o: U* d- j( M1 n8 Y# p% \
                        pRstPtrSplitWastebook->Fields->Item["NODE_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nNodeID);1 Z) ]3 `7 a' h4 B0 q8 h8 _
                        7 T0 x2 Q# l6 @* A: L1 J( d. \
                        pRstPtrSplitWastebook->Fields->Item["WASTE_NO"]->Value=_variant_t((long)m_nTableTollWasteNO);: O" T& c7 ]' r# H7 [- P9 W
                        5 z+ D& x, }0 ?5 B6 U
                        COleDateTime CreationTime((time_t)g_ModelTollInfo.m_TollWasteBook.m_nCreationTime);
1 x* u! q9 h3 w% u                        pRstPtrSplitWastebook->Fields->Item["SPLIT_TIME"]->Value=(DATE)CreationTime;5 B& B2 e9 [9 j( Z
                        8 a5 E4 g3 N5 Y) l/ s0 E
                        pRstPtrSplitWastebook->Fields->Item["WORK_DATE"]->Value=(DATE)g_ModelOperatorInfo.m_oletmWorkDate;
9 l# l- ^2 X( y# _# ^% @                        6 Y7 p) }7 D2 u4 R  a: s$ R! R
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_NUMBER"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_NUMBER"]->Value;  m: d% |4 i9 U. H
                        7 r4 x- w: P2 l8 {( l. x* }
                        pRstPtrSplitWastebook->Fields->Item["SPLIT_SEQUENCE"]->Value=pRstPtrTollRate->Fields->Item["SPLIT_SEQUENCE"]->Value;
, Y! a# k3 V, R/ Y) V: i                       
, M) Z1 t- L9 B! p; Y1 I# P                        pRstPtrSplitWastebook->Fields->Item["OUT_OWNER_ID"]->Value=_variant_t((long)g_ModelNodeInfo.m_nOwnerID);
1 J( {  f+ U1 B                       
' |- X  V0 y6 i/ ^' W                        pRstPtrSplitWastebook->Fields->Item["OWNER_ID"]->Value=pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value;
: t+ o+ c( j: D, c7 F8 W; Z. ^                        8 O  G) g/ e3 k* u' g7 ^
                        float fTollReceivable = g_ModelTollInfo.m_TollWasteBook.m_fTollReceivable;; ]' n) Q! F2 ~. A& I8 I
                        float fOwnerToll = pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value;
7 R' [1 D, M, u0 I' ]. A% e                        float fTollMoney = pRstPtrTollRate->Fields->Item["Toll_Money"]->Value;' M! J  s8 Q8 Z; F! x7 [
                        pRstPtrSplitWastebook->Fields->Item["OWNER_TOLL"]->Value= fTollReceivable * fOwnerToll / fTollMoney;4 f7 d. b, K6 }4 f; L8 h2 ?
                        1 A( H) ?0 O9 S
                        pRstPtrSplitWastebook->Update();
- _2 |7 [. w, [2 j# f                       
3 H2 D& P5 X& v1 e; X; e7 b3 [                        m_nTableSplitTollWasteNOCount++;' ^+ c0 r% z% ^+ c9 t) h2 `
                        pRstPtrTollRate->MoveNext();
2 B$ h! k1 Y2 n* y2 \* K# P! B" I                }                , h; X+ P" P. m5 F$ t, d. f
                pRstPtrSplitWastebook->Close();# C8 o4 Z4 A+ c, M
                pRstPtrTollRate->Close();  C1 O' C1 z; Z3 N; q( w
                pLocalConnectPtr->Close();+ P0 V4 a  n& [# R
                m_nStatus = DB_OK;6 P% {& G; |, D, h% r
                return(m_nStatus);       
( J+ }3 m* h3 I9 D9 @7 w( w        }, x& x3 g3 r, {8 |& a2 C5 j- X
        catch(_com_error e)
  }* i1 r& M# z# H        {! w) _0 I! }$ \# p$ |3 U* H( C
                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());
% M( u0 W8 m: X* w5 k  @                g_ModelPrompt.Notify();        ) T) b( ~9 Z3 p: K9 N1 x6 @
                m_nStatus = DB_ERROR;3 _6 \( p0 ]) q) m2 F
                return(m_nStatus);        ! N0 w; H7 U4 J2 e: M; T
        }: m5 K. g+ a4 N. ^8 M
}
; |) @, Z7 ^' ~8 h; @  r; u
1 k: B6 T8 M: u) |' T* d
! ]  [4 M- _+ {8 G7 a/ @. V/ h5 F' |/ w) o. v. @; i; p6 B, v
float CObserverLaneDB::GetTollRate()+ ]$ b4 R2 \' e: i2 \) @4 d7 K
{
7 J- f0 m% P* M$ Z0 t  W, L, c  j& ?" P+ ~# m5 `
        _ConnectionPtr pLocalConnectPtr;" O3 L8 i# \$ U; ^5 j  u- A
        _RecordsetPtr  pRstPtrTollRate;0 I" o6 a7 U7 V' y7 \/ g- v
        _RecordsetPtr  pRstPtrOrganInfo;
3 U# G& M1 s4 i. E  l4 Q' u% v  D. [- T# n8 Q" S
        float fToll = -1;
8 A6 l5 m% K& [        char pcSQL[300];
0 |5 w" _1 p% ?/ W9 U. s% G        int  nSplitCount = 0;
: E. Q8 y) l1 M5 S2 x        switch(g_ModelNodeInfo.m_nNodeType)
) A& T/ t2 s7 r  n0 U7 M        {+ ~6 B" T8 c4 U$ k- l
        case NODE_TYPE_ENTRY:- _6 |4 \/ c8 J' j
                return(0);! @% e2 d# z. c8 I. C# Y
                break;
& u4 F* p( W  X2 X2 X. v) v3 M        case NODE_TYPE_EXIT:
" a$ M' J( L' ?4 M7 z7 R        case NODE_TYPE_MAIN_LANE:  U0 L- z! m" I
                switch(g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase)2 k7 O# e* ]0 i; C: `  u0 t6 m
                {
; I, _# v: a( X  Y2 e2 B% x                case VEHICLE_CASE_URGENT:% V2 F+ L  y8 _) E: }6 S$ q
                case VEHICLE_CASE_ILLEGAL:! A/ N0 D' D# A4 a' w  F5 \
                case VEHICLE_CASE_OFFDUTY_ILLEGAL:
6 E: F7 u; n4 I; ^, S" A3 S                case VEHICLE_CASE_MOTORCADE:
, p: E2 Q: n" K! Q" _4 W                case VEHICLE_CASE_COMMUTATION_TICKET:  f/ ]* G5 e% z4 E" P
                case VEHICLE_CASE_ETC:  P' F6 t4 G: v; @4 p+ a
                case VEHICLE_CASE_BAD_CARD:/ I. d' b0 x4 G  k, q; R
                case VEHICLE_CASE_NO_CARD:- H- k; I1 w4 \: H' g: a2 j0 J
                        sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld and Vehicle_Case = %ld ",; K& a( @  B) U1 O$ N+ V$ ~5 {
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,
: r+ p. b) v% x/ b  I/ p                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);1 N8 f2 S5 s! T) `! [! t" {
                        break;& ?! n& T$ t" Z3 Z& s
                default:8 {4 r+ l0 d! 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 ",
* T3 g- ]1 z8 h9 w% \: u                                g_ModelEntryInfo.m_PassCard.nEntryStationID,
# d8 H1 y# x+ i2 I$ F                                g_ModelEntryInfo.m_PassCard.nFlagStation1,
, J" y- `  i; T4 i8 ?4 V                                g_ModelEntryInfo.m_PassCard.nFlagStation2,$ x- e+ d! f3 |5 X! {
                                g_ModelNodeInfo.m_nStationID,
# a# e/ b3 Y" p- _' Y                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass,+ i: \3 K5 J8 }# F5 {" O( V
                                g_ModelTollInfo.m_TollWasteBook.m_nVehicleCase);
) M+ E* q6 P* ?$ R8 S! m                        break;
& A; d& f& b; c3 W9 S                }
& C2 L+ W2 M, M1 E               
1 |$ b' G  {; c: a3 e                try7 n4 c/ n1 F# }, {$ Q# |/ K# P
                {
) l6 ?; ]. w3 I                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));5 y) @) x* [2 j' p$ L! W3 x3 E
                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
9 i9 b+ O6 Z* K* }6 [5 Q# w                       
& B2 |# |4 r* w0 K. N: |) n                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));$ G  A0 g4 Q3 w9 }* K& t
                        pRstPtrTollRate->Open(pcSQL,
2 k- ?2 {9 ^' S  p' T) E  F# F                                pLocalConnectPtr.GetInterfacePtr(),
( k9 ]+ X! e4 w6 s                                adOpenStatic,
: `) q: ?# J) J! a3 l0 @                                adLockPessimistic,- N, g/ b$ E+ h& \' e, S
                                adCmdText);. I5 C3 u: i. L2 o3 j5 O
                        if(pRstPtrTollRate->RecordCount > 0)- I/ T6 _1 E2 c4 n% N' U
                        {
3 w$ B  u" e# ^+ Y# `7 h  s                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
2 }1 L( Z) q  U) i                                if(pRstPtrTollRate->RecordCount > 1 && pRstPtrTollRate->RecordCount < 5 )( M0 Z# ?& l  h! q& l5 w8 z
                                {6 u8 ^# M3 A1 m' K! c
                                        memset(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,0,sizeof(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo));; e' Y! j! d* X! T
                                        pRstPtrTollRate->MoveFirst();5 V1 a4 M2 D- P0 }" x
                                        for(int i = 0;i<pRstPtrTollRate->RecordCount;i++)
. _$ w. d* h' R2 g; d. O: l9 R' |# `                                        {
& K. t3 s3 b' D6 g* ^. S9 x7 B                                                sprintf(pcSQL,"Select * from TBL_ORGANINFO where MAINOWNER_ID = %ld",(long)pRstPtrTollRate->Fields->Item["OWNER_ID"]->Value);
' ^" d) k  x  \                                                pRstPtrOrganInfo.CreateInstance(__uuidof(Recordset));
# @7 L( y% J4 h                                                pRstPtrOrganInfo->Open(pcSQL,
# m/ H1 v) m0 V: t                                                        pLocalConnectPtr.GetInterfacePtr(),
9 \  W$ y% `2 g1 t9 `* z* |                                                        adOpenStatic,
$ E8 e9 I+ M" Y) h, r& f8 R  o                                                        adLockPessimistic,$ a, a$ y- ]% a' E6 H0 {8 o
                                                        adCmdText);
/ t; Z- _' O' U, z                                                nSplitCount++;
6 f! b% c( P' F1 X                                                char pcBuf[10];
! |0 p, |, L, f/ T) I/ i3 m1 R/ S9 E                                                if(0 == nSplitCount%3)- J0 {# X4 x, d- H7 |. _; X! W
                                                {, f0 n! N* d, \1 x" \# n, ^
                                                        sprintf(pcBuf," : %.2f \n",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
, l" h/ `$ w6 q5 E% A0 W' C                                                }else8 w4 U1 H8 z5 K: i% l; ~
                                                {6 W' s4 k  a+ E3 _' S" Y3 G( P8 t
                                                        sprintf(pcBuf," : %.2f ",(float)pRstPtrTollRate->Fields->Item["OWNER_TOLL"]->Value);
% u( {: {. U$ k. ~                                                }( K' b  s* }+ Y9 C2 P  W
                                                strncat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,(LPCSTR)(_bstr_t)(pRstPtrOrganInfo->Fields->Item["ORGAN_NAME"]->Value),4);
3 g* R- P3 m) Y6 f8 H$ x  W: F6 V9 F' i                                                strcat(g_ModelDeviceInfo.m_Printer.m_pcSplitInfo,pcBuf);" P6 p. [; q1 F5 Z( D7 V  M1 d
                                                pRstPtrOrganInfo->Close();
7 t; F! v/ f! s0 e( y# S! ~                                                pRstPtrTollRate->MoveNext();6 J. |- v* q  z1 Y$ m! {
                                        }9 r! P4 [8 ?  {/ z
                                }
0 |  w. ?9 ~/ d- t+ x                               
- v( S% u& z- y9 p: k+ d                        }else fToll = -1;8 P& `1 K/ B/ @4 g+ ]2 k7 V9 Y
                       
- w' ^9 B/ \! a  h  U% P* G                        pRstPtrTollRate->Close();% s8 G5 x0 E  ~7 l3 }1 r
                        pLocalConnectPtr->Close();
3 ?  r4 ~+ o7 K1 a) ~, C                        m_nStatus = DB_OK;7 p3 K. d' |+ }+ M4 K
                        return(fToll);4 w$ Y# @7 Z1 {: Y3 `9 p
                }6 @2 F5 K: B2 w8 K& B$ D4 C
                catch(_com_error e)
) R6 i7 `8 H4 E. o- o                {. ^' J. A% z. V# A4 n8 H- x  b
                        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 x$ ?" _# _, z: h  t, w# Q) @
                        g_ModelPrompt.Notify();       
, K$ n6 _. \* V$ Z' k& o                        m_nStatus = DB_ERROR;  t" U- J, t. F6 J$ U
                        return(fToll);       
- o6 o: [& n, ]+ d& O9 Y                }
/ N, n0 y6 @; X3 z* a5 J$ H" Q               
! h) l, ]6 R& [                break;, _, X4 s* ^, k. _
        case NODE_TYPE_OPEN:
! P0 B! u, e) B, t6 ?5 B                sprintf(pcSQL,"Select * from TBL_TOLL_RATE where Vehicle_Class = %ld",
, b! h4 |$ C' C                        g_ModelTollInfo.m_TollWasteBook.m_nVehicleClass6 K: w; `& e, }
                        );9 I$ w' T/ g% @! e3 ?
                try9 `# D: [7 D! V& R4 d/ I
                {
4 a* G" I8 k) M+ X                        pLocalConnectPtr.CreateInstance(__uuidof(Connection));
$ C5 n2 ^6 I% W& o1 D                        pLocalConnectPtr->Open(m_pcConnection,"","",adConnectUnspecified);
" g9 `6 z; c& n4 A  \                        8 U/ [& P7 F# K! _: \6 h
                        pRstPtrTollRate.CreateInstance(__uuidof(Recordset));4 d( A' I; d: E# N
                        pRstPtrTollRate->Open(pcSQL,& Q8 W( a+ U- R/ l5 h
                                pLocalConnectPtr.GetInterfacePtr(),8 f4 Z8 c2 f3 n9 p" T% d
                                adOpenStatic,. \4 d& A+ T- X6 |7 i* y: s$ N# N, W
                                adLockPessimistic,
; e6 g" ?7 b7 N" k" J% q                                adCmdText);
$ y# b; r& x5 z% z                        if(pRstPtrTollRate->RecordCount > 0)/ v$ s' T9 p8 J7 x5 n% `
                        {
- F3 ~* u' A: p( q& _                                fToll = pRstPtrTollRate->Fields->Item["TOLL_MONEY"]->Value;
9 O  y( @  s$ P9 Y+ q9 h5 b                        }else fToll = -1;
) Z# I! e$ N- P                        8 e8 ^( E+ u# e7 Z" Q2 e4 }
                        pRstPtrTollRate->Close();
; _' J* A3 c% v9 u, O! P1 J0 B                        pLocalConnectPtr->Close();
; Y& Y# D. D% n                        m_nStatus = DB_OK;/ T3 ]8 T( L7 D5 a, L
                        return(fToll);+ V! v* e, l1 k* M6 C
                }% M" X& p# [  Z  j0 k  K
                catch(_com_error e)6 [' |4 {2 h$ o8 c
                {
) S2 s) R; A% J  a                        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());/ ^/ l0 m) W! w, R
                        g_ModelPrompt.Notify();       
3 m- J3 _; |7 j, y" A                        m_nStatus = DB_ERROR;
' s& v' f+ V& N6 ]! h, b                        return(fToll);       
% Y# m4 \) V2 H. ?* I0 f3 A. C2 M                }( S7 _% r4 x* R
                break;
6 Q7 Z$ v2 o1 l1 D$ O        default:, v+ O8 C- J( q" J  n" m4 U
                m_nStatus = DB_ERROR;
& f% g! t3 ?7 }8 M+ x( k                break;
; I4 _" ]: |! {- N+ C6 R8 K$ h7 }4 y        }" s# B8 k6 |3 b
        return(fToll);       
3 ]" {; e5 Q0 U# `& P, D6 `" V}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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