以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之一连接数据库
来源:互联网 发布:域名注册身份证怎么弄 编辑:程序博客网 时间:2024/05/13 01:49
// 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格式文档的相关操作 主要相关代码 之一连接数据库
- 以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之四 读取xml格式文档到界面
- 以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之二 读取数据数据到界面
- 题阅读评论评论权限操作 以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之五 导入xml格式文档数据到数据库
- 以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之三 导出数据库数据为xml格式自定义
- Java 连接到 MySQL 数据库的代码及相关操作
- Java连接到MySQL数据库的代码及相关操作
- MFC 对MYSQL数据库相关操作
- MFC对MYSQL数据库相关操作
- C#连接数据库的相关的代码
- php连接数据库的相关操作
- 数据库的相关操作
- 数据库的相关操作
- 数据库相关的操作
- 数据库的相关操作
- 数据库的相关操作
- 数据库的相关操作
- python操作数据库相关的代码
- ADC 参考源
- 内核对象
- 第十一周任务2:有趣的数学
- 面试题目写一个字符串逆序函数strrev()
- 制作根文件系统
- 以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之一连接数据库
- MSSQL-基础实用语句集
- xp windows7 与windows8
- Spring接口动态注入不同的实现类
- 第十一周任务3:大奖赛计分
- 以前实习期间写的一段mfc编程,对数据库和xml格式文档的相关操作 主要相关代码 之二 读取数据数据到界面
- 输出10000以内的所有可逆素数
- HDU 1255 覆盖的面积
- 动态分配内存空间