以前实习期间写的一段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()
//
#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()
相关文章推荐
- 以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之三 导出数据库数据为xml格式自定义
- 以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之四 读取xml格式文档到界面
- 题 阅读 评论 评论权限 操作 以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之五 导入xml格式文档数据到数据库
- jdbc 连接数据库并进行操作相关代码
- Java 连接到 MySQL 数据库的代码及相关操作
- MFC:结合文档和视图使用数据库类和其他相关数据库编程
- ASP编程中连接数据库和数据库操作的常用代码
- ASP编程中连接数据库和数据库操作的常用代码
- asp连接access sql数据库代码及数据库操作
- android开发包相关操作编程代码
- 相关的连接与操纵数据库的编程方法之ADO连接access数据库
- 数据库操作:java连接MSSQL实例代码演示
- php连接数据库的相关操作
- JAVA中JDBC连接数据库的完整步骤及相关代码
- 数据库的相关操作:如连接、查询、添加、删除、修改、分页显示
- 用代码连接数据库 并进行操作
- java数据库编程--JDBC操作步骤以及数据库连接
- 数据库监控相关:连接方式、主要采集信息
- 数据库操作:java连接MSSQL实例代码演示
- asp连接access、sql数据库代码及数据库常用操作代码