ADO如何取得数据库中表的表名

92 views 十二月 21, 04 by Timothy

_variant_t vFieldValue;
CString strFieldValue;
m_pRs=m_pConnection->OpenSchema(adSchemaTables);
while(VARIANT_FALSE==m_pRs->IsEOF)
{
strFieldValue=(char*)_bstr_t(m_pRs->GetCollect(“TABLE_TYPE”));
if(!strcmp(strFieldValue.GetBuffer(0),”TABLE”)||!strcmp(strFieldValue.GetBuffer(0),”table”))
{
strFieldValue.ReleaseBuffer();
strFieldValue=(char*)_bstr_t(m_pRs->GetCollect(“TABLE_NAME”));
m_ctlList.AddString(strFieldValue); ///把所有的表名加到控件
}
m_pRs->MoveNext();
}
m_pRs->Close();

ADO连接SQL的一般过程

102 views 十二月 20, 04 by Timothy

void ADOConn::OnInitADOConn()
{
::CoInitialize(NULL);
// 初始化OLE/COM库环境 ,为访问ADO接口做准备

try
{
// 创建Connection对象
m_pConnection.CreateInstance(“ADODB.Connection”);
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect = “Provider=SQLOLEDB; Server=flyspider;Database=HrSys; uid=sa; pwd=;”;
m_pConnection->Open(strConnect,”",”",adModeUnknown);
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}
}

// 执行查询
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{
// 连接数据库,如果Connection对象为空,则重新连接数据库
if(m_pConnection==NULL)
OnInitADOConn();
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}
// 返回记录集
return m_pRecordset;
}

// 执行SQL语句,Insert Update _variant_t
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
//_variant_t RecordsAffected;
try
{
// 是否已经连接数据库
if(m_pConnection == NULL)
OnInitADOConn();
// Connection对象的Execute方法:(_bstr_t CommandText,
// VARIANT * RecordsAffected, long Options )
// 其中CommandText是命令字串,通常是SQL命令。
// 参数RecordsAffected是操作完成后所影响的行数,
// 参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名
// adCmdProc-存储过程;adCmdUnknown-未知
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}

void ADOConn::ExitConnect()
{
// 关闭记录集和连接
if (m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close();
// 释放环境
::CoUninitialize();
}