I have been working on a log monitoring system over the past two days. I am using a MySQL database and have encountered many problems. I would like to summarize them here and hope to help you.
First, the date field in the database uses text, and there are many problems in the result query by date, which can be solved using the date (colname) function.
Second, garbled characters are displayed in database queries. They are displayed with question marks. Many codes are queried online, and database encoding settings and field encoding settings are modified, C # The encoding settings of the database Connection string do not work. Finally, add the Connection
The code is configured. The sample code is as follows:
// The Key is string configuration.
String connstr = "Data Source =" + Config. database + "; Password =" + Config. password + "; User ID =" + Config. username + "; Location =" + Config. location + "; charset = gbk ";
Conn = new MySQLConnection (connstr );
Conn. Open ();
// Key code. Set the connection encoding after the connection is opened.
MySQLCommand command = new MySQLCommand ("set names gbk", conn );
Command. ExecuteNonQuery ();
// Normal business
String querySize = "select count (*) from" + Config. table + "where date (log_time) = '" + time + "'";
MySQLCommand cmd = new MySQLCommand (querySize, conn );
Int count = 0;
MySQLDataReader reader = cmd. ExecuteReaderEx ();
If (reader. Read ())
{
Count = Convert. ToInt32 (reader [0]. ToString ());
}
Author "my website"