|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
, v8 \) j; V$ N$ }5 [; d
头文件,加入 #include <afxdb.h>
, t7 v% k6 q1 W" U1 ]8 [# |5 [7 w
( L8 J F( _2 N1 R( y9 z% \; Nvoid excelTest::do_it(); g, U3 W& X" z: V
{/ J+ j @$ n! ?4 I$ r. g
4 s/ ^% A3 v6 R# t
// TODO: add your code here/ N- f3 |' |6 d3 u9 m
# a8 ^9 A e; f$ I
CDatabase Database; // 定义一个MFC的CDatabse数据库类对象Databse
, B$ c& G3 C5 c, P. ~) [0 Z2 b CString SQLCommand = "select * from plmhome"; // SQL查询语句 5 c7 Y+ E% g9 @6 s% v
DWORD dwOptions = 0; // 设置连接的建立方式
, c) R# ]+ S6 n8 T CDBVariant temp; // 数据库数据通用类型 ! m( ]$ V2 F; [2 |3 X
int result; // 储存返回值的变量
5 A4 g' g& L4 _6 B4 P/ \6 F char cyl_height[133]; 2 z% m( T' w* q0 I
char cyl_diam[133]; . n# f' {* f0 S: e- q6 I7 f
double *cylinderValue = NULL; 7 y- [2 q, [2 X, P W% `
) h9 |& @3 @6 i3 ~ try / f# p, p) ]) B% \/ `% i K! m5 ^4 }
{ 1 K/ H# S$ ]) [' [
& {& H+ h4 }( L3 D( C8 ]2 Z8 l
result = Database.OpenEx(_T("DSN=plmhome"), dwOptions ); // 打开数据库
2 H: u0 @8 V1 s# P0 x/ \ if( result != 0 ) // 如果成功打开 + Z4 |# @8 D. |+ ^# u/ t* D% N' x
{
4 d: k* K# f1 G+ c: N( W CRecordset rs( &Database ); // 定义记录集
. N1 Q- i! T h# o if ( rs.Open( CRecordset::snapshot, SQLCommand ) != false ) % x+ J1 Q0 S1 n9 R; w
{ ) P3 N# L; W9 y
rs.MoveFirst( ); 5 ~$ P$ g i; ~2 \" e& ?
short nFields = rs.GetODBCFieldCount( ); // 获取记录的字段数目 & c9 x8 B0 b+ m
while( !rs.IsEOF( ) )
! b* K" j4 C$ _& @3 |5 _7 J- X6 c1 B { 9 H; {8 ]0 W% d8 w, p; V1 E
cylinderValue =(double *)malloc( nFields * sizeof(double) );
! ]5 X% Y9 N3 W" m" {8 s, ]" h2 x for( short index = 0; index < nFields; index++ )
2 e# |) t# ` ]( j1 m/ x { 2 ]( w0 Z2 s- i
rs.GetFieldValue( index, temp ); Z: C6 b4 f6 ~9 n$ ^
cylinderValue[index] = temp.m_dblVal;
0 L8 G9 N, D x! s" V" x! d } 7 L- c8 a; [( i" `* K& S
sprintf(cyl_height,"%f",cylinderValue[0] );
Y9 d: I- S7 c( u sprintf(cyl_diam,"%f",cylinderValue[1] );
1 E( }; F! g5 x7 I& h" _ F$ z1 \- d9 ~' V
rs.MoveNext( );
! L$ N! i* c7 b. ]7 X' m) i free(cylinderValue); " Y5 i9 G t; v y* S3 S7 Z2 E
} / d* ~9 l: t1 |
rs.Close( ); // 关闭记录集
$ S+ [; ?9 C$ e6 y3 }/ u( o: N ^! c } 1 s& }8 w% u% i7 s' W
Database.Close(); // 关门数据库连接 # {8 w, U% ^4 N8 L
}
9 k- y- P; Y8 E8 C0 n }
& {% E) w# U2 \* u
5 P; x+ s9 m! D r4 D! T0 s+ T! J. O) F0 ~- Y
caTCh( CDBException *pe ) // 出错处理 - P6 t% Y8 V4 _
{ 9 C: F( q' _. t. _1 l+ H& U
AfxMessageBox( _T("Exception!" )); ! O; H8 d- A3 C4 A' i: b
AfxMessageBox( pe->m_strError ); 0 o2 |$ a7 r/ ?3 h" p" H
pe -> Delete( );
$ g% e$ D5 n" E- W& B) b: ? }
4 i4 B& \/ ?: o2 J: u. u0 q}3 l r. l) B H( X& n/ s* ~
: X, i4 P. z* ~4 H& X, U( B) d( f
2 j; H: l) ?, h& U( c, m1 N6 W2 ]5 \% |+ }7 f
|
|