求大神帮忙看一下程序
初学数据库,东拼西凑了这么个程序,用的是vs2010
按条件查找,现在正在编按名称查找,结果总是运行过程中有错退出,
调试时发现是在m_pRecordset->Open处出错,
是这个open的参数写的有错吗?
麻烦大神帮忙改一下,感激不尽!!!!
// DemoDlg.cpp : implementation file
// Download by http://www.NewXing.com
#include "stdafx.h"
#include "Demo.h"
#include "DemoDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog
CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDemoDlg)
/*
*/
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
if (!SUCCEEDED(m_pConnection.CreateInstance(__uuidof(Connection))))
{
m_pConnection = NULL;
TRACE(_T("Database CreateInstance failed"));
}
if (!SUCCEEDED(m_pRecordset.CreateInstance(__uuidof(Recordset))))
{
m_pRecordset = NULL;
TRACE(_T("Recordset CreateInstance Failed!"));
}
//打开数据库
CString strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= DataBase.mdb"); //
if (!OpenDatabase(strConnect))
{
AfxMessageBox(_T("数据库打开失败。"));
return;
}
//打开记录集
if (!OpenRecordset(_T("SELECT * FROM file_info"))) //
{
AfxMessageBox(_T("记录集打开失败。"));
return;
}
}
CDemoDlg::~CDemoDlg()
{
m_pRecordset->Close();
m_pConnection->Close();
}
void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDemoDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
//{{AFX_MSG_MAP(CDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_FIND, OnFind)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers
BOOL CDemoDlg::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
//初始化ListCtrl /////
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
pListCtrl->SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
pListCtrl->InsertColumn(1, _T("序号"), LVCFMT_CENTER, 60);
pListCtrl->InsertColumn(2, _T("文件名"), LVCFMT_CENTER, 60);
pListCtrl->InsertColumn(3, _T("类型"), LVCFMT_CENTER, 40);
pListCtrl->InsertColumn(4, _T("时间"), LVCFMT_CENTER, 80);
pListCtrl->InsertColumn(5, _T("保存路径"), LVCFMT_CENTER, 200);
return TRUE;
}
void CDemoDlg::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 CDemoDlg::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 CDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CDemoDlg::OpenDatabase(LPCTSTR lpszConnect, long nOptions)
{
ASSERT(m_pConnection != NULL);
ASSERT(lpszConnect != NULL);
ASSERT(AfxIsValidString(lpszConnect));
//打开数据库连接
try
{
return SUCCEEDED(m_pConnection->Open(_bstr_t(lpszConnect),
_T(""), _T(""), nOptions)); ////////////////
}
catch (_com_error& e)
{
TRACE(_T("%s\n"), e.ErrorMessage());
return FALSE;
}
}
BOOL CDemoDlg::CloseDatabase()
{
ASSERT(m_pConnection != NULL);
//关闭数据库连接
try
{
if (m_pConnection->State & adStateOpen)
{
return SUCCEEDED(m_pConnection->Close());
}
else
{
return TRUE;
}
}
catch (_com_error& e)
{
TRACE(_T("%s\n"), e.ErrorMessage());
return FALSE;
}
}
BOOL CDemoDlg::OpenRecordset(LPCTSTR lpszSource, long nCursorType, long nLockType, long nOptions)
{
ASSERT(m_pConnection != NULL);
ASSERT(m_pRecordset != NULL);
ASSERT(lpszSource != NULL);
ASSERT(AfxIsValidString(lpszSource));
//打开记录集
try
{
return (SUCCEEDED(m_pRecordset->Open(_variant_t(lpszSource),
m_pConnection.GetInterfacePtr(),
(CursorTypeEnum)nCursorType,
(LockTypeEnum)nLockType,
nOptions)));
}
catch(_com_error e)
{
TRACE(_T("%s\n"), e.ErrorMessage());
return FALSE;
}
}
BOOL CDemoDlg::CloseRecorset()
{
ASSERT(m_pRecordset != NULL);
//关闭记录集
try
{
if (m_pRecordset->State & adStateOpen)
{
return SUCCEEDED(m_pRecordset->Close());
}
else
{
return TRUE;
}
}
catch (_com_error e)
{
TRACE(_T("%s\n"), e.ErrorMessage());
return FALSE;
}
}
void CDemoDlg::OnFind()
{
if (!(m_pRecordset->State & adStateOpen))
{
AfxMessageBox((_T("记录集未打开。")));
return;
}
//查找条件
CString strCriteria = _T("");
CString strName = _T("");
CString strtype = _T("");
CString strtime = _T("");
GetDlgItemText(IDC_NAME, strName);
GetDlgItemText(IDC_TYPE, strtype);
GetDlgItemText(IDC_TIME, strtime);
if(strName != _T(""))
strCriteria.Format(_T("NAME like '*%s*'"),strName);
else return;
//查找记录集
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
pListCtrl->DeleteAllItems();
if (m_pRecordset->BOF && m_pRecordset->adoEOF)
{
return;
}
m_pRecordset->MoveFirst();
///////////
m_pRecordset->Open(_bstr_t(strCriteria),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockBatchOptimistic,adCmdText);
//////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int n = 0;
while (!m_pRecordset->adoEOF)
{
_variant_t varValue;
CString strName = _T("");
CString strtime = _T("");
CString strtype = _T("");
CString strpath = _T("");
varValue = m_pRecordset->GetCollect(_variant_t(_T("NAME")));
if (varValue.vt != VT_NULL)
{
strName = varValue.bstrVal;
}
else
{
strName = _T("");
}
varValue = m_pRecordset->GetCollect(_variant_t(_T("TYPE")));
if (varValue.vt != VT_NULL)
{
strtype = varValue.bstrVal;
}
else
{
strtype = _T("");
}
varValue = m_pRecordset->GetCollect(_variant_t(_T("TIME")));
if (varValue.vt != VT_NULL)
{
strtime = varValue.bstrVal;
}
else
{
strtime = _T("");
}
varValue = m_pRecordset->GetCollect(_variant_t(_T("PATH")));
if (varValue.vt != VT_NULL)
{
strpath = varValue.bstrVal;
}
else
{
strpath = _T("");
}
//刷新ListCtrl
CString strText = _T("");
strText.Format(_T("%d"), n + 1);
pListCtrl->InsertItem(n, strText);
strText.Format(_T("%s"), strName);
pListCtrl->SetItemText(n, 1, strText);
strText.Format(_T("%s"), strtype);
pListCtrl->SetItemText(n, 2, strText);
strText.Format(_T("%s"),strtime);
pListCtrl->SetItemText(n, 3, strText);
strText.Format(_T("%s"), strpath);
pListCtrl->SetItemText(n, 4, strText);
n++;
////////////////////////////
m_pRecordset->MoveNext();
////////////////////////////
}
if(n==0)
{
pListCtrl->InsertItem(n,"NULL!");
}
}