请教MFC连接数据库问题关于连接数据库中不同表的
现在我出现的问题是,我找了个例子连接ACCESS的2张表,现在我把原来例子的数据库换成另外我新建的数据库里的2张表,结果是第一张表能成功运行,进行操作。第2张表却不能进行操作了。是怎么回事?
我做了个测试,把第二张表的数据另外建成一个数据库,进行连接。第二个数据库也能连接。这是什么原因啊?
现在我把原来没动的代码和我改动后的代码贴出来,大神求解决
程序代码:
BOOL CShebei::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here // 创建列表控件的标题头 CString strHeader[6]={ "更换日期", "更换原因", "上线车号", "下线车号", "炉体编号", "更换类别"}; for (int nCol=0; nCol<6; nCol++) m_ListCtrl.InsertColumn(nCol,strHeader[nCol],LVCFMT_LEFT,85); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CShebei::OnButton1() { // TODO: Add your control notification handler code here ShebeiConn(); DispShebei(); } CShebei::DispShebei() { m_ListCtrl.DeleteAllItems(); int nItem = 0; while (!m_pRs1->adoEOF) { vFieldValue1=m_pRs1->GetCollect("更换日期"); m_ListCtrl.InsertItem( nItem, (char*)_bstr_t(vFieldValue1) ); vFieldValue1.Clear(); vFieldValue1=m_pRs1->GetCollect("更换原因"); m_ListCtrl.SetItemText( nItem, 1, (char*)_bstr_t(vFieldValue1) ); vFieldValue1.Clear(); vFieldValue1=m_pRs1->GetCollect("上线车号"); m_ListCtrl.SetItemText( nItem, 2, (char*)_bstr_t(vFieldValue1) ); vFieldValue1.Clear(); vFieldValue1=m_pRs1->GetCollect("下线车号"); m_ListCtrl.SetItemText( nItem, 3, (char*)_bstr_t(vFieldValue1) ); vFieldValue1.Clear(); vFieldValue1=m_pRs1->GetCollect("炉体编号"); m_ListCtrl.SetItemText( nItem, 4, (char*)_bstr_t(vFieldValue1) ); vFieldValue1.Clear(); vFieldValue1=m_pRs1->GetCollect("更换类别"); m_ListCtrl.SetItemText( nItem, 5, (char*)_bstr_t(vFieldValue1) ); vFieldValue1.Clear(); m_pRs1->MoveNext(); nItem++; } } CShebei::ShebeiConn() { m_pCon1.CreateInstance(__uuidof(Connection)); m_pCon1->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=lunwen.mdb;Persist Security Info=False" ,"","",-1); m_pRs1.CreateInstance(__uuidof(Recordset)); m_pRs1->Open("select * from 钢包车渣车换车记录", m_pCon1.GetInterfacePtr(), adOpenDynamic,adLockOptimistic,adCmdText); m_pRs1->MoveFirst(); } void CShebei::OnButton2() { // TODO: Add your control notification handler code here CAddShebei dlgaddshebei; if (IDOK != dlgaddshebei.DoModal()) return; ShebeiConn(); DispShebei(); } void CShebei::OnButton3() { // TODO: Add your control notification handler code here POSITION pos; pos = m_ListCtrl.GetFirstSelectedItemPosition(); if (pos == NULL){ MessageBox("你还没有选中列表项!"); return ; } m_pRs1->MoveFirst(); int i=m_ListCtrl.GetSelectionMark(); m_pRs1->Move(i); m_pRs1->Delete(adAffectCurrent); m_pRs1->Update(); MessageBox("删除成功!"); m_pRs1->MoveFirst(); DispShebei(); } void CAddShebei::OnButton1() { // TODO: Add your control notification handler code here if (!Youxiao()) return; AddShebeiConn(); m_pRs1->AddNew(); m_pRs1->PutCollect(CComVariant(0), CComVariant(m_genghuanriqi)); m_pRs1->PutCollect(CComVariant(1), CComVariant(m_genghuanyuanyin)); m_pRs1->PutCollect(CComVariant(2), CComVariant(m_shangxianchehao)); m_pRs1->PutCollect(CComVariant(3), CComVariant(m_xiaxianchehao)); m_pRs1->PutCollect(CComVariant(4), CComVariant(m_lutibianhao)); m_pRs1->PutCollect(CComVariant(5), CComVariant(m_genghuanleibie)); m_pRs1->Update(); MessageBox("添加成功!"); m_genghuanriqi=""; m_genghuanyuanyin=""; m_shangxianchehao=""; m_xiaxianchehao=""; m_lutibianhao=""; m_genghuanleibie=""; UpdateData(false); } CAddShebei::Youxiao() { UpdateData(true); m_genghuanriqi.TrimLeft(); if (m_genghuanriqi.IsEmpty()) { MessageBox("更换类别不能为空!"); return false; } m_genghuanyuanyin.TrimLeft(); if (m_genghuanyuanyin.IsEmpty()) { MessageBox("更换原因不能为空!"); return false; } m_shangxianchehao.TrimLeft(); if (m_shangxianchehao.IsEmpty()) { MessageBox("上线车号不能为空!"); return false; } m_xiaxianchehao.TrimLeft(); if (m_xiaxianchehao.IsEmpty()) { MessageBox("下线车号不能为空!"); return false; } m_lutibianhao.TrimLeft(); if (m_lutibianhao.IsEmpty()) { MessageBox("炉体编号不能为空!"); return false; } m_genghuanleibie.TrimLeft(); if (m_genghuanleibie.IsEmpty()) { MessageBox("更换类别不能为空!"); return false; } return true; } CAddShebei::AddShebeiConn() { m_pCon1.CreateInstance(__uuidof(Connection)); m_pCon1->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=lunwen.mdb;Persist Security Info=False" ,"","",-1); m_pRs1.CreateInstance(__uuidof(Recordset)); m_pRs1->Open("select * from 钢包车渣车换车记录", m_pCon1.GetInterfacePtr(), adOpenDynamic,adLockOptimistic,adCmdText); } void CAddShebei::OnCancel() { // TODO: Add extra cleanup here CDialog::OnOK(); } 下面这是原来的代码 void CAddShebei::OnButton1() { // TODO: Add your control notification handler code here if (!Youxiao()) return; AddShebeiConn(); m_pRs->AddNew(); m_pRs->PutCollect(CComVariant(0), CComVariant(m_Bianhao)); m_pRs->PutCollect(CComVariant(1), CComVariant(m_Xinghao)); m_pRs->PutCollect(CComVariant(2), CComVariant(m_Changjia)); m_pRs->PutCollect(CComVariant(3), CComVariant(m_Jiage)); m_pRs->PutCollect(CComVariant(4), CComVariant(m_Shuliang)); m_pRs->Update(); MessageBox("添加成功!"); m_Bianhao=""; m_Xinghao=""; m_Changjia=""; m_Jiage=""; m_Shuliang=""; UpdateData(false); } CAddShebei::Youxiao() { UpdateData(true); m_Bianhao.TrimLeft(); if (m_Bianhao.IsEmpty()) { MessageBox("编号不能为空!"); return false; } m_Xinghao.TrimLeft(); if (m_Xinghao.IsEmpty()) { MessageBox("型号不能为空!"); return false; } m_Changjia.TrimLeft(); if (m_Changjia.IsEmpty()) { MessageBox("厂家不能为空!"); return false; } m_Jiage.TrimLeft(); if (m_Jiage.IsEmpty()) { MessageBox("价格不能为空!"); return false; } m_Shuliang.TrimLeft(); if (m_Shuliang.IsEmpty()) { MessageBox("数量不能为空!"); return false; } return true; } CAddShebei::AddShebeiConn() { m_pCon.CreateInstance(__uuidof(Connection)); m_pCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MY.mdb;Persist Security Info=False" ,"","",-1); m_pRs.CreateInstance(__uuidof(Recordset)); m_pRs->Open("select * from 设备", m_pCon.GetInterfacePtr(), adOpenDynamic,adLockOptimistic,adCmdText); } void CAddShebei::OnCancel() { // TODO: Add extra cleanup here CDialog::OnOK(); } CShebei::CShebei(CWnd* pParent /*=NULL*/) : CDialog(CShebei::IDD, pParent) { //{{AFX_DATA_INIT(CShebei) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } void CShebei::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CShebei) DDX_Control(pDX, IDC_LIST1, m_ListCtrl); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CShebei, CDialog) //{{AFX_MSG_MAP(CShebei) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_BN_CLICKED(IDC_BUTTON3, OnButton3) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CShebei message handlers BOOL CShebei::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here // 创建列表控件的标题头 CString strHeader[5]={ "设备编号", "型号", "厂家", "价格", "数量"}; for (int nCol=0; nCol<5; nCol++) m_ListCtrl.InsertColumn(nCol,strHeader[nCol],LVCFMT_LEFT,85); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CShebei::OnButton1() { // TODO: Add your control notification handler code here ShebeiConn(); DispShebei(); } CShebei::DispShebei() { m_ListCtrl.DeleteAllItems(); int nItem = 0; while (!m_pRs->adoEOF) { vFieldValue=m_pRs->GetCollect("设备编号"); m_ListCtrl.InsertItem( nItem, (char*)_bstr_t(vFieldValue) ); vFieldValue.Clear(); vFieldValue=m_pRs->GetCollect("型号"); m_ListCtrl.SetItemText( nItem, 1, (char*)_bstr_t(vFieldValue) ); vFieldValue.Clear(); vFieldValue=m_pRs->GetCollect("厂家"); m_ListCtrl.SetItemText( nItem, 2, (char*)_bstr_t(vFieldValue) ); vFieldValue.Clear(); vFieldValue=m_pRs->GetCollect("价格"); m_ListCtrl.SetItemText( nItem, 3, (char*)_bstr_t(vFieldValue) ); vFieldValue.Clear(); vFieldValue=m_pRs->GetCollect("数量"); m_ListCtrl.SetItemText( nItem, 4, (char*)_bstr_t(vFieldValue) ); vFieldValue.Clear(); m_pRs->MoveNext(); nItem++; } } CShebei::ShebeiConn() { m_pCon.CreateInstance(__uuidof(Connection)); m_pCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MY.mdb;Persist Security Info=False" ,"","",-1); m_pRs.CreateInstance(__uuidof(Recordset)); m_pRs->Open("select * from 设备", m_pCon.GetInterfacePtr(), adOpenDynamic,adLockOptimistic,adCmdText); m_pRs->MoveFirst(); } void CShebei::OnButton2() { // TODO: Add your control notification handler code here CAddShebei dlgaddshebei; if (IDOK != dlgaddshebei.DoModal()) return; ShebeiConn(); DispShebei(); } void CShebei::OnButton3() { // TODO: Add your control notification handler code here POSITION pos; pos = m_ListCtrl.GetFirstSelectedItemPosition(); if (pos == NULL){ MessageBox("你还没有选中列表项!"); return ; } m_pRs->MoveFirst(); int i=m_ListCtrl.GetSelectionMark(); m_pRs->Move(i); m_pRs->Delete(adAffectCurrent); m_pRs->Update(); MessageBox("删除成功!"); m_pRs->MoveFirst(); DispShebei(); }