MYSQL_ADO 接口

mysql> create database school;
mysql> use school;
Database changed
mysql> create table teacher
    -> (
    -> id int(3) auto_increment not null primary key,
    -> name char(10) not null,
     -> );
mysql> insert into teacher values('','glchengang');
mysql> insert into teacher values('','jack');
mysql> use school;
mysql> select * from teacher ;
+----+------------+
| id | name       | 

+----+------------+
|  1 | glchengang | 

|  2 | jack       | 

+----+------------+

******************************************************************************************************************

// kkkk.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "kkkk.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// The one and only application object

CWinApp theApp;

using namespace std;

#import "c:/Program Files/Common Files/System/ADO/msado15.dll" /
no_namespace rename("EOF", "EndOfFile")

#include <iostream>
#include <stdio.h>
#include <afx.h>  //for CString()


using namespace std;
class STU
{
  public:
    char  id[10];
    char name[10];
  public:
    STU(){}
    ~STU(){}
};


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
 int nRetCode = 0;

 // initialize MFC and print and error on failure
 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
 {
  // TODO: change error code to suit your needs
  cerr << _T("Fatal Error: MFC initialization failed") << endl;
  nRetCode = 1;
 }
 else
 {
  // TODO: code your application's behavior here.
  

  STU student;
 
  CString strName,strId;
 

  ::CoInitialize(NULL);  // 初始化OLE/COM库环境 ,为访问ADO接口做准备

  _RecordsetPtr m_pRecordset("ADODB.Recordset");
  _ConnectionPtr m_pConnection("ADODB.Connection");

  _bstr_t bstrSQL("select * from teacher");  //查询语句
  char * query_cmd = "DELETE FROM DemoTable WHERE name = 'jack'";

  try
  {
    // 创建Connection对象
    m_pConnection.CreateInstance("ADODB.Connection");
    // 设置连接字符串,必须是BSTR型或者_bstr_t类型


     _bstr_t strConnect="Driver={MySQL ODBC 3.51 Driver};Server=localhost;"
     "Database=school;User=root;Password=111111121;Option=4;";
   

    m_pConnection->Open(strConnect,"","",adModeUnknown);
    if(m_pConnection==NULL)
      cerr<<"Lind data ERROR!/n";
    // 创建记录集对象
    m_pRecordset.CreateInstance(__uuidof(Recordset));
    // 取得表中的记录
    m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),
    adOpenDynamic,adLockOptimistic,adCmdText);

    _variant_t vsid,vsname; //对应库中的id,name
    cout << "ID    名字 ";
    cout << "/n----------------------------------------------------------------/n";

    while (!m_pRecordset->EndOfFile)
    {
     
       vsid= m_pRecordset->GetCollect("id");
      vsname = m_pRecordset->GetCollect("name");

     
      if (vsid.vt != VT_NULL && vsname.vt != VT_NULL)
      {
        strId =  (LPCSTR)_bstr_t(vsid);
        strName =  (LPCSTR) _bstr_t(vsname);
  printf("%s/t",strId);
  printf("%s",strName);
  printf("/n");     //一个记录完给个回车
       
      }
      m_pRecordset->MoveNext(); ///移到下一条记录
    }
    cout << "/n----------------------------------------------------------------/n";
    cout << "/n请输入你要添加的记录/n";

    cout << "/id:";
    cin >> student.id;
    cout << "/n名字:";
    cin >> student.name;
   
    m_pRecordset->MoveFirst(); //移动到第一条记录
    m_pRecordset->AddNew(); ///添加新记录
 atol(student.id);
    m_pRecordset->PutCollect("id",_variant_t(student.id));
    m_pRecordset->PutCollect("name",_variant_t(student.name));
   
    m_pRecordset->Update();

    m_pConnection->Execute(query_cmd,NULL,1); //用Execute执行sql语句来删除
 getchar();
    m_pRecordset->Close(); // 关闭记录集
  }

  // 捕捉异常
  catch(_com_error e)
  { 
    // 显示错误信息
    cerr << "/nERROR:" << (char*)e.Description();   //被抛出则异常
 getchar();
  }
  if(m_pConnection->State)
    m_pConnection->Close();

  ::CoUninitialize();


 }

 return nRetCode;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值