|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
×
/ H5 v, L1 x3 o- p
头文件,加入 #include <afxdb.h>4 Y6 U! F0 F4 p+ z% F0 h" i; [
+ K: N m4 \$ u& [
void excelTest::do_it()
4 x- P( T8 W- G7 ]{6 W+ j5 n5 E8 _* }. S. e% U5 R
& A. u) y7 d* N: {5 k7 d
// TODO: add your code here
/ R1 {+ t/ U! |) Z9 e, S- v; z+ ]- O6 X6 ~% D4 f1 E
CDatabase Database; // 定义一个MFC的CDatabse数据库类对象Databse
" i: x, U: d+ z& _9 f6 O0 R CString SQLCommand = "select * from plmhome"; // SQL查询语句 1 d' l+ S/ U& g5 Z' c
DWORD dwOptions = 0; // 设置连接的建立方式 $ s' v( Y7 b" l% r. H7 ]
CDBVariant temp; // 数据库数据通用类型
, q- j9 T% c: c3 L/ O8 l int result; // 储存返回值的变量
, t1 i+ y- Z2 t7 s5 S# M( `3 z char cyl_height[133]; & l/ O; |( L3 d# ?$ Z
char cyl_diam[133]; ) V' u! J2 G+ W. R2 a; z- Y
double *cylinderValue = NULL; 6 m, B Z* E5 x6 P3 D& ]( Y$ U
& q* w2 q( I% ?3 z& s: ~! R
try
* l7 L9 t2 b7 x! Q) `% P5 } { ' L8 V( P2 Z* H
0 V5 D) @$ |! J# h5 R$ n( {+ B
result = Database.OpenEx(_T("DSN=plmhome"), dwOptions ); // 打开数据库 9 Z7 y+ S3 @, `
if( result != 0 ) // 如果成功打开
1 |+ c+ |. p" X* a% f" q, D/ x {
' G: _4 d# v) _6 {# Q; O CRecordset rs( &Database ); // 定义记录集
' [' N) Q' v. K7 s. H' F9 J if ( rs.Open( CRecordset::snapshot, SQLCommand ) != false )
3 V( y+ C9 R" q, r { 4 E7 d6 T% ?/ F7 D7 V) V
rs.MoveFirst( ); 4 u$ ^( n. }8 ^" o% U
short nFields = rs.GetODBCFieldCount( ); // 获取记录的字段数目 : A! L* N, l# L, d
while( !rs.IsEOF( ) ) + I: d2 m& \1 b/ K; _+ h
{
0 V* W6 `+ {! ?* I, { cylinderValue =(double *)malloc( nFields * sizeof(double) ); 6 o/ L" P& u. j3 p) M
for( short index = 0; index < nFields; index++ ) ( A6 ]- @5 H, w0 a
{
8 u# P0 {" k( { rs.GetFieldValue( index, temp ); 6 l( Z7 P# z! x# ?' t1 H K& S
cylinderValue[index] = temp.m_dblVal;
9 j! Y+ @* F8 b W% l' N% Z& P }
! j V# y/ D' J- s. N# U7 x7 t sprintf(cyl_height,"%f",cylinderValue[0] ); ) _9 U' w: q* {3 a- \
sprintf(cyl_diam,"%f",cylinderValue[1] );
( B7 p; [! w! k e& @6 r* H( Y/ i d+ _( f+ s
rs.MoveNext( ); , K6 o+ a. k# j0 [
free(cylinderValue);
- s6 @# ~8 p! P2 x5 O8 O' T }
" O7 ]9 m! v+ j) n rs.Close( ); // 关闭记录集 7 p# o% Y( h: j3 e
} / n2 [) | a$ d( r: ^4 v
Database.Close(); // 关门数据库连接
" p; {1 A8 F0 _# n! a( C }
/ F4 T. r9 p3 u } 1 T' d% d; b5 T f1 v& |5 h9 k1 H" O
) {; ?' T @2 d( W9 f; s
; R1 V- ~' u" s! X* DcaTCh( CDBException *pe ) // 出错处理
& v$ A1 F, ^% C* g7 R- f" W9 T { # i) I6 d# x( H- X
AfxMessageBox( _T("Exception!" )); . J( B2 Q) @" Z% d Q3 S
AfxMessageBox( pe->m_strError );
/ @& k1 O, `/ a3 q( a pe -> Delete( );
9 M0 G# ~/ D! B }
: M9 d: _; ]' ?3 g* d5 D}
) k6 n, n1 _% {8 F+ M/ [, ^1 c9 ^$ W) K. `% l# Y7 ^0 y8 U
3 S9 J7 @# Y! c* j5 |) j% U
+ \* r9 W, D9 ^; ` h& j |
|