读书人

ADO 获取字段名?解决办法

发布时间: 2012-03-04 11:13:33 作者: rapoo

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;
}


[解决办法]
用字段索引即可
[解决办法]
_RecordsetPtrm_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

读书人网 >VC

热点推荐