学生考试管理系统ADO版
2008-04-19 20:27:52 来源:WEB开发网那么pDoc->m_BaseTblList.GetFieldRecord(dialog1.RecordArray,this->m_CurTblName);如何实现把this->m_CurTblName的字段信息添加到RecordArray列表中去呢?
咱们看一下这个方法的定义:
//单表记录数组
bool CBaseTbl::GetFieldRecord(CPtrArray& FieldArray,CString ctblname,CString constr)
{
ASSERT(ctblname.Trim()!="");
//1.判断及清理工作
…省略
CFieldRecord* fldrec;
CADOFieldInfo fldinfo;
CString fldvalue;
//2.判断记录位置
bool IsZeroRecord;//记录数为0
…省略
int count=this->m_Storage.GetFieldCount();
//3.添加字段记录到指针数组
for(int i=0;i<count;i++)
{
fldrec=new CFieldRecord();
if(IsZeroRecord)
{
fldvalue="";
}
else
{
this->m_Storage.GetFieldValue(i,fldvalue);
}
this->m_Storage.GetFieldInfo(i,&fldinfo);
strcpy(fldrec->FKtbl,ctblname);
strcpy(fldrec->FieldName,fldinfo.m_strName);
strcpy(fldrec->Value,fldvalue);
switch(fldinfo.m_nType)
{
case VT_DATE:
fldrec->IsStrType=true;
fldrec->IsBool =false;
break;
…
default:
fldrec->IsStrType=false;
fldrec->IsBool =false;
break;
}
fldrec->pFK=NULL;
fldrec->IsVisible=true;
FieldArray.Add(fldrec);
}
//添加外键信息
this->AddExtraFieldRecord(FieldArray,ctblname);
return true;
}
基本过程就是两个:
一是添加fldrec=new CFieldRecord()字段信息;
二是查询是否有外键this->AddExtraFieldRecord(FieldArray,ctblname);并把外键信息添加到fldrec的链表尾部,然后在加入到 FieldArray.Add(fldrec);
②、有了这些工作下面的显示界面如图四、五就简单了,就是逆向的读取 FieldArray 的过程:
在CBMDialog类中:
//动态创建组件
CFieldRecord* pRecord;
int top=0;
for(int i=1;i<=this->RecordArray.GetCount();i++)
{
top=(i-1)*22;
pRecord=(CFieldRecord*)RecordArray.GetAt(i-1);
this->CreateStatic(pRecord,top,70+i);
//布尔或有关联外键
if((pRecord->IsBool)||(pRecord->pFK!=NULL))
this->CreateCombo(pRecord,top,10+i);
else
this->CreateEdit(pRecord,top,10+i);
}
注:有关联外件就读取关联表显示字段的索引信息,然后添加到 ComboBox 中,
显示出界面:
如何修改和添加记录呢?
//发送修改或添加消息
void CBMDialog::SendChangeMsg(void)
{
CWnd* pWnd;
CString value,Msg;
CFieldRecord* pRecord;
for(int i=1;i<=this->RecordArray.GetCount();i++)
{
pWnd=this->GetDlgItem(10+i);
if(pWnd)
{
pRecord=(CFieldRecord*)RecordArray.GetAt(i-1);
if ((!pRecord->IsBool)&&pWnd->IsKindOf(RUNTIME_CLASS(CComboBox)))
{ //非布尔类型,且有关联字段时
CComboBox* pCombo=(CComboBox*) pWnd;
CFldValue* p;
int index=pCombo->GetCurSel();
p=(CFldValue*)pCombo->GetItemDataPtr(index);
value=p->FieldValue;
}
else
pWnd->GetWindowText(value);
if(value.Trim()=="")
{
Msg=(CString)pRecord->FieldName+"不能为空!";
AfxMessageBox(Msg);
pWnd->SetFocus();
return;
}
else
{
if((i==1)&&(!this->ISADD))//如果是修改的话,则不对第一字段操作
{}
else
strcpy(pRecord->Value,value);
}
}
}
//发送字符串
CString cSend;
if(this->ISADD)
{
cSend=this->GenerateInsertSql();
}
else
{
cSend=this->GenerateUpdateSql();
}
LPARAM lparam=(LPARAM)&cSend;
::SendMessage((HWND)this->m_hParent,WM_USER+51,0,lparam);
}
这是基本的信息,根据显示内容生成sql语句,发送消息给基本表视,完成数据库更新操作。
③、视的更新过程:
LRESULT CStudentScoreView::OnExecSql(WPARAM wParam,LPARAM lParam)
{
CString c;
c=*((CString*)lParam);
CStudentScoreDoc* pDoc=this->GetDocument();
pDoc->m_BaseTblList.ExecSql(c);
this->RefreshShow();
//AfxMessageBox(c);
return 0;
}
更多精彩
赞助商链接