C++ access数据库中字段信息获取
博客专区 > RogerLee 的博客 > 博客详情
C++ access数据库中字段信息获取
RogerLee 发表于11个月前
C++ access数据库中字段信息获取
  • 发表于 11个月前
  • 阅读 0
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

void CDataControl::GetFieldNameCn(const CString& sTableName,CADORecordset* pAdoAccess,const CString& sNameEn, CString& sNameCn)
{
	SAFEARRAY   FAR*   psa   =   NULL; 
	SAFEARRAYBOUND   rgsabound; 
	_variant_t     var; 
	_variant_t     Array; 
	rgsabound.lLbound   =   0; 
	rgsabound.cElements   =   4; 
	psa   =   SafeArrayCreate(VT_VARIANT,   1,   &rgsabound); 
	var.vt   =   VT_EMPTY; 
	long   ix; 
	ix   =   0; 
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	ix=   1; 
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	ix   =   2; 
	var.vt   =   VT_BSTR; 
	var.bstrVal   =sTableName.AllocSysString();		//表名  sTableName 
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	var.vt   =   VT_BSTR; 
	ix   =   3; 
	var.bstrVal   =sNameEn.AllocSysString();//字段名
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	Array.vt   =   VT_ARRAY|VT_VARIANT; 
	Array.parray   =   psa;   
 
	pAdoAccess->m_pRecordset   =   pAdoAccess->GetActiveConnection()->OpenSchema(adSchemaColumns,&Array);  
 
	Field *fd = pAdoAccess->m_pRecordset->Fields->GetItem("DESCRIPTION");
	if (fd->GetValue().vt != VT_NULL)
	{
		_bstr_t  column_discription = fd->Value;
		CString sNote;
		sNote.Format(_T("%s"), (LPCTSTR)column_discription);
		sNameCn =  (LPCTSTR)column_discription;
	}
	else
	{
		sNameCn = sNameEn;
	}
}
 
BOOL CDataControl::GetFileNULL(const CString& sTableName,const CString& sFileNameEN,CString& sFileNameCN,CADORecordset* pAdoAccess,BOOL& ISNullSet, int& OrdinalPostion)
{
	SAFEARRAY   FAR*   psa   =   NULL; 
	SAFEARRAYBOUND   rgsabound; 
	_variant_t     var; 
	_variant_t     Array; 
	rgsabound.lLbound   =   0; 
	rgsabound.cElements   =   4; 
	psa   =   SafeArrayCreate(VT_VARIANT,   1,   &rgsabound); 
	var.vt   =   VT_EMPTY; 
	long   ix; 
	ix   =   0; 
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	ix=   1; 
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	ix   =   2; 
	var.vt   =   VT_BSTR; 
	var.bstrVal   =sTableName.AllocSysString();		//表名  sTableName 
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	var.vt   =   VT_BSTR; 
	ix   =   3; 
	var.bstrVal   =sFileNameEN.AllocSysString();//字段名
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	Array.vt   =   VT_ARRAY|VT_VARIANT; 
	Array.parray   =   psa;   
 
	pAdoAccess->m_pRecordset   =   pAdoAccess->GetActiveConnection()->OpenSchema(adSchemaColumns,&Array);  
	//IS_NULL = 1 表示字段可以为空   0 表示必填
	ISNullSet = (pAdoAccess->m_pRecordset->Fields->GetItem("IS_NULLABLE")->Value.boolVal == -1)?TRUE:FALSE;//是否允许为空
	_bstr_t  column_name = pAdoAccess->m_pRecordset->Fields->GetItem("COLUMN_NAME")->Value;//字段名
	OrdinalPostion = pAdoAccess->m_pRecordset->Fields->GetItem("ORDINAL_POSITION")->Value;//字段序号
 
	Field *fd = pAdoAccess->m_pRecordset->Fields->GetItem("DESCRIPTION");
	if (fd->GetValue().vt != VT_NULL)
	{
		_bstr_t  column_discription = fd->Value;
		CString sNote;
		sNote.Format(_T("%s"), (LPCTSTR)column_discription);
		sFileNameCN =  (LPCTSTR)column_discription;
	}
	else
	{
		sFileNameCN = sFileNameEN;
	}
 
	//int  NUMERIC_PRECISION = m_pRecordset->Fields->GetItem("NUMERIC_PRECISION")->Value;
	//int  NUMERIC_SCALE = m_pRecordset->Fields->GetItem("NUMERIC_SCALE")->Value;
 
 
	return TRUE;
}
 
BOOL CDataControl::GetFilePkName(const CString& sTableName,std::vector<CString>& sPkName,CADORecordset* pAdoAccess)
{
	SAFEARRAY   FAR*   psa   =   NULL; 
	SAFEARRAYBOUND   rgsabound; 
	_variant_t     var; 
	_variant_t     Array; 
	rgsabound.lLbound   =   0; 
	rgsabound.cElements   =   3; 
	psa   =   SafeArrayCreate(VT_VARIANT,   1,   &rgsabound); 
	var.vt   =   VT_EMPTY; 
	long   ix; 
	ix   =   0; 
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	ix=   1; 
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	ix   =   2; 
	var.vt   =   VT_BSTR; 
	var.bstrVal   =sTableName.AllocSysString();		//表名  sTableName 
	SafeArrayPutElement(psa,   &ix,   &var); 
 
	Array.vt   =   VT_ARRAY|VT_VARIANT; 
	Array.parray   =   psa;   
 
	pAdoAccess->m_pRecordset   =   pAdoAccess->GetActiveConnection()-> OpenSchema(adSchemaPrimaryKeys,&Array);  //adSchemaColumns
 
	while(!pAdoAccess->IsEOF())
	{
		_bstr_t  column_name = pAdoAccess->m_pRecordset->Fields->GetItem("COLUMN_NAME")->Value;
		_bstr_t   column_PK=   pAdoAccess->m_pRecordset-> Fields-> GetItem( "PK_NAME")-> Value; 
 
		sPkName.push_back((LPCTSTR)column_name);
		pAdoAccess->MoveNext();
	}
 
	return TRUE;
}
共有 人打赏支持
粉丝 3
博文 3
码字总数 1204
×
RogerLee
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: