您的位置:首页 > 数据库

以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之一连接数据库

2012-11-11 15:18 726 查看
// ADO.cpp : 实现文件

//

#include "stdafx.h"

#include "XMLwithSQL.h"

#include "ADO.h"

#include <iostream>

#include "Dlog.h"

#include "DDlog.h"

extern CString name;

extern CString pwd;

extern CString tname;

// ADO

IMPLEMENT_DYNAMIC(ADO, CWnd)

ADO::ADO()

{

    //m_pConnection = NULL;

}

ADO::~ADO()

{

}

/*!!!!!!!!!!连接数据库代码!!!!!!!!!!!!*/

BOOL ADO::Connect()

{

    Dlog log;

    log.DoModal();

    if(name == "" || pwd == "")

    {

        AfxMessageBox("用户名和密码不能为空!");

        return FALSE;

    }

    else

    {

        try

        {

            m_pConnection.CreateInstance(__uuidof(Connection));

            //创建连接字符串

            m_pConnection->ConnectionString = "Provider=SQLOLEDB.1; \

                                              Persist Security Info=True;Initial Catalog=master; \

                                              User ID=; Password=;  Data Source=(local)";

            //连接数据库

            m_pConnection->Open(m_pConnection->ConnectionString,

                (char*)(_bstr_t) name, (char*)(_bstr_t) pwd, adModeUnknown);

            AfxMessageBox("连接成功");

            return TRUE;

        }

        catch(_com_error e)

        {

            AfxMessageBox("账户名或密码错误");

            return FALSE;

        }

    }

}

/*数据库连接退出*/

void ADO::ExitConnect(void)

{

    if(m_pRecordset != NULL)

    {

        m_pRecordset->Close();

        m_pConnection->Close();

    }

}

/*!!!!!!!!!查询表数据代码!!!!!!!!!*/

_RecordsetPtr& ADO::GetRecordset( )

{

    DDlog Ddlog;

    if(m_pConnection == NULL)

    {

        AfxMessageBox("请先连接数据库!");

    }

    else

    {

        if(tname == "")

        {

            AfxMessageBox("表名不能为空!");

        }

        else

        {

            try

            {

                m_pRecordset.CreateInstance(__uuidof(Recordset));

                //获取表的记录集

                m_pRecordset->Open((char*)(_bstr_t) tname,

                    m_pConnection.GetInterfacePtr(),

                    adOpenDynamic, adLockOptimistic, adCmdTable);

            }

            catch(_com_error e)

            {

                AfxMessageBox("连接表错误!");

                m_pRecordset = NULL;

                return m_pRecordset;

            }

        }

    }

        return m_pRecordset;

}

/*创建表*/

_RecordsetPtr& ADO::CreRecordset( )

{

    _bstr_t cstr, dstr;

    _variant_t RecordsAffected;

    try

    {

        if(m_pConnection == NULL)

        {

            AfxMessageBox("请先连接数据库!");

        }

        dstr = "DROP TABLE ofsecurityauditlog";

        /*创建表字符串*/

        cstr = "CREATE TABLE ofsecurityauditlog(LogID VARCHAR(10), \

               Node VARCHAR(20), NodeID VARCHAR(20), ComputerName VARCHAR(50), \

               UserName VARCHAR(50), Department VARCHAR(50), Program VARCHAR(50), \

               Facility VARCHAR(50), ObjectName VARCHAR(50), Details VARCHAR(50),\

               Result VARCHAR(50), EntryStamp VARCHAR(50), Level VARCHAR(50), \

               Type VARCHAR(50), ProductType VARCHAR(50), BehaviourType VARCHAR(50),\

               Reservation VARCHAR(50))";

        m_pRecordset.CreateInstance(__uuidof(Recordset));

        try

        {

            //删除数据库中的表

            m_pConnection->Execute(dstr, &RecordsAffected, adCmdText);

            //创建表

            m_pConnection->Execute(cstr, &RecordsAffected, adCmdText);

        }

        catch(_com_error e)

        {

            m_pConnection->Execute(cstr, &RecordsAffected,adCmdText);

        }

        //获取创建表的记录集

        m_pRecordset->Open("ofsecurityauditlog", m_pConnection.GetInterfacePtr(),\

            adOpenDynamic, adLockOptimistic, adCmdTable);

        return m_pRecordset;

    }

    catch(_com_error e)

    {

        m_pRecordset = NULL;

        return m_pRecordset;

    }

    return m_pRecordset;

}

BEGIN_MESSAGE_MAP(ADO, CWnd)

END_MESSAGE_MAP()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: