C # garbled Solution

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.