当前位置: 代码迷 >> VC >> ADO 获取字段名?解决办法
  详细解决方案

ADO 获取字段名?解决办法

热度:6727   发布时间:2013-02-25 00:00:00.0
ADO 获取字段名?
在ADO中怎么得到一个已知表的未知字段名称.如下表:
表名称为USER
ID     NAME     AGE                               ..........1
1       JACK     30                                 ..........2
2       TIM       18                                 ..........3
3       JIM       16                                 ..........4

我应该如何得到1中的名称.用到了ADO中的什么方法.表的列数已经取得,需要知道的就是取字段名的方法.在网上找半天没找到....

------解决方案--------------------------------------------------------
把表放入datatable, 然后读取不就可以了吗?
------解决方案--------------------------------------------------------
#import "c:\Program Files\Common Files\system\ado\msadox.dll " no_namespace
#import "c:\Program Files\Common Files\system\ado\msado15.dll "
#include <stdio.h>

int main()
{
if(FAILED(::CoInitialize(NULL)))
return 1;
_CatalogPtr pCatalog = NULL;

_bstr_t strcnn( "Provider=SQLOLEDB.1;Password=xxxx;Persist Security Info=True; "
"User ID=sa;Initial Catalog=pubs;Data Source=server ");

pCatalog.CreateInstance(__uuidof (Catalog));
pCatalog-> PutActiveConnection(strcnn);

_variant_t varIndex((long)-1);
_KeyPtr pKey = NULL;
KeyTypeEnum enumKey ;
try
{
while (1)//i don 't how to decide the key 's cnt,so just make a throw error
{
++varIndex.llVal;
pKey = pCatalog-> Tables-> GetItem( "titleauthor ")-> Keys-> GetItem(varIndex);
static const char *KEYDESC [] = { "adKeyPrimary ", "adKeyForeign ", "adKeyUnique "};
enumKey = pKey-> GetType ();
printf( "%s\t%d\t%s\r\n ",(char*)pKey-> GetName (),enumKey,KEYDESC[enumKey-1]);
}

}
catch(...)
{
}
::CoUninitialize();
return 0;
}


------解决方案--------------------------------------------------------
用字段索引即可
------解决方案--------------------------------------------------------
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance( "ADODB.Recordset ");
HRESULT hr = m_pRecordset-> Open( "SELECT * FROM student ",_variant_t((IDispatch *)theApp.m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);//Access
m_strName = m_pRecordset-> GetCollect( "name ").bstrVal;
------解决方案--------------------------------------------------------
ADO?

rs.Field(0).Name
  相关解决方案