读书人

MFC对话框数据库ADO失误 求高手帮助

发布时间: 2012-09-16 17:33:17 作者: rapoo

MFC对话框数据库ADO出错 求高手帮助
新手学数据库方面的东西,我照书上写的代码:


[code=C/C++][/code]#endif // _AFX_NO_AFXCMN_SUPPORT
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namsepace\
rename("EOF","adoEOF")rename("BOF","adoBOF")//StdAfx.h头文件里 并且msado15.dll路径正确

::CoInitialize(NULL); //初始化COM坏境 InitInstance()函数里



// CADO_Dlg dialog 对话框类头文件里
_CollectionPtr m_pConnection; //连接对象指针
_RecordsetPtr m_pRecordset; //记录集对象指针



//下面是初始化时候进行数据库连接
BOOL CADO_Dlg::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

m_List.SetExtendedStyle(LVS_EX_FLATSB//扁平显示滚动显示条
|LVS_EX_FULLROWSELECT//允许整行选中
|LVS_EX_HEADERDRAGDROP//允许整行拖动
|LVS_EX_ONECLICKACTIVATE//单击选中
|LVS_EX_GRIDLINES);//画出网络格);
//设置列标题及其宽度
m_List.InsertColumn(0,"商品名称",LVCFMT_LEFT,110,0);
m_List.InsertColumn(1,"销售数量",LVCFMT_LEFT,110,1);

try
{
m_pConnection.CreateInstance("ADODB.Connection");//创建连接对象实例
_bstr_t strConnect="DRIVER={Microsoft Access Driver(*.mdb)};\
uid=;pwd=;DBQ=Database.mdb;";
m_pConnection->Open(strConnect,"","",adModeUnknown);//打开数据库
}
catch(_com_error e)
{
AfxMessageBox(e.Description());//弹出对话框
}
CString sql="select * from Selllnfo";
m_pRecordset.CreateInstance(_uuidof(Recordset));
m_pRecordset->Open(_bstr_t(sql),m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText); //执行SQL得到记录集
while(!m_pRecordset->adoEOF)
{
m_List.InsertItem(0,"");//向列表视图中插入行
m_List.SetItemText(0,0,(char *)(_bstr_t)m_pRecordset->GetCollect("Merchandise"));
m_List.SetItemText(0,0,(char *)(_bstr_t)m_pRecordset->GetCollect("Sum"));
m_pRecordset->MoveNext();

}
m_pRecordset->Close();

return TRUE; // return TRUE unless you set the focus to a control
}


然后编译出现错误:
e:\ado_\ado_dlg.h(15) : error C2146: syntax error : missing ';' before identifier 'm_pConnection'
e:\ado_\ado_dlg.h(15) : error C2501: '_CollectionPtr' : missing storage-class or type specifiers
e:\ado_\ado_dlg.h(15) : fatal error C1004: unexpected end of file found
Generating Code...
执行 cl.exe 时出错.

ADO_.exe - 1 error(s), 0 warning(s)

差了一些资料【2天了还是不行】,建议Rebuilt All试了不行,各种解决方法都么用了,不知道是哪里出错了,
希望高手看看,给些详细的解决方案

[解决办法]
这代码帖地,谁知道哪行啊,也没提示下大家
[解决办法]
_CollectionPtr 是拼错了吧, _ConnectionPtr
[解决办法]

C/C++ code
#endif // _AFX_NO_AFXCMN_SUPPORT                  // 这里打错了(no_namespace)#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namsepace\ rename("EOF","adoEOF")rename("BOF","adoBOF")//StdAfx.h头文件里 并且msado15.dll路径正确 


[解决办法]
fatal error C1004: unexpected end of file found

头文件类定义完,少了个;

这个错误是少了个括号或;之类的
[解决办法]

探讨
问题解决了,是定义的位置不对+头文件缺少造成的
但是执行弹出
:abnormal program termination错误


狂汗

读书人网 >VC/MFC

热点推荐