永久关闭

有了开始就有了好兆头,接受新生活也就是接受挑战。
我是一个普通的程序员,在这里开始写下程序人生的苦辣酸甜。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
刚开始学习VC,凭以往经验,先从数据库程序开始入门是最好的。简单,容易上手。在网上下载了几个例子,照着描红起来。几天下来自己也弄了几个例子。现在把自己的一点心得记下来:
1.在StdAfx.h中引入ado控件
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rsEOF")
2.用AfxOleInit()初始化Com
if(!AfxOleInit())
 {
  AfxMessageBox("Ole Initialization failed!");
  return false;
 }
3._ConnectionPtr,_CommandPtr,_RecordSetPtr的使用
//define
_ConnectionPtr pConn;
_CommandPtr pCmd;
_RecordsetPtr pRst;
 pConn.CreateInstance(__uuidof(Connection));//
 pCmd.CreateInstance((__uuidof(Command)));

 pRst.CreateInstance((__uuidof(Recordset)));
//connection string
pConn->ConnectionString="Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=pubs";
//open connection
try
{
    pConn->Open("","","",adModeUnknown);
}
catch(_com_error e)
{
    AfxMessageBox(e.ErrorMessage());
}
//execute sql, fill recordset
try
{
 pRst=pConn->Execute("select * from authors",NULL,adCmdText);  
}
catch(_com_error e)
{
 AfxMessageBox(e.ErrorMessage());
 return ;
}
//display data
try
{
  while(!pRst->rsEOF)
  {
    ((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("au_lname"));
    pRst->MoveNext();
  }
}catch(_com_error *e)
{
 AfxMessageBox(e->ErrorMessage());
}
posted on 2007-12-18 17:34  Niyo Wong  阅读(1055)  评论(0)    收藏  举报