Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226352
  • 博文数量: 65
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 655
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-31 13:33
文章分类

全部博文(65)

文章存档

2016年(1)

2010年(4)

2009年(4)

2008年(4)

2007年(40)

2006年(12)

我的朋友

分类: C/C++

2007-09-24 21:38:21

在VC6中,提供了CDatabase和CRecordset两个数据库操作相关的类(具体用法请参考MSDN)。
下面是使用这两个类操作ODBC数据库的示例:
 
LPCTSTR CalcScore(LPCTSTR lpszScoreFile, LPTSTR lpResult, int nResultSize )
{
 CDatabase mydb;//数据库对象
  CRecordset myrs( &mydb);//记录集对象
 
 //打开数据库
 mydb.Close();
 if ( mydb.OpenEx( _T("DSN=SQL Server;DB=usr;UID=SA;PWD=passwd") ) == 0 )
 {
  AfxMessageBox("Opening database error!");
  return NULL;
 }
 
 //获取记录
 if ( myrs.Open( CRecordset::forwardOnly, _T(CString("select * from usr..yg_check where gender='") + "' order by id") ) == 0 )
 {
  AfxMessageBox("Faild to open table!");
  return NULL;
 }
 
//读取字段值
 CString strResult;
 while( !myrs.IsEOF() )
 {
  myrs.GetFieldValue( "signal", strSignal );
 
  //裁剪空格
  strSignal.TrimLeft();
  strSignal.TrimRight();
 
  //操作字段
  char tmpStr[8];
  strResult += strSignal + ":" + itoa( stdScore, tmpStr, 10 ) + ",";
 
  //移动向下一条记录
  myrs.MoveNext();
 }
 
 strncpy( lpResult, (LPCTSTR)strResult, nResultSize );
 
 myrs.Close();
 mydb.Close();
 
 return lpResult;
}
阅读(1808) | 评论(0) | 转发(0) |
0

上一篇:system详细分析

下一篇:CPU

给主人留下些什么吧!~~