VS2010、MFC ADO方式访问ACCESS数据库笔记


note1:_nonamespace
这是告诉编译器该类不在一个单独的名字空间中,使用no_namespace意味着不需要在初始化变量的时候引用名字空间。当然如果在应用中需要导入多个类型库的话,最好不要使用no_namespace,以免引起名字冲突。
note2: 创建数据库只能用ADOX方式
note3: 表名不能含有"-"符号
note4:
生成表:
两种方式:法1:          
  m_pCommand->ActiveConnection=m_pConnection;
                  m_pCommand->CommandText=(_bstr_t)m_sql;
                  m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdText);
法2:      _variant_t RecordsAffected;
m_sql.Format("CREATE TABLE %s(序号 Counter Primary Key,\
                              流水号 varchar(30) not null,\
                              功率  varchar(30) not null)",(LPCTSTR)strdate);
m_pConnection->Execute((_bstr_t)(m_sql),&RecordsAffected,adCmdText);


note5:访问有密码的access数据库时,密码放在Provider字符串中;

note6:
  //Open方法的原型:
           //Open(_bstr_t ConnectionString,_bstr_t UserID,_bstr_t Password,long Options)
           //ConnectionString为连接字串,UserID是用户名,Password是登陆密码
           //Options是连接选项,可以是如下几个常量:
           //adModeUnknown 缺省,当前的许可权未设置
           //adModeRead 只读
           //adModeWrite 只写
           //adModeReadWrite 可以读写
           //adModeShareDenyRead 阻止其它Connection对象以读权限打开连接
           //adModeShareDenyWrite 阻止其它Connection对象以写权限打开连接
           //adModeShareExclusive 阻止其它Connection对象打开连接
           //adModeShareDenyNone 阻止其它程序或对象以任何权限建立连接
note7:替换重复记录
try
{
if((m_pRecordset->adoEOF))//如果没有重复数据直接添加新数据
{
            m_pRecordset->AddNew();
    var=_variant_t(long(0));
            m_pRecordset->PutCollect(&var,(_variant_t)code );
            var=_variant_t(long(1));
            m_pRecordset->PutCollect(&var,(_variant_t)data );
            var=_variant_t(long(2));
            m_pRecordset->PutCollect(&var,(_variant_t)strTime );
            m_pRecordset->Update();
}
else
{
  m_pRecordset->MoveFirst();
  var=_variant_t(long(1));
          m_pRecordset->PutCollect(&var,(_variant_t)data );
          var=_variant_t(long(2));
          m_pRecordset->PutCollect(&var,(_variant_t)strTime );
          m_pRecordset->Update();
}
}
catch(_com_error *e)
{
  AfxMessageBox(e->Description());
  return;
}
       m_pRecordset->Close();
note8:创建新表
  try
  {
 
       m_sql.Format("CREATE TABLE %s(流水号 varchar(30) not null,\
                                        功率  varchar(30) not null,\
                                        测试时间 varchar(30) not null)",(LPCTSTR)strdate);
                m_pConnection->Execute((_bstr_t)(m_sql),&RecordsAffected,adCmdText);
  }
  catch(_com_error *e)
  {
     AfxMessageBox(e->ErrorMessage());
              return;
  }
note8:查询表是否存在
bool flag_table=0;//查询表名为"strdate"的表是否在标识,0-不存在,1-存在;
try
{
  m_pRecordset=m_pConnection->OpenSchema(adSchemaTables);
           while(!(m_pRecordset->adoEOF))
           {
              _bstr_t table_name=m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;
              if(strcmp(((LPCSTR)table_name),strdate)==0)
              {
                 flag_table=1;
                 break;
              }
           m_pRecordset->MoveNext();
        }
  m_pRecordset->Close();
}
catch(_com_error *e)
{
  AfxMessageBox(e->Description());
  AfxMessageBox(e->ErrorMessage());
  return;
}


note10:
几个参考网址:
http://bbs.csdn.net/topics/340001609      //这个是版主汇总的
http://blog.csdn.net/fengzhishang2019/article/details/7890041
http://blog.csdn.net/suhuaiqiang_janlay/article/details/5943692
http://download.csdn.net/detail/qq_23992597/8787753    //亲测,在vs2008下编译运行通过;使用了ADO和ADOX






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值