carek 2004年12月22日
VC+ADO+Access 使用相同的绑定类,进行出库操作没问题,入库操作就是过不去.
我是初学者,想用VC+ADO+Access 做一个系统.可是编译完成后,进行入库操作 跳出"未知错误" 然后vc也提示出错,程序被强行关掉了.可是入库就不会啊.操作的都是一个表.大虾,高手指点啊~
出库:
void CDlgDevOut::OnBtnDoadd()
{
if (m_devs.GetCurSel()==CB_ERR)
{
MessageBox("请选择一个设备");
return;
}

if (!UpdateData())
return;

if ((m_max>0.0f) && (m_number>m_max))
{
MessageBox("超过最大限量!");
return;
}

_RecordsetPtr pRst = NULL;
CDevOutRs rs; // 这是跟表绑定的类

try
{
TESTHR(pRst.CreateInstance(__uuidof(Recordset)));

pRst->Open("device_out",_variant_t((IDispatch *) m_DBCnt, true),adOpenKeyset,adLockOptimistic,adCmdTable);

m_devs.GetWindowText(rs.m_sz_code,11);
sprintf(rs.m_sz_date,"%d-%d-%d %d:%d:%d",
m_date.GetYear(), m_date.GetMonth(), m_date.GetDay(),
m_time.GetHour(), m_time.GetMinute(), m_time.GetSecond());
strcpy(rs.m_sz_dept, m_dept);
sprintf(rs.m_sz_state, "%d", m_state);
strcpy(rs.m_sz_keeper, m_keeper);
rs.m_f_number = m_number;
strcpy(rs.m_sz_taker, m_taker);
strcpy(rs.m_sz_usage, m_usage);

COleSafeArray vaFieldlist, vaValuelist;
rs.FillFieldsArray(vaFieldlist,vaValuelist);
TESTHR(pRst->AddNew(vaFieldlist, vaValuelist));
pRst->Close();

CString sql_;
sql_.Format("SELECT * FROM device WHERE code='%s'", rs.m_sz_code);
_bstr_t sql = sql_;
pRst->Open(sql, _variant_t((IDispatch *) m_DBCnt, true),adOpenKeyset,adLockOptimistic,adCmdText);
if (pRst->GetRecordCount()==1)
{
CDevRs rsDev;
IADORecordBinding *picRs = NULL; //Interface Pointer declared.(VC++ Extensions)
TESTHR(pRst->QueryInterface(__uuidof(IADORecordBinding),(LPVOID*)&picRs));
TESTHR(picRs->BindToRecordset(&rsDev));

strcpy(rsDev.m_sz_code, rs.m_sz_code);
rsDev.m_f_cur = rsDev.m_f_cur - rs.m_f_number;

TESTHR(picRs->Update(&rsDev));
picRs->Release();
}
pRst->Close();
}
catch(_com_error& e)
{
AfxMessageBox(e.ErrorMessage());
return;
}

MessageBox("完成操作!");
m_log->AddLog("添加出库信息");

EndDialog(0);
}
----------------------------------------------------------------

入库:
void CDlgDevIn::OnBtnDiadd()
{
if (m_devs.GetCurSel()==CB_ERR)
{
MessageBox("请选择一个设备");
return;
}

if (!UpdateData())
return;

_RecordsetPtr pRst = NULL;
CDevInRs rs;

try
{
TESTHR(pRst.CreateInstance(__uuidof(Recordset)));

pRst->Open("device_in",_variant_t((IDispatch *) m_DBCnt, true),adOpenKeyset,adLockOptimistic,adCmdTable);

m_devs.GetWindowText(rs.m_sz_code,11);
sprintf(rs.m_sz_date,"%d-%d-%d %d:%d:%d",
m_date.GetYear(), m_date.GetMonth(), m_date.GetDay(),
m_time.GetHour(), m_time.GetMinute(), m_time.GetSecond());
strcpy(rs.m_sz_provider, m_provider);
strcpy(rs.m_sz_tel, m_tel);
rs.m_f_number = m_number;
rs.m_f_price = m_price;
strcpy(rs.m_sz_buyer, m_buyer);

COleSafeArray vaFieldlist, vaValuelist;
rs.FillFieldsArray(vaFieldlist,vaValuelist);

m_DBCnt->BeginTrans();

TESTHR(pRst->AddNew(vaFieldlist, vaValuelist));
pRst->Close();

CString sql_;
sql_.Format("SELECT * FROM device_in WHERE code='%s'", rs.m_sz_code);
_bstr_t sql = sql_;

pRst->Open(sql, _variant_t((IDispatch *) m_DBCnt, true),adOpenKeyset,adLockOptimistic,adCmdText);
if (pRst->GetRecordCount()==0)找不到到相同的设备号,就添加新记录
{
CDevRs rsDev;
IADORecordBinding *picRs = NULL; //Interface Pointer declared.(VC++ Extensions)
TESTHR(pRst->QueryInterface(__uuidof(IADORecordBinding),(LPVOID*)&picRs));
TESTHR(picRs->BindToRecordset(&rsDev));

strcpy(rsDev.m_sz_code, rs.m_sz_code);

rsDev.m_f_cur = rs.m_f_number;
rsDev.m_f_total = rs.m_f_number;
rsDev.m_f_max = 1;
rsDev.m_f_min = 1;

COleSafeArray vaFields, vaValues;
rsDev.FillFieldsArray(vaFields,vaValues);
TESTHR(pRst->AddNew(vaFields, vaValues));
} else 找到,则修改库存量
{
CDevRs rsDev;
IADORecordBinding *picRs = NULL; //Interface Pointer declared.(VC++ Extensions)
TESTHR(pRst->QueryInterface(__uuidof(IADORecordBinding),(LPVOID*)&picRs));
TESTHR(picRs->BindToRecordset(&rsDev));

strcpy(rsDev.m_sz_code, rs.m_sz_code);
rsDev.m_f_cur = rs.m_f_number + rsDev.m_f_cur;
rsDev.m_f_total = rs.m_f_number + rsDev.m_f_total;

TESTHR(picRs->Update(&rsDev));
picRs->Release();
}
pRst->Close();

m_DBCnt->CommitTrans();
}
catch(_com_error& e)
{
AfxMessageBox(e.ErrorMessage());
m_DBCnt->RollbackTrans();
return;
}

MessageBox("完成操作!");
m_log->AddLog("添加入库信息");

EndDialog(0);
}
...全文
144 点赞 收藏 14
写回复
14 条回复

还没有回复,快来抢沙发~

发动态
发帖子
数据库
创建于2007-09-28

3443

社区成员

3.9w+

社区内容

VC/MFC 数据库
社区公告
暂无公告