本人以前没玩过数据库,目前做 一个项目,在主窗口中使用ADO方式打开老数据库,并操作老数据库。 现在需要在子窗口中往数据库中写数据,在子窗口中已经获取老父窗口的句柄,但是不能获取到定义操作数据库的指针。 我的数据库连接指针是在父窗口中的InitInstance函数中定义的,因为在打开父窗口之前有一个登陆过程,使用到数据库。
CPerCardSystemApp theApp;CString strUser;
_ConnectionPtr m_pConnection; //数据库指针
_RecordsetPtr m_pRecordset;/////////////////////////////////////////////////////////////////////////////
// CPerCardSystemApp initializationBOOL CPerCardSystemApp::InitInstance()
{
// 初始化COM,创建ADO连接等操作
AfxOleInit(); AfxEnableControlContainer(); m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset)); m_pConnection->CommandTimeout = 5; CString strConnect = "Provider=SQLOLEDB;Data Source=DEREK\\SQLEXPRESS;Initial Catalog=;Persist Security Info=True;User ID=;Password=;";
m_pConnection->Open((_bstr_t)strConnect,"","",adConnectUnspecified);
}
//登陆框
CDialogLogon dlglogon;
int isLogon = FALSE;
// m_pMainWnd = &dlg;
int Response = dlglogon.DoModal();
if (!isLogon)
{
if (Response == IDOK)
{
strUser = dlglogon.m_user;
isLogon = TRUE;
}
else if (Response == IDCANCEL)
{
isLogon = FALSE;
return FALSE;
}
}
//显示主窗口
if (isLogon)
{
CPerCardSystemDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
}
else if (nResponse == IDCANCEL)
{
}
} return FALSE;
}
子窗口获取父窗口的句柄 CSystemDlg *pNew = (CSystemDlg*)AfxGetApp()->GetMainWnd();
http://blog.csdn.net/xianglitian/archive/2010/05/22/5617173.aspx
成全局变量CPerCardSystemApp.cpp中,InitInstance函数定义:m_pConnection.CreateInstance(__uuidof(Connection));,在要使用的.cpp文件中
extern _ConnectionPtr m_pConnection; // Connection对象
那在子窗口中还需要重新m_pConnection.open()吗?