This part is part of the VC and database projects. Later I modified it into a student achievement information management system and used these controls.
To display information in the database, you must first connect to the database and display it in the CList Control. If you do not need to talk about it, directly run the Code:
M_ListValue.DeleteAllItems ();
MYSQL * conn;
Conn = mysql_init (NULL );
If (mysql_real_connect (conn, "localhost", "root", "123456", "mysql", 3306, NULL, 0) = NULL)
{
AfxMessageBox ("database connection failed! ");
}
Else
{
Char * ch_query;
Ch_query = "select * from score ";
If (mysql_real_query (conn, ch_query, (UINT) strlen (ch_query ))! = 0)
{
AfxMessageBox ("the table data is incorrect! ");
}
CString str;
MYSQL_RES * result;
MYSQL_ROW row;
If (! (Result = mysql_use_result (conn )))
{
AfxMessageBox ("failed to read data! ");
}
Int I = 0;
While (row = mysql_fetch_row (result ))
{
Str. Format ("% s", row [0]);
M_ListValue.InsertItem (I, str );
Str. Format ("% s", row [1]);
M_ListValue.SetItemText (I, 1, str );
Str. Format ("% s", row [2]);
M_ListValue.SetItemText (I, 2, str );
Str. Format ("% s", row [3]);
M_ListValue.SetItemText (I, 3, str );
Str. Format ("% s", row [4]);
M_ListValue.SetItemText (I, 4, str );
Str. Format ("% s", row [5]);
M_ListValue.SetItemText (I, 5, str );
Str. Format ("% s", row [6]);
M_ListValue.SetItemText (I, 6, str );
Str. Format ("% s", row [7]);
M_ListValue.SetItemText (I, 7, str );
Str. Format ("% s", row [8]);
M_ListValue.SetItemText (I, 8, str );
I ++;
}
Mysql_free_result (result );
}
M_ListValue is the variable associated with CList Control.