1、在屬性添加:附加目錄,附加依賴庫,附加庫目錄;
2、在stdafx.h中加入:
#include <winsock.h>
#include "mysql.h"
3、拷貝libmysql.lib和libmysql.dll到工程解決方案
4、【X64】項目屬性->配置管理器->活動解決方案平臺-><新建>->鍵入或選擇新平臺:X64;
整個使用過程:
在類裏面:
private:
MYSQL *conn;
const char *Cerror;
然後在構造函數裏面:
CMmysql5Dlg::CMmysql5Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMmysql5Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//自己的內容
//初始化數據庫
conn = mysql_init(NULL);
if (conn == NULL)
{
Cerror = mysql_error(conn);
CString Error(Cerror);
AfxMessageBox(_T("數據庫初始化失敗!"), MB_OK | MB_ICONWARNING);
AfxMessageBox(Error, MB_OK | MB_ICONWARNING);
return ;
}
//連接數據庫
if ( mysql_real_connect(conn, "localhost", "root", "123456", NULL, 0, NULL, 0) == NULL)
{
Cerror = mysql_error(conn);
CString Error(Cerror);
AfxMessageBox(_T("數據庫連接失敗,請檢查是不是用戶名或密碼出錯!"), MB_OK | MB_ICONWARNING);
AfxMessageBox(Error, MB_OK | MB_ICONWARNING);
return ;
}
}
//在相應的需要操作的函數中:
void CMmysql5Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知處理程序代碼
CString str;
GetDlgItemText(IDC_EDIT,str);
const wchar_t* wstr = ( LPCTSTR )str;
char Xstr[200] = {0};
wcstombs( Xstr, wstr, wcslen( wstr ) );
int n = 20;
CSting sz;
sz.Format("%d", n);
AfxMessageBox(sz);
if ( mysql_query(conn, Xstr))
{
Cerror = mysql_error(conn);
CString Error(Cerror);
AfxMessageBox(_T("命令錯誤!"));
AfxMessageBox(Error, MB_OK | MB_ICONWARNING);
}else
MessageBox(str,_T("命令執行正確!"));
//OnOK();
}