ADO 中recordset->open() 错误 本人新手,刚接触ADO
先贴代码:
if (!m_fConnected) return FALSE;
HRESULT hr;
CString strQuery;
CString strValue;
VARIANT var_value;
VARIANT_BOOL ValEof;
int nRowCount;
Field *field = NULL;
_variant_t var_t;
_bstr_t bst_t;
VARIANT varCounter;
varCounter.vt = VT_I4;
varCounter.lVal = 0;
//建立查询字符窜
strQuery.Format(_T("select*from%s"), strTableName);
_bstr_t query = strQuery;
_bstr_t source = m_strSource;
try{
//打开结果集
hr = m_recordset->Open(query, source, adOpenDynamic, adLockOptimistic, adCmdText);
ValEof = m_recordset->get_adoEOF(&ValEof);
while (TRUE)
{
if (ValEof)
break;
varCounter.lVal = 0;
//读取字段内容
m_recordset->Fields->get_Item(varCounter, &field);
field->get_Value(&var_value);
uaID.Add(var_value.lVal);
varCounter.lVal = 1;
m_recordset->Fields->get_Item(varCounter, &field);
field->get_Value(&var_value);
var_t = var_value;
bst_t = var_t;
saArray.Add(bst_t);
//
nRowCount++;
m_recordset->MoveNext();
m_recordset->get_adoEOF(&ValEof);
}
//关闭结果集
m_recordset->Close();
}
catch (_com_error &e)
{
MessageBox(e.ErrorMessage());
return FALSE;
}
return(SUCCEEDED(hr));
这其实为数据库编程ADO上的一个实例,用过的应该有印象。
在执行hr = m_recordset->Open(query, source, adOpenDynamic, adLockOptimistic, adCmdText);这步后,程序直接进入catch中的MessageBox(e.ErrorMessage());
弹出的内容为 :未定义的错误
求大神指点啊,哪里出错了急 急~~~
------解决方案--------------------这是我代码里的写法,可以正常使用
m_pConnection->Open("Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=DFSF;Data Source=192.168.1.100,10000","","",adModeUnknown);
------解决方案--------------------
::CoInitialize(NULL);
try
{
// 创建Connection对象
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open("Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=DFSF;Data Source=192.168.1.100,10000","","",adModeUnknown);
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}
try
{
CString sqlstr="select * from model";
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->CursorLocation=adUseClient;
// 取得表中的记录
m_pRecordset->Open(_bstr_t(sqlstr),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}