_variant_t name;
m_rs.CreateInstance("adodb.recordset");
CString s;
s.Format("select * from worker");
m_rs->Open(s,m_pConnection,3,3,0);
name=m_rs->GetCollect("用户名");
s.Format("%s",name.bstrVal);
m_username=s;
UpdateData(false);
为什么提示'm_pConnection':undeclared identifier
m_rs.CreateInstance("adodb.recordset");
CString s;
s.Format("select * from worker");
m_rs->Open(s,m_pConnection,3,3,0);
name=m_rs->GetCollect("用户名");
s.Format("%s",name.bstrVal);
m_username=s;
UpdateData(false);
为什么提示'm_pConnection':undeclared identifier
m_pConnection.CreateInstance("adodb.Connection")
m_pConnection.Open() '具体open什么,自己去查书,不同的数据库这里要写的不同
{
AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
AfxOleInit();///初始化COM库
////////////连接数据库//////////////
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Data Source=moto","","",adModeUnknown);///连接数据库
///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif CAsdDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
} // Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
在APP中已经定义了m_pConnection
要保证m_rs->Open(s,m_pConnection,3,3,0);的语句能够访问m_pConnection
(你的m_pConnection是定义在那里?)找找变量作用域方面有没有问题
m_pConnection.CreateInstance("ADODB.Connection");
try
{
m_pConnection->Open("Provider=Microsoft Jet 4.0 OLE DB Provider;Data Source=e:\\lgmq.mdb",
"","",adModeUnknown);
}
}
catch(_com_error &e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
pRst.CreateInstance("ADODB.Recordset");
pRst->CursorLocation=adUseClient;
try
{
pRst->Open("SELECT * FROM 班报", m_pConnection.GetInterfacePtr(), adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error * e)
{
AfxMessageBox(e->ErrorMessage());
}
m_gridban1.SetCaption("班数据");
m_gridban1.SetRefDataSource(NULL);
m_gridban1.SetRefDataSource( (LPUNKNOWN) pRst );
}
public:
_pConnection m_pConnection;
_pRecordset pRst;
但你的open 不一定在CAsdApp 中,要么你在同一类中定义和使用
要么定义不同的指针,进行传递如下:
如:xxx类中定义:
public:
_ConnectionPtr m_pConnection;
_RecorsetPtr rs;
在CAsdApp类调用xxx类时
xxx->m_pConnection=m_pConnection;
本人将m_pConnection移到与m_rs一起后解决问题
多谢各位
但新问题又来了
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance("adodb.connection");
if(SUCCEEDED(hr))
{
hr=m_pConnection->Open("data source=moto","","",0);
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
// TODO: Add extra initialization here
_variant_t name;
try
{
hr=m_rs.CreateInstance("adodb.recordset");
if(SUCCEEDED(hr))
{
hr=m_rs->Open("select * from worker",_variant_t((IDispatch *)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("打开数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
try
{
m_rs->MoveFirst();
name=m_rs->GetCollect("用户名");
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("获取数据失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
为什么提示
获取数据失败
错误信息:指针无效
好象recordsetPtr 的ActiveConnection 属性为指定连接指针
_variant_t vConn; try
{
if(m_pConn->State != adStateOpen)
{
return FALSE;
} if(m_pRecordset->State != adStateClosed)
{
m_pRecordset->Close();
} vConn = (IDispatch *)m_pConn;//Connection对象指针来构造一个_variant_t对象
m_pRecordset->Open(lpszSQL,vConn,adOpenStatic,adLockOptimistic,adCmdText); return TRUE;
}