字段 自动编号
我想做一表,表中已有自动编号的ID。但我还想把一个字段(属性:长整型)自动编号,添加一个新记录根据另一字段自动加1,想用一个函数实现。怎么实现?
[解决办法]
不如用触发器或者存储过程来完成吧。
[解决办法]
long CUserDlg::GenNewID(){ CString sql; long NewID; _RecordsetPtr m_pRecordset; //Must define it in function!!!! sql="SELECT Max(ID) FROM USERS"; try { m_pRecordset.CreateInstance("ADODB.Recordset"); m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); _variant_t vIndex = (long)0;//How to get field value in only 1 record and 1 field condition. _variant_t vtemp = m_pRecordset->GetCollect(vIndex); if(vtemp.lVal>0) NewID =(long)(m_pRecordset->GetCollect(vIndex))+1;///取得第一个字段的值(MAX ID)加1后放入id变量. else NewID=1; m_pRecordset->Close(); } catch(_com_error e)///捕捉异常 { CString stemp; stemp.Format("获得ID最大值出错:%s",e.ErrorMessage()); AfxMessageBox(stemp); } return NewID;}