📄 dbconfig.cpp
字号:
// DbConfig.cpp : implementation file
//
#include "stdafx.h"
#include "stocksystem.h"
#include "DbConfig.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDbConfig dialog
CDbConfig::CDbConfig(CWnd* pParent /*=NULL*/)
: CDialog(CDbConfig::IDD, pParent)
{
//{{AFX_DATA_INIT(CDbConfig)
m_dbname = _T("");
m_username = _T("");
m_password = _T("");
//}}AFX_DATA_INIT
}
void CDbConfig::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDbConfig)
DDX_Text(pDX, IDC_DB_NAME, m_dbname);
DDX_Text(pDX, IDC_USERNAME, m_username);
DDX_Text(pDX, IDC_PASSWORD, m_password);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDbConfig, CDialog)
//{{AFX_MSG_MAP(CDbConfig)
ON_BN_CLICKED(IDC_TEST_CONNECT, OnTestConnect)
ON_BN_CLICKED(IDC_OK_BTN, OnOkBtn)
ON_BN_CLICKED(IDC_CANCLE_BTN, OnCancleBtn)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDbConfig message handlers
void CDbConfig::OnOK()
{
// TODO: Add extra validation here
// CDialog::OnOK();
}
void CDbConfig::OnCancel()
{
// TODO: Add extra validation here
// CDialog::OnCancle();
}
//测试连接
void CDbConfig::OnTestConnect()
{
UpdateData();
CAdoConnect connect;
connectstr.Format("Provider=OraOLEDB.Oracle.1;Password=%s;Persist Security Info=True;User ID=%s;Data Source=%s",m_password,m_username,m_dbname);
connect.InitInstance();
if (!connect.ConnectDb(connectstr))
{
MessageBox("测试连接失败","提示");
m_dbname.Empty();
m_password.Empty();
m_username.Empty();
connect.m_connect.Release();
UpdateData(FALSE);
return;
}
else
{
MessageBox("测试连接成功","提示");
connect.m_connect.Release();
}
// m_dbname.Empty();
// m_password.Empty();
// m_username.Empty();
// connect.m_connect->Close();
return;
}
//确定按钮,保存配置
void CDbConfig::OnOkBtn()
{
UpdateData();
if (m_dbname.GetLength() == 0)
{
MessageBox("数据库名不能为空","错误");
m_dbname.Empty();
m_password.Empty();
m_username.Empty();
UpdateData(FALSE);
return;
}
else if (m_username.GetLength() == 0)
{
MessageBox("用户名不能为空","错误");
m_dbname.Empty();
m_password.Empty();
m_username.Empty();
UpdateData(FALSE);
return;
}
else if (m_password.GetLength() == 0)
{
MessageBox("密码不能为空","错误");
m_dbname.Empty();
m_password.Empty();
m_username.Empty();
UpdateData(FALSE);
return;
}
//写入配置文件
ReadInConfig();
//onnectstr.Format("Provider=OraOLEDB.Oracle.1;Password=%s;Persist Security Info=True;User ID=%s;Data Source=%s",m_password,m_username,m_dbname);
//发送消息给主框架,把connectstr发给它
// CMainFrame *mainframe = (CMainFrame *)GetParent();
// mainframe->SendMessage(WM_UPDATECONNECTSTR, (WPARAM)&connectstr, 0);
CDialog::OnOK();
}
void CDbConfig::OnCancleBtn()
{
// CDialog::OnCancel();
CDialog::OnCancel();
CDialog::OnClose();
}
void CDbConfig::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnClose();
}
void CDbConfig::ReadInConfig()
{
CString buf;
buf.Format("#数据库名\r\n"
"dbname=%s\r\n"
"#用户名\r\n"
"user=%s\r\n"
"#密码\r\n"
"password=%s\r\n",
m_dbname,m_username,m_password);
CFile file("dbconf.conf",CFile::modeCreate | CFile::modeWrite);
file.Write(buf, strlen(buf));
file.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -