以下是一段ADO数据库代码。其中有几个地方不懂,请各位帮解释一下。
HRESULT是什么类型?
strQuery.Format起什么作用?
_bstr_t是啥类型?
adOpenDynamic, adLockOptimistic, adCmdText都是什么意思?
小弟多谢了!
BOOL CADODemoView::OpenCurRecordset(CString strTableName)
{
if(!m_fConnected) return FALSE;
if(strTableName.IsEmpty()) return FALSE;
//
HRESULT hr;
CString strQuery;
strQuery.Format("select * from %s", strTableName);
_bstr_t query = strQuery;
_bstr_t source = m_strSource;
try{
hr = m_recordset->Open(query, source, adOpenDynamic, adLockOptimistic, adCmdText);
} catch (_com_error &e){
MessageBox(e.ErrorMessage());
return FALSE;
}
return (SUCCEEDED(hr));
}
HRESULT是什么类型?
strQuery.Format起什么作用?
_bstr_t是啥类型?
adOpenDynamic, adLockOptimistic, adCmdText都是什么意思?
小弟多谢了!
BOOL CADODemoView::OpenCurRecordset(CString strTableName)
{
if(!m_fConnected) return FALSE;
if(strTableName.IsEmpty()) return FALSE;
//
HRESULT hr;
CString strQuery;
strQuery.Format("select * from %s", strTableName);
_bstr_t query = strQuery;
_bstr_t source = m_strSource;
try{
hr = m_recordset->Open(query, source, adOpenDynamic, adLockOptimistic, adCmdText);
} catch (_com_error &e){
MessageBox(e.ErrorMessage());
return FALSE;
}
return (SUCCEEDED(hr));
}
COM 函数或方法的返回值就是一个 HRESULT. 它的值如下所示:
E_NOINTERFACE The QueryInterface function did not recognize the requested interface. The interface is not supported.
E_NOTIMPL The function contains no implementation.
E_FAIL An unspecified failure has occurred.
E_OUTOFMEMORY The function failed to allocate necessary memory.
E_POINTER Invalid pointer.
E_INVALIDARG One or more arguments are invalid.
E_UNEXPECTED A catastrophic failure has occurred.
E_HANDLE Invalid handle.
E_ABORT Operation aborted. strQuery.Format起什么作用?
组合CString字符串啊,这是CString的一个Format函数_bstr_t是啥类型?
BSTR, 宽字符串,Unicode, COM中使用。因为COM要实现语言无关性,C++写的COM VB可以用。adOpenDynamic, adLockOptimistic, adCmdText都是什么意思?
adOpenDynamic 游标类型,这个是动态游标,还有adOpenStatic,静态游标 等等。
adLockOptimistic 锁类型,这个是优化锁,一般这个参数即可
adCmdText 标识符,这里是说,使用查询文本获得结果集,adCmdTable是指打开表。
ADO 是一个标准的、用于数据库连接的 COM 组件,你的问题多是COM使用规则问题。
strQuery.Format格式化字符串
其它的查看msdn很简单,对了参照vb的她是中文很容易看懂,只要你人学
strQuery.Format("select * from %s", strTableName)是初始化查询语句;
_bstr_t是基于COM的一种对字符串的变量定义;
关于记录集的open方法:
recordset.Open Source, ActiveConnection, CursorType, LockType, Options
Parameters:
Source
Optional. A Variant that evaluates to a valid Command object, an SQL statement, a table name, a stored procedure call, a URL, or the name of a file or Stream object containing a persistently stored Recordset.
ActiveConnection
Optional. Either a Variant that evaluates to a valid Connection object variable name, or a String that contains ConnectionString parameters.
CursorType
Optional. A CursorTypeEnum value that determines the type of cursor that the provider should use when opening the Recordset. The default value is adOpenForwardOnly.
LockType
Optional. A LockTypeEnum value that determines what type of locking (concurrency) the provider should use when opening the Recordset. The default value is adLockReadOnly.
Options
Optional. A Long value that indicates how the provider should evaluate the Source argument if it represents something other than a Command object, or that the Recordset should be restored from a file where it was previously saved. Can be one or more CommandTypeEnum or ExecuteOptionEnum values, which can be combined with a bitwise AND operator.