首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++连接 mysql有关问题

2013-09-12 
c++连接 mysql问题// LandDlg.cpp : 实现文件//#include stdafx.h#include Land.h#include LandDlg.h

c++连接 mysql问题

// LandDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Land.h"
#include "LandDlg.h"
#include "afxdialogex.h"
//#include "winsock.h" 
//include "D:\Program Files\MySQL\MySQL Server 5.6\include\mysql.h" 
//#pragma comment(lib,"libmysql.lib")

// CLandDlg 对话框

IMPLEMENT_DYNAMIC(CLandDlg, CDialogEx)

CLandDlg::CLandDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CLandDlg::IDD, pParent)
{
m_pUserName = _T("");
m_pPass = _T("");
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
EnableAutomation();

}

CLandDlg::~CLandDlg()
{

}

void CLandDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CExamDlg)
DDX_Text(pDX, IDC_EDIT1, m_pUserName);
DDX_Text(pDX, IDC_EDIT2, m_pPass);
//}}AFX_DATA_MAP
}


void CLandDlg::OnFinalRelease()
{
// 释放了对自动化对象的最后一个引用后,将调用
// OnFinalRelease。基类将自动
// 删除该对象。在调用该基类之前,请添加您的
// 对象所需的附加清理代码。

CDialogEx::OnFinalRelease();
}


BEGIN_MESSAGE_MAP(CLandDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnBtnOk)
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CLandDlg, CDialogEx)
END_DISPATCH_MAP()

BOOL CLandDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon,TRUE);
SetIcon(m_hIcon,FALSE);

return TRUE;
}

void CLandDlg::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();
}

}
HCURSOR CLandDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CLandDlg::OnBtnOk()
{
UpdateData(TRUE);
int iRet = OnLogin(m_pUserName,m_pPass);
if(iRet == -1)
{
MessageBox("数据库并不存在");


}
else if(iRet == 0)
{
MessageBox("错误的用户名或密码");
}
else if(iRet == 1)
{
MessageBox("登陆成功");
}
else if(iRet > 1)
{
MessageBox("此用户已登录");
}
}
BOOL CLandDlg::AdoInit()
{
if(!AfxOleInit())
{
MessageBox("OLE初始化失败");
return FALSE;
}
memset(m_pConnectString,0x0,sizeof(m_pConnectString));
memset(m_pProgPath,0x0,sizeof(m_pProgPath));
GetCurrentDirectory(256,m_pProgPath);                           //获得程序路径
m_pProgPath[strlen(m_pProgPath)]='\\';
strcpy(m_pConnect,"odbc mysqlDRIVER={MySQL ODBC 5.2 ANSI driver};server=localhost;database=test;UID=root;password=326492;option=3306");
strcpy(m_pDBName,"Lander.ibd");
strcat(m_pConnectString,m_pConnect);
strcat(m_pConnectString,m_pProgPath);
strcat(m_pConnectString,m_pDBName);
return TRUE;
}

int CLandDlg::OnLogin(CString username,CString pass)
{/*
try 
{ MYSQL m_sqlCon;
mysql_init(&m_sqlCon);  
if(!mysql_real_connect(&m_sqlCon, "localhost","root","326492","test",3306,NULL,0)) 

AfxMessageBox(_T("数据库连接失败!"));  return FALSE; 

return TRUE;  
}  
catch (...) 

return FALSE;  
} */

if (FAILED(m_pRst.CreateInstance("ADODB.Recordset")))
{
MessageBox("Create Instance failed!");
return FALSE;
}

int i = 0;

CString Mysql = "select 1 from user_t where UserName='"+username+"' and Password='"+pass+"'";
if(FAILED(m_pRst->Open((_variant_t)Mysql,(_variant_t)m_pConnectString,adOpenStatic,adLockOptimistic,adCmdText)))
{
m_pRst.Release();
return -1;
}
while(!m_pRst->GetadoEOF())
{
i++;
m_pRst->MoveNext();

}
m_pRst.Release();
return i;
}


// 注意: 我们添加 IID_ILandDlg 支持
//  以支持来自 VBA 的类型安全绑定。此 IID 必须同附加到 .IDL 文件中的
//  调度接口的 GUID 匹配。

// {ECBF284B-2EBD-42FC-BCA8-1B5C3C54787B}
static const IID IID_ILandDlg =
{ 0xECBF284B, 0x2EBD, 0x42FC, { 0xBC, 0xA8, 0x1B, 0x5C, 0x3C, 0x54, 0x78, 0x7B } };

BEGIN_INTERFACE_MAP(CLandDlg, CDialogEx)
INTERFACE_PART(CLandDlg, IID_ILandDlg, Dispatch)


END_INTERFACE_MAP()


// CLandDlg 消息处理程序


问题:
数据库连接不成功;
随意输入点击登录有时提示:
Land.exe 中的 0x77b715de 处有未经处理的异常: Microsoft C++ 异常: 内存位置 0x0024df00 处的 _com_error。
不输入,直接点登录:直接提示上面信息.
求指导
[解决办法]

 int iRet = OnLogin(m_pUserName,m_pPass);
//这里判断下UserName 和Password的地址是否是有效的吧!
//这个内存出错的问题应该不难找的

[解决办法]
不输入  你的程序也没有做这方面的处理啊 

热点排行