|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
×
8 H7 k9 G& G" j8 C3 n/ v( ]# v头文件,加入 #include <afxdb.h>
9 v; R9 W4 {$ f1 n6 B
7 u) O) I1 e) Fvoid excelTest::do_it()
$ q" @1 k8 z9 G3 H' [8 n0 J x{
0 ^+ m' X" O- F. m4 N# q+ J+ P. H
" c4 o: J& g3 U6 C' \. w5 P3 P; p( @ // TODO: add your code here( a! O: {3 T, }* ^
8 u/ F7 e& g6 h G1 \ CDatabase Database; // 定义一个MFC的CDatabse数据库类对象Databse 6 _( {. z& v' e# y& R1 H0 O ~
CString SQLCommand = "select * from plmhome"; // SQL查询语句
* M1 b' [5 l4 a$ X DWORD dwOptions = 0; // 设置连接的建立方式 / t, i5 a1 ^" E; ^/ a
CDBVariant temp; // 数据库数据通用类型 4 |" f" D7 g! T) P" \5 O0 T7 f
int result; // 储存返回值的变量
. L' f9 \' }( S& A char cyl_height[133];
9 J) a# d& d- P$ n char cyl_diam[133];
t- n9 X3 c4 `( @7 D double *cylinderValue = NULL; " e' b$ S8 H c$ l
# q9 U8 Z4 Q8 ^( |' Q* Q' b try
5 c$ j: c8 @) o* Z" a3 N {
7 L3 g+ Z* P+ V# Y3 d 8 \# K4 G1 s; Q" @) S0 O
result = Database.OpenEx(_T("DSN=plmhome"), dwOptions ); // 打开数据库 0 e7 Z' C; ?! P8 g+ E, ?
if( result != 0 ) // 如果成功打开 $ H0 K& n1 Q* Q4 S0 I
{
3 ] d& i! B9 d8 I: P CRecordset rs( &Database ); // 定义记录集
7 K X( v8 t) Q2 G if ( rs.Open( CRecordset::snapshot, SQLCommand ) != false ) 6 R5 [- O) D1 c% U3 a1 w# p! C
{
; H) ~. @/ B/ h9 {, Q5 u$ l. [ rs.MoveFirst( ); ( ?3 Z, h) ]+ b* d6 A
short nFields = rs.GetODBCFieldCount( ); // 获取记录的字段数目 % n/ P0 p- x9 F( `' h
while( !rs.IsEOF( ) )
# {0 n% h% [5 D& C { 2 \8 U* ~8 C5 P% Y. B& ^8 s
cylinderValue =(double *)malloc( nFields * sizeof(double) ); : {7 L' E+ Y9 M( ^
for( short index = 0; index < nFields; index++ ) 2 s0 B- Q( ~3 i
{ 7 H1 r1 |0 `; H3 t+ t; ~- L/ J
rs.GetFieldValue( index, temp );
. o8 E7 W; o4 D# u) o cylinderValue[index] = temp.m_dblVal;
: V, A7 a) p$ U& a, i9 k: j } ! Y$ @- G7 D+ g7 y; Y2 _5 m: G/ v
sprintf(cyl_height,"%f",cylinderValue[0] );
* v* e* [9 y- ^3 w/ e sprintf(cyl_diam,"%f",cylinderValue[1] ); ) c& ]1 O0 E& M$ B
9 }" ?$ |8 C. Q' \; e; n$ @8 _
rs.MoveNext( );
7 T% t1 |7 C5 P" ]5 [( [ free(cylinderValue);
" H% U4 D* T* N& I* l6 ] } * D+ B, J4 ~& i. e
rs.Close( ); // 关闭记录集 6 x( j' l- x" Q
}
! s- C1 v$ |6 Q& p [6 {& ` Database.Close(); // 关门数据库连接
* C0 U$ b% N5 V' \8 x! B; v } : r" W! q: i' @0 G1 `; D7 K4 R
} . ^6 ^5 @, a; [$ e' e6 f l
: D. I: [5 N0 j% b& P* _
! L+ S, e; P* g; x
caTCh( CDBException *pe ) // 出错处理 ! G! y; V1 c) Y1 g# C6 O
{ 2 m$ w4 M2 g: R6 e3 F6 R5 c s# B
AfxMessageBox( _T("Exception!" ));
) D4 M3 U- S; @9 I0 M6 i# U8 d AfxMessageBox( pe->m_strError );
! j7 E0 }. ]2 @/ a. w2 Y3 a pe -> Delete( ); * c: @ t7 n; }$ I- c
} + @0 O$ C$ H8 u6 }) b5 G& S
}
1 I3 @2 C; R5 Y7 D! y$ P; j; J3 Q# E' e$ g8 S
' P4 L5 l- _4 X/ O5 i- k5 h0 S3 l
& C% T X _; Y: S$ l
|
|