|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
: }* J p& e5 b
头文件,加入 #include <afxdb.h>
- e6 h3 G- `, G2 Y4 y& o2 }0 A% y z( S. [) e
void excelTest::do_it()3 u3 j) X+ J5 E1 |) O U$ R1 x) y
{& y3 Q9 Q0 E. Y9 T; n5 f5 ]
1 h% u1 ?3 m( Y( L
// TODO: add your code here: `, `" p& T _6 L% G2 {9 q4 U
0 b, i8 C: |# C8 G* j1 T, t" \7 U4 \
CDatabase Database; // 定义一个MFC的CDatabse数据库类对象Databse
" F5 C! Y: f+ O$ q CString SQLCommand = "select * from plmhome"; // SQL查询语句
5 _! _& d$ E& H* a0 R5 O- D6 p0 g' o DWORD dwOptions = 0; // 设置连接的建立方式 " y. H. D4 Q6 s# n0 r8 n/ \4 O
CDBVariant temp; // 数据库数据通用类型 - p0 Z" Y) P9 W) j4 k/ e
int result; // 储存返回值的变量 7 l4 n. q4 K; e5 L
char cyl_height[133];
% Q4 ^7 M* k8 Z) V6 q7 S- ]9 b2 |) W char cyl_diam[133];
) M' b+ ^/ v6 ?3 s( e1 Z# \ double *cylinderValue = NULL;
- ?, K B# x: Y1 a# X( P5 V2 l$ `1 b/ ^, w; h6 E
try 6 J2 o0 O5 z& q4 t# O
{
6 I# Y8 N3 L4 `8 ]: {# e. T& [ ) {: I* ~5 f5 K6 R2 s9 l
result = Database.OpenEx(_T("DSN=plmhome"), dwOptions ); // 打开数据库 # r2 ]) Q Z4 Y, j% W' C& T
if( result != 0 ) // 如果成功打开
+ I9 j/ _% t) M% m% j7 z9 d$ O, Z$ d9 j {
) J' @( H5 v( i9 M% ~; [$ m! A CRecordset rs( &Database ); // 定义记录集
# e! U2 J8 i* t if ( rs.Open( CRecordset::snapshot, SQLCommand ) != false ) # t* X8 w q* {, [6 u
{
7 P4 U# F5 c+ L# R/ O rs.MoveFirst( ); 2 x9 m; X$ ^. y* r/ E. D
short nFields = rs.GetODBCFieldCount( ); // 获取记录的字段数目
+ N, m3 J9 U9 }. {6 ^7 K while( !rs.IsEOF( ) ) % y0 `$ z3 V; v+ f
{
0 V" I7 R7 B0 D- {7 R7 O cylinderValue =(double *)malloc( nFields * sizeof(double) );
0 \7 e3 J0 i, ^ for( short index = 0; index < nFields; index++ )
) l7 D5 k [! e, L3 J( G( O) w- i; r { $ b! }) p, ^, ~3 P' A8 s G
rs.GetFieldValue( index, temp ); . t/ e' H! Z/ L3 ]$ o
cylinderValue[index] = temp.m_dblVal;
; G( ~1 z; I$ M$ P } 1 J5 {. F+ K1 U9 |
sprintf(cyl_height,"%f",cylinderValue[0] ); 4 a6 E7 [/ H+ g( k/ n
sprintf(cyl_diam,"%f",cylinderValue[1] ); $ N: }. p( t6 |
, R$ M( x, k% A% |! |, s" A rs.MoveNext( ); ( E; t8 ~/ L5 [3 R' l1 s9 ]
free(cylinderValue);
/ V2 w# `, i1 p! y8 s* p }
9 M: q2 V. ]4 o( c* W$ ~% j( P rs.Close( ); // 关闭记录集 $ c; d$ i7 Q# Q; E/ u7 {2 O
}
9 e, H( X2 l# v5 [8 w6 m Database.Close(); // 关门数据库连接 " _/ K: `* z, T- J& O8 p w6 Q
}
9 _+ V1 s) k7 x }
6 ~% `- f" f7 N9 ?( p; F( ?8 E& {- n7 m9 ` t
% n" X0 H' B: {" b4 R- fcaTCh( CDBException *pe ) // 出错处理
, [5 `+ c% ]' h, D) y { 3 O6 ^9 [4 I K! L
AfxMessageBox( _T("Exception!" ));
) L$ I9 ]# j! F3 N8 P AfxMessageBox( pe->m_strError ); 5 ]# Z- b3 Q6 w$ \
pe -> Delete( ); 7 m2 `5 t7 f; C4 S+ g# T/ J9 v
}
: a- B7 d# B/ W: `% p2 i}3 {. L% E' S. W( ~( M2 L! w
8 T$ T6 }( {. Z$ Q! _3 p; F
! x- K1 Q9 x7 }/ D; v# o2 D4 n4 ]
: b0 ?* K O. O2 A' d |
|