// BarcodeDetection.cpp: 定义应用程序的类行为。
//
#include "pch.h"
#include "framework.h"
#include "BarcodeDetection.h"
#include "BarcodeDetectionDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CBarcodeDetectionApp
BEGIN_MESSAGE_MAP(CBarcodeDetectionApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
ON_COMMAND(ID_SQL_SET, &CBarcodeDetectionApp::OnSqlSet)
END_MESSAGE_MAP()
// CBarcodeDetectionApp 构造
CBarcodeDetectionApp::CBarcodeDetectionApp()
:m_nConnectionState(0)
,m_nSelDB(0)
,m_sDBName(_T("barcode"))
,m_sDBUser(_T("root"))
,m_sDBPass(_T("wzks"))
,m_nDBPort(3306)
{
// 支持重新启动管理器
m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
m_chIPAdd[0] = 127, m_chIPAdd[1] = 0, m_chIPAdd[2] = 0, m_chIPAdd[3] = 1;
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
}
// 唯一的 CBarcodeDetectionApp 对象
CBarcodeDetectionApp theApp;
// CBarcodeDetectionApp 初始化
BOOL CBarcodeDetectionApp::InitInstance()
{
// 如果一个运行在 Windows XP 上的应用程序清单指定要
// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
//则需要 InitCommonControlsEx()。 否则,将无法创建窗口。
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// 将它设置为包括所有要在应用程序中使用的
// 公共控件类。
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
AfxEnableControlContainer();
// 创建 shell 管理器,以防对话框包含
// 任何 shell 树视图控件或 shell 列表视图控件。
CShellManager *pShellManager = new CShellManager;
// 激活“Windows Native”视觉管理器,以便在 MFC 控件中启用主题
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
// 标准初始化
InitData();
if (InitDB())
{
ReLoadMode();
}
SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
CBarcodeDetectionDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: 在此放置处理何时用
// “确定”来关闭对话框的代码
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置处理何时用
// “取消”来关闭对话框的代码
}
else if (nResponse == -1)
{
TRACE(traceAppMsg, 0, "警告: 对话框创建失败,应用程序将意外终止。\n");
TRACE(traceAppMsg, 0, "警告: 如果您在对话框上使用 MFC 控件,则无法 #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS。\n");
}
// 删除上面创建的 shell 管理器。
if (pShellManager != nullptr)
{
delete pShellManager;
}
#if !defined(_AFXDLL) && !defined(_AFX_NO_MFC_CONTROLS_IN_DIALOGS)
ControlBarCleanUp();
#endif
// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
// 而不是启动应用程序的消息泵。
return FALSE;
}
bool CBarcodeDetectionApp::InitDB(void)
{
//0-正为连接正常 负数为连接异常
m_nConnectionState = -1;
HRESULT hr = ::CoInitialize(NULL);//初始化COM 环境
if (!SUCCEEDED(hr))
{
AfxMessageBox(_T("组件环境初始化失败!"));
return false;
}
try {
hr = m_pConnection.CreateInstance(__uuidof(Connection));//创建链接对象实例
if (!SUCCEEDED(hr))
{
AfxMessageBox(_T("数据库组件初始化失败!"));
return false;
}
}
catch (_com_error &e) {
#ifdef _DEBUG
AfxMessageBox(e.Description());
#else
AfxMessageBox(_T("数据库初始化失败,请重启,如多次失败,请重新安装操作系统!"));
#endif
return false;
}
//初始化配置文件
InitData();
if (theApp.m_oIpConfig.GetSize() != 0)
{
IpConfiguration* pIpConfig = new IpConfiguration();
theApp.m_pIpConfig = (theApp.m_nCurIP >= 0 && theApp.m_nCurIP < theApp.m_oIpConfig.GetSize()) ? theApp.m_oIpConfig[theApp.m_oIpConfig.GetSize() - 1] : NULL;
m_chIPAdd[0] = theApp.m_pIpConfig->m_chIPAdd[0];
m_chIPAdd[1] = theApp.m_pIpConfig->m_chIPAdd[1];
m_chIPAdd[2] = theApp.m_pIpConfig->m_chIPAdd[2];
m_chIPAdd[3] = theApp.m_pIpConfig->m_chIPAdd[3];
m_nSelDB = theApp.m_pIpConfig->m_nSelDB;
}
//连接数据库
_bstr_t sConn = GetConnStr();//获取数据库连接字符串
//打开数据库
try {
hr = m_pConnection->Open(sConn, "", "", adConnectUnspecified);
if (S_OK != hr)
{
AfxMessageBox(_T("字符串数据错误,数据库连接失败!"));
return false;
}
}
catch (_com_error &e)
{
#ifdef _DEBUG
AfxMessageBox(e.Description());
#else
AfxMessageBox(_T("数据库连接失败,请重新设定系统参数!"));
#endif
OnSqlSet();//菜单设置
if (-1 == m_nConnectionState)
{
return false;
}
}
m_nConnectionState = 0;
return true;
}
CString CBarcodeDetectionApp::GetConnStr(int nType)
{
CString sIPAdd;
CString sPort;
CString sRn;
switch (nType)
{
case 0:
if (0 == m_nSelDB)//选择为本地数据库
{
sIPAdd = _T("127.0.0.1");
}
else//网络数据库
{
sIPAdd.Format(_T("%d.%d.%d.%d"), m_chIPAdd[0], m_chIPAdd[1], m_chIPAdd[2], m_chIPAdd[3]);
}
sPort.Format(_T("%d"), m_nDBPort);
//AfxMessageBox(sIPAdd);
sRn = _T("Driver=MySQL ODBC 5.2 Unicode Driver;SERVER=") + sIPAdd + _T(";UID=") + m_sDBUser + _T(";PWD=") + m_sDBPass
+ _T(";DATABASE=") + m_sDBName + _T(";PORT=") + sPort;
break;
default:
break;
}
return sRn;
}
bool CBarcodeDetectionApp::ConnectDB(void)
{
try
{
if (0 == m_nConnectionState)//连接正常断开连接
{
m_pConnection->Close();//断开连接
m_nConnectionState = -1;
}
_bstr_t sConn = GetConnStr();//重连
HRESULT hr = m_pConnection->Open(sConn, "", "", adConnectUnspecified);
if (S_OK != hr)
{
AfxMessageBox(_T("数据库连接失败!"));
return false;
}
m_nConnectionState = 0;
}
catch (_com_error &e)
{
#ifdef _DEBUG
AfxMessageBox(e.Description());
#else
AfxMessageBox(_T("数据库连接失败,请重新设定系统参数!"));
#endif
return false;
}
ReLoadMode();//读取数据库
return true;
}
bool CBarcodeDetectionApp::ReLoadMode(void)
{
if (ExistTable(_T("barcodeTable")))
{
CreateTable();
}
//读取数据
CString sSQL("SELECT* FROM barcodeTable");
_RecordsetPtr pRecordSet;// COM 接口的指针用于管理与 ADODB.Recordset 相关的 COM 接口指针。
HRESULT hr = pRecordSet.CreateInstance("ADODB.Recordset");//.Recordset 属性或方法,可以访问记录集对象并进行相关的操作,
if (SUCCEEDED(hr))
{
//COleDateTime oleTm;
pRecordSet = m_pConnection->Execute((_bstr_t)sSQL, NULL, adCmdText);
long nCount;
pRecordSet->get_RecordCount(&nCount);
if (pRecordSet->_ADOEOF && pRecordSet->_ADOBOF)
{
return true;
}
}
else
{
AfxMessageBox(_T("读取失败!"));
return false;
}
return true;
}
bool CBarcodeDetectionApp::ExistTable(CString sTbName)
{
CString sSQL;
bool bRn = true;
try
{
sSQL = _T("SELECT * FROM ") + sTbName;
m_pConnection->Execute((_bstr_t)sSQL, NULL, adCmdText);
}
catch (_com_error e)
{
HRESULT hrErr = e.Error();
long nErr = hrErr;
switch (hrErr)
{
case -858993460: //表不存在
return bRn = false;
break;
}
}
return bRn;
}
int CBarcodeDetectionApp::CreateTable()
{
if (0 == m_nConnectionState)//正常状态
{
_CommandPtr pCommad;//_CommandPtr 为智能指针管理COM 接口
HRESULT hr = pCommad.CreateInstance("ADODB.Command");//实例化ADO数据库
pCommad->ActiveConnection = m_pConnection; //活动连接
pCommad->CommandType = adCmdText; //命令类型 为文本命令
CString sSQL;
try
{
CString sSQL = CString(_T("CREATE TABLE IF NOT EXISTS bar
没有合适的资源?快使用搜索试试~ 我知道了~
mfc 连接ODBC数据库以及配置ip文件操作
共78个文件
tlog:19个
obj:10个
h:8个
2 下载量 7 浏览量
2023-09-11
11:04:37
上传
评论
收藏 119.44MB ZIP 举报
温馨提示
mfc 连接ODBC数据库以及配置ip文件操作
资源推荐
资源详情
资源评论
收起资源包目录
BarcodeDetection.zip (78个子文件)
BarcodeDetection
CMySQL.h 832B
BarcodeDetectionDlg.h 627B
BarcodeDetection.rc 12KB
BarcodeDetection.cpp 12KB
resource.h 1016B
framework.h 2KB
BarcodeDetection.h 2KB
res
BarcodeDetection.ico 66KB
BarcodeDetection.rc2 688B
Release
vc141.pdb 7.01MB
BarcodeDetection.obj 104KB
BarcodeDetectionDlg.obj 60KB
BarcodeDetection.res 68KB
BarcodeDetection.tlog
CL.write.1.tlog 3KB
rc.command.1.tlog 500B
BarcodeDetection.lastbuildstate 208B
rc.write.1.tlog 298B
rc.read.1.tlog 4KB
CL.command.1.tlog 4KB
link.command.1.tlog 2KB
BarcodeDetection.write.1u.tlog 798B
link.read.1.tlog 8KB
link.write.1.tlog 1KB
CL.read.1.tlog 112KB
msado15.tli 145KB
pch.obj 6.49MB
BarcodeDetection.pch 53.44MB
BarcodeDetection.log 1012B
CMySQL.obj 70KB
IpConfiguration.obj 65KB
msado15.tlh 167KB
pch.h 573B
BarcodeDetection.vcxproj.user 239B
CMySQL.cpp 2KB
BarcodeDetection.vcxproj.filters 3KB
BarcodeDetectionDlg.cpp 2KB
IpConfiguration.h 369B
pch.cpp 158B
BarcodeDetection.aps 107KB
Debug
vc141.pdb 7.02MB
vc141.idb 1.35MB
BarcodeDetection.obj 247KB
BarcodeDetection.Build.CppClean.log 2KB
BarcodeDetectionDlg.obj 34KB
BarcodeDetection.res 68KB
BarcodeDetection.tlog
CL.write.1.tlog 5KB
rc.command.1.tlog 492B
BarcodeDetection.lastbuildstate 206B
rc.write.1.tlog 294B
rc.read.1.tlog 4KB
CL.command.1.tlog 4KB
link.command.1.tlog 2KB
link.read.1.tlog 8KB
link.write.1.tlog 1KB
CL.read.1.tlog 109KB
msado15.tli 145KB
pch.obj 1011KB
BarcodeDetection.pch 57.81MB
BarcodeDetection.log 438B
CMySQL.obj 48KB
IpConfiguration.obj 84KB
msado15.tlh 167KB
targetver.h 299B
BarcodeDetection.vcxproj 11KB
IpConfiguration.cpp 858B
.vs
BarcodeDetection
v15
Browse.VC.db 74.08MB
.suo 42KB
ipch
34a6934cedfd69c3.ipch 123.19MB
57e6baa308f465a3.ipch 117MB
Release
DATA
BarcodeDetection.exe 105KB
Log
BarcodeDetection.ipdb 205KB
BarcodeDetection.iobj 1.2MB
BarcodeDetection.pdb 6.9MB
sys
Debug
DATA
BarcodeDetection.exe 277KB
BarcodeDetection.ilk 1.73MB
Log
BarcodeDetection.pdb 2.75MB
sys
ip.ksmode 2KB
BarcodeDetection.sln 1KB
共 78 条
- 1
资源评论
吃个糖糖
- 粉丝: 394
- 资源: 28
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- onenet_chongwukanhu_v06.apk
- 2022NOC软件创意编程赛项真题C++初中-决赛(有解析)
- 2022NOC软件创意编程赛项真题python初中-决赛(有解析)
- openLayer-本地数据加载 (day5)
- A题-正弦信号发生器.xdf
- 2022NOC软件创意编程赛项真题python小学高年级-决赛(有解析)
- mathml转换latex需要的xsl文件
- 2022NOC软件创意编程赛项真题图形化小学高年级-决赛(有解析)
- gbase驱动下载gbase-connector-java-8.3.81.53驱动下载
- 2022NOC软件创意编程赛项真题图形化小学低年级-决赛(有解析)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功