我做了一个com接口,其中有一部分需要读数据库,编译后错误提示:Expression:m_spRowset !=0,怎么修改?跟踪后发现OpenDataSource返回的hr值为0,似乎数据库连接就有问题?
程序如下:class CdboDay : public CCommand<CAccessor<CdboDayAccessor> >
{
public:
//constructor
    CdboDay()
{
m_strFilter=new char[1];
    strcpy(m_strFilter,"");
}
//destructor
   ~CdboDay()
{
delete m_strFilter;
}
HRESULT Open()
{
HRESULT hr; hr = OpenDataSource();  //调试时值为0
if (FAILED(hr))
return hr; return OpenRowset();
}
HRESULT OpenDataSource()  
{
HRESULT hr;
CDataSource db;
CDBPropSet dbinit(DBPROPSET_DBINIT); dbinit.AddProperty(DBPROP_AUTH_PASSWORD, OLESTR("USER"));
dbinit.AddProperty(DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false);
dbinit.AddProperty(DBPROP_AUTH_USERID, OLESTR("WEB"));
dbinit.AddProperty(DBPROP_INIT_CATALOG, OLESTR("PB"));
dbinit.AddProperty(DBPROP_INIT_DATASOURCE, OLESTR("J"));
dbinit.AddProperty(DBPROP_INIT_LCID, (long)2052);
dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
hr = db.Open(_T("SQLOLEDB.1"), &dbinit);
if (FAILED(hr))
return hr; return m_session.Open(db);
}
HRESULT OpenRowset()
{
// Set properties for open
CDBPropSet propset(DBPROPSET_ROWSET);
propset.AddProperty(DBPROP_IRowsetChange, true);
propset.AddProperty(DBPROP_UPDATABILITY, DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_INSERT | DBPROPVAL_UP_DELETE); // Add
    //char newSQL[512];
char newSQL[1000];
    char *SQLCommand;
    GetDefaultCommand((const char * *)& SQLCommand);
    strcpy(newSQL,SQLCommand);
    if (strlen(m_strFilter))
{
   strcat(newSQL," WHERE ");
   strcat(newSQL,m_strFilter);
}
return CCommand<CAccessor<CdboDayAccessor> >::Open(m_session, newSQL, &propset);
}
char *m_strFilter;
CSession m_session;
};在函数中的调用部分:
CdboDay MyDay;
// delete MyDay.m_strFilter;
// MyDay.m_strFilter = new char[25];
strcpy (MyDay.m_strFilter, "AppyDay = '");
strcat (MyDay.m_strFilter, pdate);
strcat (MyDay.m_strFilter, "'");
MyDay.Close();
hr=MyDay.Open();
         hr=MyDay.MoveFirst();
while (SUCCEEDED(hr))
{
temp[0][0]=MyDayBidedEnergySWJ21001FGEN.m_015; //m_015是表中一个数据项
         }