First, include # import "C:/program files/common files/system/ADO/msado15.dll" RENAME ("EOF", "adoeof") in the header file and create the connection object ADODB :: _ connectionptr m_connection;
In CPP:
Initialize listctrl:
M_listctrl.insertcolumn (0, _ T ("project name"), lvcfmt_center, 100 );
M_listctrl.insertcolumn (1, _ T ("left boundary"), lvcfmt_center, 100 );
M_listctrl.insertcolumn (2, _ T ("upper boundary"), lvcfmt_center, 100 );
M_listctrl.insertcolumn (3, _ T ("Right Border"), lvcfmt_center, 100 );
M_listctrl.insertcolumn (4, _ T ("bottom boundary"), lvcfmt_center, 100 );
M_listctrl.setextendedstyle (lvs_ex_fullrowselect | lvs_ex_gridlines); // you can select the entire row.
// Connect to the Database Function
Int cdbconnectdlg: open (maid server, maid dB, maid user, maid password)
{
Hresult comhr =: coinitialize (null );
If (failed (comhr ))
{
Return-1;
}
Hresult hR = m_connection.createinstance (_ uuidof (ADODB: Connection ));
If (failed (HR ))
Return-1;
Cstring STR;
Str. Format (_ T ("driver = SQL Server; server = % s; database = % s"), server, DB );
Try
{
// Connect to the database on the server
M_connection-> open (lpctstr) STR, user, password, ADODB: admodeunknown );
If (failed (HR ))
Return-1;
}
Catch (_ com_error & ERR)
{
Trace (_ T ("database operation failed! Error message: % s, file: % s, row: % d./N "), Err. errormessage (), _ file __, _ line __);
Return-1;
}
Return 0;
}
Void cdbconnectdlg: onbnclickedok ()
{
// Todo: add the control notification handler code here
Updatedata (true );
If (this-> open (m_strdbsever, m_strdbname, m_strusername, m_struserpwd) =-1)
{
Afxmessagebox (_ T ("the database has a problem, boss! "), Mb_ OK, null );
Return;
}
// Create a command object
ADODB: _ commandptr cmd;
Hresult hR = cmd. createinstance (_ uuidof (ADODB: Command ));
If (failed (HR) return;
Cstring STR = _ T ("select * From table_project ");
CMD-> activeconnection = m_connection;
CMD-> commandtext = (lpctstr) STR;
CMD-> commandtype = ADODB: ad1_text;
Try
{
// Query all project information
ADODB: _ recordsetptr rs = cmd-> execute (null, null, ADODB: adcmdunknown );
If (failed (HR) return;
Int I = 0;
RS-> movefirst ();
While (! RS-> getadoeof ())
{
// Obtain all properties of the project
_ Variant_t prj_id = RS-> getcollect ("project_id"); // name
_ Variant_t left = RS-> getcollect ("coverage_left"); // left boundary
_ Variant_t Top = RS-> getcollect ("coverage_top"); // upper boundary
_ Variant_t right = RS-> getcollect ("coverage_right"); // right boundary
_ Variant_t Bottom = RS-> getcollect ("coverage_bottom"); // bottom boundary
_ Variant_t width = RS-> getcollect ("cell_width"); // split width
_ Variant_t Height = RS-> getcollect ("cell_height"); // split height
M_listctrl.insertitem (I, cstring (lpstr (_ bstr_t (prj_id ))));
M_listctrl.setitemtext (I, 0, cstring (lpstr (_ bstr_t (prj_id ))));
M_listctrl.setitemtext (I, 1, cstring (lpstr (_ bstr_t (left ))));
M_listctrl.setitemtext (I, 2, cstring (lpstr (_ bstr_t (top ))));
M_listctrl.setitemtext (I, 3, cstring (lpstr (_ bstr_t (right ))));
M_listctrl.setitemtext (I, 4, cstring (lpstr (_ bstr_t (bottom ))));
RS-> movenext ();
++ I;
}
}
Catch (_ com_error & ERR)
{
Trace (_ T ("database operation failed! Error message: % s, file: % s, row: % d./N "), Err. errormessage (), _ file __, _ line __);
Return;
}
Return;
Onok ();
}