|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
' m# h5 |( c0 X, D头文件,加入 #include <afxdb.h>. j0 ^7 t$ q: l0 u% C H4 W
5 C* E& A) D1 {; q. r: B
void excelTest::do_it() Y* \6 U) E; ~0 R5 x/ g
{) ?/ E! z2 ], ?( W& |) Q
! f; D G$ e% w5 V' k3 ] // TODO: add your code here
+ R8 F4 T: p9 J1 s+ g8 v, {- N% W* K. p, B. q
CDatabase Database; // 定义一个MFC的CDatabse数据库类对象Databse
& ~5 ]7 N3 u0 n# Q" x2 c3 l! R/ Y CString SQLCommand = "select * from plmhome"; // SQL查询语句
0 W! _0 n( ?6 _. G. t- V% }: B DWORD dwOptions = 0; // 设置连接的建立方式 : ]! j1 m4 g9 F0 H4 B
CDBVariant temp; // 数据库数据通用类型
7 j; j' J6 U8 S1 h, h int result; // 储存返回值的变量
: _! P5 i% P. \3 l5 s' F char cyl_height[133];
0 S$ D8 P% B W* \ char cyl_diam[133];
' n! L' C$ d# ?( E double *cylinderValue = NULL;
% Z; y) \- V+ C- |
9 X, x9 B. u5 Z try
" y7 d: N1 X# d; p( n" C5 V { ! o; C$ R% o* \, g
6 e9 ^8 t1 n1 l H4 P, q) W% W result = Database.OpenEx(_T("DSN=plmhome"), dwOptions ); // 打开数据库 : H* Y/ e% g2 ?( o* D
if( result != 0 ) // 如果成功打开 , p$ B8 n6 _' Z- y& b1 ]/ A: ~3 m+ Y
{
9 s$ _2 g5 l+ H: u$ a6 B CRecordset rs( &Database ); // 定义记录集
7 X9 `2 l9 G, i) O/ u if ( rs.Open( CRecordset::snapshot, SQLCommand ) != false ) ( C; D/ c" R5 q! U; K
{ " Q& ^" A6 n( Y# Z
rs.MoveFirst( );
# H0 I) I8 b; E- @ short nFields = rs.GetODBCFieldCount( ); // 获取记录的字段数目 9 Q& n7 u: n0 s0 R! y/ L
while( !rs.IsEOF( ) ) , U, @7 C1 J6 F. K; O
{
- P9 k6 R/ C0 r' G cylinderValue =(double *)malloc( nFields * sizeof(double) ); , O: m( W6 y l! T/ M! U3 t
for( short index = 0; index < nFields; index++ )
% n+ _( p* u3 s) q { 7 N8 u, ]* G' Y; b% x+ U) W ^
rs.GetFieldValue( index, temp ); & T! Q5 I1 O$ R: H$ |) w5 z* N
cylinderValue[index] = temp.m_dblVal;
8 ~: u, P0 \, [, K }
c. L% c/ A! K- J sprintf(cyl_height,"%f",cylinderValue[0] );
1 A' C( m" i) z8 u @ sprintf(cyl_diam,"%f",cylinderValue[1] ); $ z7 t/ M& V; p% p8 C/ p
9 h# d+ E0 t' I+ Z/ R( O7 P rs.MoveNext( ); / v- w* K$ |0 K9 p& O
free(cylinderValue); " j- L: @1 Z W9 ~
} ! Y7 i4 e& {! T( |( n- N
rs.Close( ); // 关闭记录集 % ]% F1 ^8 ]& y, e) G) \
}
# ]) d) K/ D& w) f7 _; B% b Database.Close(); // 关门数据库连接
! ], m& z0 O* ^/ i# h } 8 c* C( g, r, T( @, z
} ; Z7 A: i. q7 _
' }7 N# ?! V7 ?
/ y' a2 m& m6 G7 k2 ?
caTCh( CDBException *pe ) // 出错处理
* K- w2 n* q7 c5 r7 m# n; L { * C5 d! z) v# z
AfxMessageBox( _T("Exception!" )); / g+ ?2 n6 y) S. a0 g$ J
AfxMessageBox( pe->m_strError );
. V2 `& J6 t6 X) x9 i; ? pe -> Delete( ); " {5 L8 t8 R |% }: N; o
} ( O/ y$ V3 r) \& P! E
}$ o$ B% j8 h. M( ~7 f \$ R
( l4 [7 i5 N* B1 O
c3 K! @9 c/ M* N
* _3 f/ z% H& Q% j2 E |
|