设为首页 加入收藏

TOP

C++ 连接Oracle(十)
2012-12-02 23:00:40 来源: 作者: 【 】 浏览:2342
Tags:  连接 Oracle


    }我的代码是放在MFC一个按钮Click事件里面的:
    记住在处理事件的cpp文件中导入头文件:#include “DBOperation.h”
    [cpp]
    CDBOperation dbOper;
    bool bConn = dbOper.ConnToDB(“Provider=OraOLEDB.Oracle.1;Persist Security Info=True;Data Source=boss”, “用户名”, “密码”);
    if (false == bConn)
    {
    MessageBox((LPCTSTR)“连接数据库出现错误\0”,0,0);
    return;
    }
    //查询
    _RecordsetPtr pRst;
    char sql[255] = {0};
    strcpy(sql, “ select * from boss_test_table2 where rownum = 1 ”);
    pRst = dbOper.ExecuteWithResSQL(sql);
    if (NULL == pRst)
    {
    MessageBox(_T(“查询数据出现错误!\0”),0,0);
    return;
    }
    if (pRst->adoEOF)
    {
    pRst->Close();
    MessageBox((LPCTSTR)“There is no records in this table\0”,0,0);
    return;
    }
    _variant_t vSno, vName;
    while (!pRst->adoEOF)
    {
    //pRst->MoveFirst(); //记录集指针移动到查询结果集的前面
    vSno = pRst->GetCollect(_variant_t(“U_NUMBER”));
    vName = pRst->GetCollect(_variant_t(“USERS_NAME”));
    MessageBox((LPCTSTR)(_bstr_t)vSno,0,0);
    pRst->MoveNext();
    }
    strcpy(sql, “insert into boss_test_table2 (u_number, users_name, users_phone, status, customno_id) values ('0001', 'C+TTT+', '13999000000', 2, 'BPPPPPPPPPP')”);
    pRst = dbOper.ExecuteWithResSQL(sql);
    if (NULL != pRst)
    {
    AfxMessageBox(_T(“插入数据成功\n”));
    }
    //执行删除语句
    sprintf(sql, “delete boss_test_table2 where u_number = '%s‘”, “009”);
    pRst = dbOper.ExecuteWithResSQL(sql);
    if (NULL != pRst)
    {
    MessageBox(_T(“删除数据成功\0”),0,0);
    }
    //执行更新语句
    sprintf(sql, “update boss_test_table2 set users_name = '%s' ”, “C++(www.cppentry.com)反人类、MFC反社会”);
    pRst = dbOper.ExecuteWithResSQL(sql);
    if (NULL != pRst)
    {
    MessageBox(_T(“更新数据成功\0”),0,0);
    }
    CDBOperation dbOper;
    bool bConn = dbOper.ConnToDB(“Provider=OraOLEDB.Oracle.1;Persist Security Info=True;Data Source=boss”, “用户名”, “密码”);
    if (false == bConn)
    {
    MessageBox((LPCTSTR)“连接数据库出现错误\0”,0,0);
    return;
    }
    //查询
    _RecordsetPtr pRst;
    char sql[255] = {0};
    strcpy(sql, “ select * from boss_test_table2 where rownum = 1 ”);
    pRst = dbOper.ExecuteWithResSQL(sql);
    if (NULL == pRst)
    {
    MessageBox(_T(“查询数据出现错误!\0”),0,0);
    return;
    }
    if (pRst->adoEOF)
    {
    pRst->Close();
    MessageBox((LPCTSTR)“There is no records in this table\0”,0,0);
    return;
    }
    _variant_t vSno, vName;
    while (!pRst->adoEOF)
    {
    //pRst->MoveFirst(); //记录集指针移动到查询结果集的前面
    vSno = pRst->GetCollect(_variant_t(“U_NUMBER”));
    vName = pRst->GetCollect(_variant_t(“USERS_NAME”));
    MessageBox((LPCTSTR)(_bstr_t)vSno,0,0);
    pRst->MoveNext();
    }
    strcpy(sql, “insert into boss_test_table2 (u_number, users_name, users_phone, status, customno_id) values ('0001', 'C+TTT+', '13999000000', 2, 'BPPPPPPPPPP')”);
    pRst = dbOper.ExecuteWithResSQL(sql);
    if (NULL != pRst)
    {
    AfxMessageBox(_T(“插入数据成功\n”));
    }
    //执行删除语句
    sprintf(sql, “delete boss_test_table2 where u_number = '%s’”, “009”);
    pRst = dbOper.ExecuteWithResSQL(sql);
    if (NULL != pRst)
    {
    MessageBox(_T(“删除数据成功\0”),0,0);
    }
    //执行更新语句
    sprintf(sql, “update boss_test_table2 set users_name = '%s' ”, “C++(www.cppentry.com)反人类、MFC反社会”);
    pRst = dbOper.ExecuteWithResSQL(sql);
    if (NULL != pRst)
    {
    MessageBox(_T(“更新数据成功\0”),0,0);
    }

        

首页 上一页 7 8 9 10 下一页 尾页 10/10/10
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++类设计的一些心得 下一篇C++ vector MSDN简单入..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: