|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ O! V/ p# L1 ~7 U8 Y% V& ~/ r# |* }
头文件,加入 #include <afxdb.h>
' \. X# q( {5 p, m
# Q. L" q" t* O. V' _* _void excelTest::do_it()' J4 z' J& ? W9 O' k0 y
{
$ _; m( J5 |+ j
( s2 h N, q3 o0 q4 `0 M // TODO: add your code here
/ d, @6 M; T( |, _8 E
6 C( m4 G1 \2 `! k CDatabase Database; // 定义一个MFC的CDatabse数据库类对象Databse
% I/ r P \. ?) N+ G& j' v CString SQLCommand = "select * from plmhome"; // SQL查询语句 / I }' r* V/ p
DWORD dwOptions = 0; // 设置连接的建立方式
+ l; n3 u8 A4 k2 v- R CDBVariant temp; // 数据库数据通用类型
; Z2 i+ O d2 g int result; // 储存返回值的变量
& `* `# j, i: S9 m+ E( I char cyl_height[133];
. R& o4 _- @* I char cyl_diam[133]; + W) y/ l% S6 e! V
double *cylinderValue = NULL; * a, a+ K0 [$ q T: j5 A* m
" _; C$ M/ b# U1 e
try
% }0 a* ~ Z1 @ {
- y; s& [* j7 D! G; V" ]6 w4 W $ y& b/ M- Z$ y2 W6 w3 F1 |# ^
result = Database.OpenEx(_T("DSN=plmhome"), dwOptions ); // 打开数据库 4 W5 |, C9 J& m' h t. }0 v
if( result != 0 ) // 如果成功打开
$ O$ X! F* \) Z) Z6 c( |! [ {
# z5 @; r0 G' Z' H CRecordset rs( &Database ); // 定义记录集 # [3 M* h" \. p. v( G5 O% B/ X: E% o u
if ( rs.Open( CRecordset::snapshot, SQLCommand ) != false ) / ~% M9 D1 K4 Q# ^6 w
{ ! k) p0 e, e1 S
rs.MoveFirst( ); & z! \" [* S+ P% |( J$ ?8 D
short nFields = rs.GetODBCFieldCount( ); // 获取记录的字段数目 ! j7 b7 I0 X! x9 y
while( !rs.IsEOF( ) ) % y: Y7 y% M+ X0 ^7 t
{
$ v f6 B' }( ` cylinderValue =(double *)malloc( nFields * sizeof(double) );
1 V$ n# ~: W% K for( short index = 0; index < nFields; index++ ) : G9 X, K% k+ h7 p& }- K
{ 6 y3 e& [3 b1 j* x
rs.GetFieldValue( index, temp );
! s# I3 h& L7 |( o/ j cylinderValue[index] = temp.m_dblVal; $ n% f$ L( T1 l. t$ r2 t; L9 M
}
9 e/ `# [; s4 j, a0 k( t: L& Z sprintf(cyl_height,"%f",cylinderValue[0] ); ) f/ F/ b$ a( Q/ P2 W2 ^( j! u
sprintf(cyl_diam,"%f",cylinderValue[1] );
2 A3 V. @, H) ]1 u& W8 X9 x3 p* Y$ c0 @, i
rs.MoveNext( ); 1 Y( `0 Y. \4 }" q( F7 D
free(cylinderValue); 0 ` o6 B& ]5 X4 ?4 }
} " V' ^0 E. w4 r- a/ x+ c# c
rs.Close( ); // 关闭记录集 8 @$ P; S8 Z0 e" X$ e6 Z
} " u1 H2 G6 W6 E( m2 V
Database.Close(); // 关门数据库连接 4 U" V6 u6 C$ r8 W* `) {, D1 V7 M3 a
} , c/ L9 N: E0 y
} ) P: q/ r4 J* b
* L5 H1 I% Y* B
( t7 b" |, ^3 T
caTCh( CDBException *pe ) // 出错处理
1 k- B2 Y! _& z& Y( g {
c8 ]( x2 r# x$ H; m AfxMessageBox( _T("Exception!" ));
5 z+ ]( w0 O, Z; y3 Y7 t k AfxMessageBox( pe->m_strError ); , R0 ]- g, \# u" S9 [8 A
pe -> Delete( ); . ^- T4 z' b# `5 c* T
}
7 \) \2 W6 }/ t7 l$ |1 |7 i4 R}0 m# C" N% N& G4 M
2 Q$ i% n. {5 p, N/ z3 o
7 K( ^( Y$ y: I. H8 h1 V
, _ ]' y, ~. u# | |
|