2 woxieele woxieele 于 2014.05.08 19:09 提问

MFC 运行程序遇到abnormal program termination,可能连接数据库有问题

C++6.0用ADO连接SQL,编译没问题,运行有错误,现在实现登录然后弹出主窗口
ADo代码

// ADO.cpp: implementation of the CADO class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ADO.h"
#include

#ifdef DEBUG
#undef THIS_FILE
static char THIS_FILE[]=
_FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int ConCount = 0;
CADOConnection g_Connection;//全局数据库连接对象

CADOConnection * GetConnection()
{
return &g_Connection;
}

CADOConnection::CADOConnection()
{
InitADO();
m_Connection.CreateInstance("ADODB.Connection");
}

CADOConnection::~CADOConnection()
{
if (IsOpen())
m_Connection->Close();
m_Connection = NULL;
UnInitADO();
}

void CADOConnection::InitADO()
{
if (ConCount++ == 0)
CoInitialize(NULL);
};
void CADOConnection::UnInitADO()
{
if (--ConCount == 0)
CoUninitialize();
};

BOOL CADOConnection::Open(CString ConStr)
{
if (IsOpen())
m_Connection->Close();
m_Connection->Open((_bstr_t)ConStr,"","",adModeUnknown);
return IsOpen();
}

CString CADOConnection::GetSQLConStr(CString IP, CString DBName)
{
CString Str;
Str.Format("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=%s;Data Source=%s",DBName,IP);
return Str;
}

_ConnectionPtr CADOConnection::GetConnection()
{
return m_Connection;
}

BOOL CADOConnection::IsOpen()
{
long State;
m_Connection->get_State(&State);
if (State == adStateOpen)
return true;
return false;
}

////////////////////////////////////////
CADODataSet::CADODataSet()
{
m_DataSet.CreateInstance("ADODB.Recordset");
}

CADODataSet::~CADODataSet()
{
if (IsOpen())
m_DataSet->Close();
m_DataSet = NULL;
m_Connection = NULL;
}

void CADODataSet::SetConnection(CADOConnection *pCon)
{
m_Connection = pCon;
}

int CADODataSet::GetRecordCount()
{
if (IsOpen())
return m_DataSet->GetRecordCount();
else
return 0;
}

BOOL CADODataSet::Open(CString SQLStr)
{
if (IsOpen())
m_DataSet->Close();
//*/
m_DataSet->Open(_bstr_t(SQLStr),
_variant_t((IDispatch*)g_Connection.GetConnection(), true),
adOpenKeyset, adLockOptimistic, adCmdText);
return IsOpen();
//*/

}

BOOL CADODataSet::IsOpen()
{
long State;
m_DataSet->get_State(&State);
if (State == adStateOpen)
return true;

return false;

}

FieldsPtr CADODataSet::GetFields()
{
return m_DataSet->GetFields();
}

BOOL CADODataSet::Next()
{
if (m_DataSet->adoEOF)
return false;
m_DataSet->MoveNext();
return true;
}

void CADODataSet::AddNew()
{
m_DataSet->AddNew();
}

void CADODataSet::SetFieldValue(CString FieldName, _variant_t Value)
{
m_DataSet->PutCollect((_bstr_t)FieldName,Value);
}

void CADODataSet::Save()
{
m_DataSet->Update();
}

void CADODataSet::move(int nIndex)
{
m_DataSet->MoveFirst();
m_DataSet->Move(nIndex);
}

int CADODataSet::GetRecordNo()
{
return m_DataSet->AbsolutePosition;
}

void CADODataSet::Delete()
{
m_DataSet->Delete(adAffectCurrent);
}

BOOL CADODataSet::Open(CString SQLStr, int LockType)
{
if (IsOpen())
m_DataSet->Close();
//*/
m_DataSet->Open(_bstr_t(SQLStr),
_variant_t((IDispatch*)g_Connection.GetConnection(), true),
adOpenKeyset,(LockTypeEnum) LockType, adCmdText);
return IsOpen();
}

Award.cpp

// LoginDialog.cpp : implementation file
//

#include "stdafx.h"
#include "Award.h"
#include "ADO.h"
#include "LoginDialog.h"

#ifdef DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE
_;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLoginDialog dialog

CLoginDialog::CLoginDialog(CWnd* pParent /*=NULL*/)
: CDialog(CLoginDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CLoginDialog)
//}}AFX_DATA_INIT
}

void CLoginDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLoginDialog)
DDX_Control(pDX, IDC_EDIT1, m_password);
DDX_Control(pDX, IDC_COMUSERLIST, m_userlist);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLoginDialog, CDialog)
//{{AFX_MSG_MAP(CLoginDialog)
ON_BN_CLICKED(IDOK, OnLogin)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLoginDialog message handlers
void CLoginDialog::OnLogin()
{
CString sql,user,pass;
m_userlist.GetWindowText(user);
m_password.GetWindowText(pass);

sql.Format("Select * From User Where userName = '%s' and passWord = '%s'",
    user,pass);
m_DataSet.Open(sql);
if (m_DataSet.GetRecordCount() == 1)
{
    ::SetUserName(user);
    this->OnOK();
}
else
    AfxMessageBox("用户名或密码不正确!");

}
BOOL CLoginDialog::OnInitDialog()
{
CDialog::OnInitDialog();
m_DataSet.SetConnection(GetConnection());
m_DataSet.Open("Select * From User");
int count = m_DataSet.GetRecordCount();
for (int i = 0; i< count;i++)
{
m_userlist.AddString((_bstr_t)m_DataSet.GetFields()->Item[L"UserName"]->Value);
m_DataSet.Next();
}
m_userlist.SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!