⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ccdlg.cpp

📁 VC操作MYSQL的一些例子
💻 CPP
字号:
// ccDlg.cpp : implementation file
//

#include "stdafx.h"
#include "cc.h"
#include "ccDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CCcDlg dialog

CCcDlg::CCcDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCcDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCcDlg)
	m_1 = _T("");
	m_3 = _T("");
	m_4 = _T("");
	m_5 = _T("");
	m_6 = _T("");
	m_7 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCcDlg)
	DDX_Text(pDX, IDC_EDIT1, m_1);
	DDX_Text(pDX, IDC_EDIT3, m_3);
	DDX_Text(pDX, IDC_EDIT4, m_4);
	DDX_Text(pDX, IDC_EDIT5, m_5);
	DDX_Text(pDX, IDC_EDIT6, m_6);
	DDX_Text(pDX, IDC_EDIT7, m_7);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCcDlg, CDialog)
	//{{AFX_MSG_MAP(CCcDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCcDlg message handlers

BOOL CCcDlg::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
	
	// TODO: Add extra initialization here
	SetDlgItemText(IDC_EDIT3,"127.0.0.1");
	SetDlgItemText(IDC_EDIT5,"");
	SetDlgItemText(IDC_EDIT6,"mysql");
	SetDlgItemText(IDC_EDIT7,"3306");
	SetDlgItemText(IDC_STATIC_MSG,"未连接...");
	SetDlgItemText(IDC_EDIT1,"select * from user;");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCcDlg::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 CCcDlg::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 CCcDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCcDlg::OnOK() 
{
	// TODO: Add extra validation here
	//CDialog::OnOK();
}

void CCcDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}


void CCcDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CString button;
	GetDlgItemText(IDC_BUTTON1,button);
	if(button=="连接...")
	{
		UpdateData();
		if((myData=mysql_init((MYSQL*)0))&&
			mysql_real_connect( myData, m_3, m_4, m_5, m_6, atoi(m_7), NULL, 0 ) )
		{
			if ( mysql_select_db( myData, m_6 ) < 0 ) 
			{
				SetDlgItemText(IDC_STATIC_MSG,"已成功连接mysql,但是选择数据库出错!");
				mysql_close( myData ) ;
				return;
			}
			SetDlgItemText(IDC_STATIC_MSG,"已成功连接mysql....");
			GetDlgItem(IDC_EDIT3)->EnableWindow(false);
			GetDlgItem(IDC_EDIT4)->EnableWindow(false);
			GetDlgItem(IDC_EDIT5)->EnableWindow(false);
			GetDlgItem(IDC_EDIT6)->EnableWindow(false);
			GetDlgItem(IDC_EDIT7)->EnableWindow(false);
			GetDlgItem(IDC_BUTTON2)->EnableWindow();
			SetDlgItemText(IDC_BUTTON1,"断开连接");
		}
		else
		{
			SetDlgItemText(IDC_STATIC_MSG,"不能从端口:"+m_7+" 连接数据库,请从mysql管理员处获得详细信息!"
				);
			mysql_close( myData ) ;
			return;
		}
	}
	else
	{
		mysql_close( myData ) ;
			SetDlgItemText(IDC_STATIC_MSG,"已断开了与mysql的连接....");
		SetDlgItemText(IDC_BUTTON1,"连接...");
			GetDlgItem(IDC_EDIT3)->EnableWindow();
			GetDlgItem(IDC_EDIT4)->EnableWindow();
			GetDlgItem(IDC_EDIT5)->EnableWindow();
			GetDlgItem(IDC_EDIT6)->EnableWindow();
			GetDlgItem(IDC_EDIT7)->EnableWindow();
			GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
			return;
	}
}

void CCcDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if(m_1.Left(5)=="clear")
	{
		SetDlgItemText(IDC_EDIT2,"");
		m_1="";
		UpdateData(false);
		return;
	}
	mysql_close( myData ) ;
	SetDlgItemText(IDC_BUTTON1,"连接...");
	OnButton1();
	if(m_1=="")
		return;
	while(m_1.Left(1)==" ")
		m_1.TrimLeft(" ");

	int num;
	MYSQL_RES	* res ;
	MYSQL_FIELD	* fd ;
	MYSQL_ROW	row ;

	char * fdstr[1000];

	CString errormsg="",ondo=m_1.Left(6),temp;
	if(ondo=="select")
	{
		if ( !mysql_query(myData,m_1 ) ) 
		{
			res = mysql_store_result( myData ) ;
			num = (int) mysql_num_rows( res ) ;
			errormsg.Format("OK 找到%d条数据:%s",num,m_1);
			for (int x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
				fdstr[ x ]=fd->name;
			num=0;
			int j,k;
			while ( row = mysql_fetch_row( res ) )
			{
				++num;
				temp.Format("%s\r\n      --No%d--",errormsg,num);
				errormsg=temp;
				j = mysql_num_fields( res ) ;
				for ( k = 0 ; k < j ; k++ )
				{
					temp.Format("%s\r\n        [%s]: %s\n",errormsg, fdstr[ k ],(((row[k]==NULL)||(!strlen(row[k])))?"":row[k])) ;
					errormsg=temp;
				}
			}
			mysql_free_result( res ) ;
		}
		else
			errormsg.Format("NO 选择数据失败,因为语句格式有误:%s",m_1);
	}
	else if(ondo=="insert")
	{
		if ( !mysql_query(myData,m_1 ) ) 
		{
			errormsg.Format("OK 已经成功插入1条数据:%s",m_1);
		}
		else
			errormsg.Format("NO 插入数据失败,因为语句格式有误:%s",m_1);
	}
	else if(ondo=="update")
	{
		if ( !mysql_query(myData,m_1 ) ) 
		{
			errormsg.Format("OK 已经成功更新数据:%s",m_1);
		}
		else
			errormsg.Format("NO 更新数据失败,因为语句格式有误:%s",m_1);
	}
	else if(ondo=="delete")
	{
		if ( !mysql_query(myData,m_1 ) ) 
			errormsg.Format("OK 已经成功删除数据:%s",m_1);
		else
			errormsg.Format("NO 删除数据失败,因为语句格式有误:%s",m_1);
	}
	else
		errormsg.Format("NO 此语句格式有误:%s",m_1);
	GetDlgItemText(IDC_EDIT2,ondo);
	SetDlgItemText(IDC_EDIT2,errormsg+"\r\n"+ondo);
	m_1="";
	UpdateData(false);
	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -