// TODO: add this extra initialization code
// Initialize the ADO environment
IF (AfxOleInit ()!) { AfxMessageBox (L "initialization failed the OLE"); return FALSE; }
// set the title's ListView
m_list.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
m_list.InsertColumn(0, L"ID", LVCFMT_CENTER, 80);
m_list.InsertColumn(1, L"mName", LVCFMT_CENTER, 80);
m_list.InsertColumn(2, L"mSex", LVCFMT_CENTER, 80);
m_list.InsertColumn(3, L"mAge", LVCFMT_CENTER, 80);
m_list.InsertColumn(4, L"mTel", LVCFMT_CENTER, 120);
// connect to the database
m_pConnection.CreateInstance (__ uuidof (Connection));
the try
{
// the following strings to get a long time to get right, the main problem is the file path right now did not understand, it is placed in the Debug directory project not connect.
// _ bstr_t connStr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = myDate1.accdb; Persist Security Info = False"; // phrase on the wrong, check for a long time, oh, wanted to give up.
_bstr_t connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\AccessData2007\\myDate1.accdb;Persist Security Info=False";
_bstr_t dataName = "Admin";
_bstr_t dataPass = "";
HRESULT hr=m_pConnection->Open(connStr,dataName,dataPass,adModeUnknown);
//MessageBox(L"数据连接成功");
}
catch (_com_error e)
{
AfxMessageBox( e.ErrorMessage());
return FALSE;
}
// disconnected recordsets
m_pRecordset.CreateInstance (__ uuidof (the Recordset));
the try
{
the _variant_t = SQL "SELECT * from myTable";
the HRESULT hret = m_pRecordset-> the Open (
SQL,
m_pConnection.GetInterfacePtr (),
adOpenDynamic,
adLockOptimistic,
adCmdText
);
// MessageBox (L "connection successful");
}
The catch (the _com_error * E)
{
the MessageBox (E-> the ErrorMessage ());
return FALSE;
}
// ListView control data is loaded into
the _variant_t var;
CString strTmp;
for (int I = 0;! M_pRecordset-> adoEOF; I ++ )
{
var = m_pRecordset-> GetCollect (_T ( "ID"));
strTmp = (LPCTSTR) _bstr_t (var);
m_list.InsertItem (I, strTmp);
var = m_pRecordset->GetCollect(_T("mName"));
strTmp = (LPCTSTR)_bstr_t(var);
m_list.SetItemText(i, 1, strTmp);
var = m_pRecordset->GetCollect(_T("mSex"));
strTmp = (LPCTSTR)_bstr_t(var);
m_list.SetItemText(i, 2, strTmp);
var = m_pRecordset->GetCollect(_T("mAge"));
strTmp = (LPCTSTR)_bstr_t(var);
m_list.SetItemText(i, 3, strTmp);
var = m_pRecordset->GetCollect(_T("mTel"));
strTmp = (LPCTSTR)_bstr_t(var);
m_list.SetItemText(i, 4, strTmp);
m_pRecordset-> the MoveNext ();
}
// close the recordset and database connections
m_pRecordset->Close();
m_pRecordset.Release();
m_pRecordset = NULL;
if (m_pConnection->State)
{
m_pConnection->Close();
}
m_pConnection = NULL;
// Close records and database connections set m_pRecordset-> the Close (); m_pRecordset.Release (); m_pRecordset = NULL; IF (m_pConnection-> State) { m_pConnection-> the Close (); } m_pConnection = NULL;