|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
! i& _2 y7 |# m! g. B \* F, T头文件,加入 #include <afxdb.h>
0 X5 X$ \. G; f/ u
0 P( G; j7 g, U5 ~void excelTest::do_it(): D t# x$ ~$ T! @0 p/ t
{% X( J. W0 B! {5 M% O2 M8 H1 v
! C$ i' D3 R2 }% e: w+ Z- \( L
// TODO: add your code here+ X5 O1 R3 ~1 G, @8 w# T! t
8 p- `2 [% k( a" X CDatabase Database; // 定义一个MFC的CDatabse数据库类对象Databse ! H5 O) c5 j6 _( ?
CString SQLCommand = "select * from plmhome"; // SQL查询语句 " u w6 c! B' d! C$ Q! K Z
DWORD dwOptions = 0; // 设置连接的建立方式 : S( @3 ?. K+ p: ?9 l* ~
CDBVariant temp; // 数据库数据通用类型 / Z( g4 M2 f3 R
int result; // 储存返回值的变量 5 K5 v; X2 d. _6 g- D
char cyl_height[133];
, @% s$ Y; S) B4 p( A char cyl_diam[133];
, @. A, M2 Q+ \" l double *cylinderValue = NULL; # \) S2 ~7 ] h7 i( ^- x% {7 m1 \. F
; @( ^; H' W$ f0 g try
9 m8 _+ s$ Z1 H5 _4 Y Y( U, V6 N( n {
2 k. B$ z0 U7 `3 N0 E n7 L 7 S. k0 u& s* l7 w
result = Database.OpenEx(_T("DSN=plmhome"), dwOptions ); // 打开数据库 Q7 ?+ A$ F1 Z# L% b" M
if( result != 0 ) // 如果成功打开
4 X) w# q* o2 t5 ` { ( j/ W4 Y3 u! |5 V6 B3 J
CRecordset rs( &Database ); // 定义记录集 ! v6 D( `- f% ]9 S7 U
if ( rs.Open( CRecordset::snapshot, SQLCommand ) != false )
% ^) i* C" T) w; F7 p {
; o8 O+ s/ y% n. X% p" N rs.MoveFirst( ); 9 k/ ]9 s! S2 E
short nFields = rs.GetODBCFieldCount( ); // 获取记录的字段数目
5 \% F4 U, R9 l, {# ` while( !rs.IsEOF( ) )
0 ?+ d3 ]+ X9 W7 W* H* e2 N {
* M* _" o6 J; W) E9 f5 N4 { cylinderValue =(double *)malloc( nFields * sizeof(double) ); 1 P$ e, y0 \+ l1 u- @% Q0 S R
for( short index = 0; index < nFields; index++ )
1 U! j) L8 \. V* z$ L( |6 Y; {$ G { 8 s- S! ?1 H4 i: B0 x5 l$ @! @$ B; T
rs.GetFieldValue( index, temp );
9 n3 w$ ^1 k2 A5 {/ G# Z cylinderValue[index] = temp.m_dblVal;
& f. O1 _2 B: S9 q; q% e } 0 M% g0 a/ P" x; p3 w
sprintf(cyl_height,"%f",cylinderValue[0] );
/ p2 j" L4 v1 z5 o sprintf(cyl_diam,"%f",cylinderValue[1] ); w' w* Q- ?% ?5 A
2 n1 t s2 Z" c9 b) `
rs.MoveNext( ); ' k# Q* \& w/ I% M3 \6 E
free(cylinderValue);
! B4 K8 {: W2 C1 f0 l }
# S0 }+ P( @- r/ }7 h rs.Close( ); // 关闭记录集 3 U8 R, G) Z3 d+ ~( C* e4 z
} * P6 b2 X) u& ]& H, @- J" q" F+ _1 x
Database.Close(); // 关门数据库连接 . ]5 @, j& o3 J- p
}
S" D0 n- E) W' A2 _7 s& \# \# a }
7 d$ j$ p; f! C- h H) W E: Q# x7 L. D' J: v t. B* i5 p+ i0 {
R- a D: g4 [' |, O+ D( k. j9 O
caTCh( CDBException *pe ) // 出错处理
" t0 ^/ X' W& q3 { {
3 x+ {, L# q( X! ]' t4 @, l! w AfxMessageBox( _T("Exception!" ));
% M, t/ ~$ p3 K9 K4 b; m AfxMessageBox( pe->m_strError ); 0 w2 O- k& k* `3 r, |2 o
pe -> Delete( );
# u( x; v, L$ k0 ^ } / Z2 y9 Q5 A# c. w6 C' E% L3 L! ~
}
0 O/ |- O5 y. X5 b# c" W
; d$ x, k$ Q9 ^- z
- [1 o1 } n; p. e) d3 g; X1 y. e1 J) O7 {, v8 ?
|
|