|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
/ J* R0 ?+ Q8 m1 n7 j' w头文件,加入 #include <afxdb.h>2 `' x# a8 {! q9 B9 J, n
3 s% N4 n; B6 A8 p8 bvoid excelTest::do_it()9 c; k s( o v. k3 t
{
" m T$ x) @9 T9 |
( C: c# c/ c o! d1 a // TODO: add your code here) t3 E. p2 [2 n5 v
' H& @8 t3 r# r% S- v CDatabase Database; // 定义一个MFC的CDatabse数据库类对象Databse 0 e k7 b _" s8 Z0 n+ j
CString SQLCommand = "select * from plmhome"; // SQL查询语句
( W# U B1 v5 h" P3 f! e DWORD dwOptions = 0; // 设置连接的建立方式
j; k/ ~6 n" W- A5 h CDBVariant temp; // 数据库数据通用类型 6 x( i$ @- K" Z* P! W3 N
int result; // 储存返回值的变量 ^5 b5 G9 o! ^* y7 q1 u' w
char cyl_height[133];
$ G- C! \8 D3 o7 L char cyl_diam[133];
5 y0 u- q: L5 _" w- } double *cylinderValue = NULL; . f4 ^8 l, i. F" U5 |( s5 X/ c$ c$ a
. S9 ?6 F, x1 i; l( \9 P
try
4 @3 x. S) [, Q; B) }+ e! A+ u8 U8 { { ( g* V+ D+ b# {$ V
; e+ L6 H1 X0 W result = Database.OpenEx(_T("DSN=plmhome"), dwOptions ); // 打开数据库
$ Q$ h! `2 f! N4 ^% L- i$ T if( result != 0 ) // 如果成功打开
* B0 Z' L+ A! D5 d8 M& r: J0 q, ` {
! U3 O: ^; I6 l9 ?0 n$ t CRecordset rs( &Database ); // 定义记录集 7 B( T: y; L2 W0 H( J
if ( rs.Open( CRecordset::snapshot, SQLCommand ) != false ) 0 c# {+ R7 f8 m/ K- m
{ . O, D/ ~% k; a) F3 G) ?
rs.MoveFirst( );
& B( S4 N& B3 S( e- e. n s short nFields = rs.GetODBCFieldCount( ); // 获取记录的字段数目 6 `- G/ V0 s. j
while( !rs.IsEOF( ) )
6 _" I/ D& M- ]! R {
6 P. x4 @9 y$ C cylinderValue =(double *)malloc( nFields * sizeof(double) ); $ N1 E t& C) O7 }+ P' I
for( short index = 0; index < nFields; index++ )
: C2 | r, Z" f6 ?2 J {
8 ]# B) ] b; m rs.GetFieldValue( index, temp ); . c3 o# f. }9 E/ A
cylinderValue[index] = temp.m_dblVal; " {7 `" W6 h) N- f
}
* H+ u- G( ^- q6 j _ sprintf(cyl_height,"%f",cylinderValue[0] );
/ m$ P, ?* I7 L6 p, z sprintf(cyl_diam,"%f",cylinderValue[1] );
: |/ y2 X. p6 Z+ f
0 W7 k& i$ p$ n0 `! l rs.MoveNext( );
3 g( M7 ^6 _( |0 l& L) B; T free(cylinderValue); " X8 t7 M# b% G$ Z. {/ r
} # s5 L& U3 o, C; u
rs.Close( ); // 关闭记录集
5 l" @; \' y: { } ( c' R/ h# x: Q2 k6 L+ c
Database.Close(); // 关门数据库连接 3 V! O$ V) g5 [& g- e
}
( D6 c- x; x; q: x' W } ) u. n0 m2 {1 V
) g7 o. V9 J3 V. l n5 z2 k
: e/ D0 B* _- S6 z5 C
caTCh( CDBException *pe ) // 出错处理
/ V$ N$ I# P* x8 V' G2 Q2 r n {
; _3 o4 y1 B: G0 P1 U3 q* j; Q1 G AfxMessageBox( _T("Exception!" )); # ~+ E5 @+ G0 Z" T6 ~8 A4 ]
AfxMessageBox( pe->m_strError ); - D3 O, b6 j) q7 P, l
pe -> Delete( );
7 B. g, q( q; c' O' `" B } 3 @+ y# X5 _( X, Q
}
" l! k, g& w- t$ q8 K0 H% F% Y; s% K. Y( l' D: c- l9 U
8 W1 ^+ i7 v: o g: j
0 o6 w5 N3 Z6 }- A$ ~# M
|
|