MFC DAO编程中的复制基本表操作

来源:互联网 发布:youcam软件 编辑:程序博客网 时间:2024/05/20 20:56

Access是关系型数据库,根据关系型数据库理论,同一个数据库中一般不存在相同结构的基本表,因为没有必要,完全可以union起来。但是,在实际编程开发过程中,总会存在一些奇特的需求;比如我现在做的这个项目,一个井眼对应一个基本表,而这些基本表的结构却完全相同,又不能合并。既然没有必要存在相同结构的基本表,当让就不存在复制基本表(或基本表的结构)的函数了,因而只能自己用代码来完成;大致思路是这样,先创建一个新的表定义,然后获取已存在的的基本表的字段信息和索引信息,然后利用这些信息设置新表定义的对应信息,看代码:  m_pDataSet->Open();  CDaoTableDef *pTableDef=new CDaoTableDef(m_pDataSet->m_pDatabase);  pTableDef->Create(str);

  CDaoFieldInfo newfield;  int j=m_pDataSet->GetFieldCount();  for(int i=0;i<j;i++)  {   m_pDataSet->GetFieldInfo(i,newfield,AFX_DAO_SECONDARY_INFO);   pTableDef->CreateField(newfield);  }    CDaoIndexInfo newindex;  j=m_pDataSet->GetIndexCount();  for(int i=0;i<j;i++)  {   m_pDataSet->GetIndexInfo(2,newindex,AFX_DAO_SECONDARY_INFO);   pTableDef->CreateIndex(newindex);  }    pTableDef->Append();    m_pDataSet->Close();  m_pDataSet->Open(pTableDef);.........

剩下的工作就简单了,对新表进行数据插入操作。

两个结构体的定义如下:struct CDaoFieldInfo          //字段信息结构{   CStringm_strName;           // Primary   shortm_nType;               // Primary   longm_lSize; // Primary   longm_lAttributes;          // Primary   shortm_nOrdinalPosition;    // Secondary   BOOLm_bRequired;            // Secondary   BOOLm_bAllowZeroLength;     // Secondary   longm_lCollatingOrder;      // Secondary   CStringm_strForeignName;    // Secondary   CStringm_strSourceField;    // Secondary   CStringm_strSourceTable;    // Secondary   CStringm_strValidationRule; // All   CStringm_strValidationText; // All   CStringm_strDefaultValue;   // All};

struct CDaoIndexInfo              //索引信息结构{   CDaoIndexInfo( );                   // Constructor   CStringm_strName;                  // Primary   CDaoIndexFieldInfo*m_pFieldInfos;  // Primary   shortm_nFields;                    // Primary   BOOLm_bPrimary;                    // Secondary   BOOLm_bUnique;                     // Secondary   BOOLm_bClustered;                  // Secondary   BOOLm_bIgnoreNulls;                // Secondary   BOOLm_bRequired;                   // Secondary   BOOLm_bForeign;                    // Secondary   longm_lDistinctCount;              // All   // Below the // Implementation comment:   // Destructor, not otherwise documented};

其实我没有认真看着两个结构的定义,没必要嘛!还要对GetFieldInfo和GetIndexInfo两个函数的第三个参数说明一下://AFX_DAO_PRIMARY_INFO (默认值)函数将获得包括字段名、字段类型、字段尺寸及字段属性等基本信息//AFX_DAO_SECONDARY_INFO   不仅包括上面的信息,还包括字段序号、可控性、是否允许空字符串、排序规则、别名、源字段名以及源表名等辅助信息//AFX_DAO_ALL_INFO   不仅包括基本信息和辅助信息,还包括合法性规则以及合法性文版在内的全部信息当然,包含的信息越多,函数执行的速度越低,应当根据实际需求来取舍!

原创粉丝点击