📄 myadotestdlg.cpp
字号:
// myadotestDlg.cpp : implementation file
//
//++++相生昌作品+++//
//++Email:superxsc@126.com++//
#include "stdafx.h"
#include "myadotest.h"
#include "myadotestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyadotestDlg dialog
CMyadotestDlg::CMyadotestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyadotestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyadotestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyadotestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyadotestDlg)
DDX_Control(pDX, IDC_LIST4, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyadotestDlg, CDialog)
//{{AFX_MSG_MAP(CMyadotestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyadotestDlg message handlers
BOOL CMyadotestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_list.InsertColumn(0,"name",LVCFMT_LEFT,70);
m_list.InsertColumn(1,"age",LVCFMT_LEFT,50);
m_list.InsertColumn(2,"highth",LVCFMT_LEFT,50);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyadotestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMyadotestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyadotestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//得到应用程序的路径
CString GetAppPath()
{
char lpFileName[MAX_PATH];
GetModuleFileName(AfxGetInstanceHandle(),lpFileName,MAX_PATH);
CString strFileName = lpFileName;
int nIndex = strFileName.ReverseFind ('\\');
CString strPath;
if (nIndex > 0)
strPath = strFileName.Left (nIndex);
else
strPath = "";
return strPath;
}
//得到访问VF表的DSN字符串
CString GetVFFreeTableSRCString(CString strFolderName)
{
CString strSRC;
strSRC="DRIVER=Microsoft Visual FoxPro Driver;";
strSRC+="SourceType=DBF;";
strSRC+="SourceDB=";
strSRC+=strFolderName;
strSRC+="\\";
strSRC+=";UID=SA;PWD=";
return strSRC;
}
//创建表
void CMyadotestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
_ConnectionPtr pConn;
if(FAILED(pConn.CreateInstance("ADODB.Connection")))
{
AfxMessageBox("Create Instance Failed!");
return;
}
CString strSRC;
CString strSQL;
strSRC=GetAppPath();
strSRC=GetVFFreeTableSRCString(strSRC);
strSQL="create table mytable(name char(10),age int,highth float(4,2))";
_variant_t varSRC(strSRC);
_variant_t varSQL(strSQL);
_bstr_t bstrSRC(strSRC);
if(FAILED(pConn->Open(bstrSRC,"","",-1)))
{
AfxMessageBox("Can not open the databse!");
pConn->Release();
return;
}
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
pConn->Execute(_bstr_t(strSQL),&vtOptional,-1);
pConn.Release();
AfxMessageBox("Created the table successfully!");
}
//插入记录
void CMyadotestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
_ConnectionPtr pConn;
if(FAILED(pConn.CreateInstance("ADODB.Connection")))
{
AfxMessageBox("Create the Instance Failed!");
return;
}
CString strSRC;
CString strSQL;
strSRC=GetAppPath();
strSRC=GetVFFreeTableSRCString(strSRC);
CString myname="xsc";
int myage=26;
float myhighth=1.72;
strSQL.Format("insert into mytable values('%s',%d,%f)",myname,myage,myhighth);
//strSQL="insert into mytable(name,age,highth) values('xsc',24,1.72)";
_variant_t varSRC(strSRC);
_bstr_t bstrSRC(strSRC);
if(FAILED(pConn->Open(bstrSRC,"","",-1)))
{
AfxMessageBox("can not open the database!");
pConn.Release();
return;
}
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
try
{
pConn->Execute(_bstr_t(strSQL),&vtOptional,-1);
}
catch(...)
{
//pConn->Close();
//pConn->Release();
AfxMessageBox("inert the record error!");
return;
}
pConn->Close();
pConn.Release();
AfxMessageBox("Insert the record successfully!");
}
//查询
void CMyadotestDlg::OnButton3()
{
// TODO: Add your control notification handler code here
_ConnectionPtr pConn;
if(FAILED(pConn.CreateInstance("ADODB.Connection")))
{
AfxMessageBox("create the Instance failed!");
return;
}
CString strSRC;
CString strSQL;
strSRC=GetAppPath();
strSRC=GetVFFreeTableSRCString(strSRC);
strSQL="select * from mytable";
_variant_t varSRC(strSRC);
_variant_t varSQL(strSQL);
_bstr_t bstrSRC(strSRC);
if (FAILED(pConn->Open(bstrSRC,"","",-1)))
{
AfxMessageBox("Can not open Database!");
pConn.Release();
return;
}
_RecordsetPtr rs;
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
try
{
rs=pConn->Execute(_bstr_t(strSQL),&vtOptional,-1);
}
catch(...)
{
pConn->Close();
pConn.Release();
AfxMessageBox("Error!");
return;
}
CString m_strname;
CString m_strage;
CString m_strhighth;
while(!rs->GetadoEOF())
{
_variant_t varstrname=rs->GetCollect("name");
_variant_t varstrage=rs->GetCollect("age");
_variant_t varstrhighth=rs->GetCollect("highth");
m_strname=(char*)_bstr_t(varstrname);
m_strage=(char*)_bstr_t(varstrage);
m_strhighth=(char*)_bstr_t(varstrhighth);
int nCount = m_list.GetItemCount();
int nItem = m_list.InsertItem (nCount,_T(""));
m_list.SetItemText (nItem,0,m_strname);
m_list.SetItemText (nItem,1,m_strage);
m_list.SetItemText (nItem,2,m_strhighth);
rs->MoveNext();
}
rs->Close();
rs.Release();
pConn->Close();
pConn.Release();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -