When inserting Chinese characters into MySql in. net, the question mark is garbled.

Source: Internet
Author: User

Modify the my. ini file
Default-character-set = utf8
Download the ODBC driver for MySQL for. net2.0 from this dude's Resource
Http://download.csdn.net/source/214322
After installation, you can add a data connection to the server resource manager in vs2005.
Click [change] in the data source option and select another one. You will see. NET Framework data provider for MySQL in [data provider ].
Enter the IP address of your MySQL server in server name and localhost in Local Machine
Enter the user name, password, and database name as required, and select Save my password.
Then go to the [advanced] Option
Set character set in advanced to utf8 and click [OK]. Now all settings are complete.
Test connection ---- the connection is normal.
The generated connection string is as follows:
Server = localhost; user id = root; Password = root; persist Security info = true; database = webdata; character set = utf8 // The password is hidden, in fact, no password will appear in the string, but the password will appear in the Web configuration file in Asp.net.

The character set in the connection string must be consistent with the character set in the database!

Use the namespace mysql. Data. mysqlclient to connect to the MySQL server.
In C #, you can use the using statement to introduce the MySQL data interface:

Using mysql. Data. mysqlclient;

After the namespace is specified, we can interact with the MySQL database.
MySQL. Data. mysqlclient this namespace provides many classes for processing Mysql Data.
Below is a sample of these classes:

* Mysqlconnection: Manages connections to MySQL servers/databases;

* Mysqldataadapter: a set of commands and connections used to fill the DataSet object and update the MySQL database;

* Mysqldatareader: allows you to read data from a MySQL database. It is a one-way data stream;

* Mysqlcommand: Provides the function of sending commands to the database server;

* Mysqlexception: Provides exception handling when a problem occurs.

Combination of ASP. NET and MySQL
Sending the show databases command to the MySQL server using the mysqlcommand object is the same as inputting this command directly in the MySQL management tool. The only difference is that we must use another object in the code to obtain the result set. The mysqldatareader object is instantiated when obtaining results (through the executereader method of the mysqlcommand class ). The getstring method of the mysqldatareader object is used to display data in the result set through ASP. NET label control. The pointer 0 of the getstring method specifies the first column of data that shows the current row of the result set (in the while loop.
========================================================== ========================================================== ==================================
Connection:

String connstr = "Server = localhost; user id = root; Password =; database = AA; pooling = false ";//

Connection string

Reference content is as follows:
Mysqlconnection conn = new mysqlconnection (connstr); // construct a database connection

Try
{
Conn. open (); // open the connection
Mysqlcommand cmd = new mysqlcommand ("select * from user", Conn); // construct a query command
This. datagrid1.datasource = cmd. executereader (); // executes the query, returns a datareader, and sets the data source of datagrid1 as the datareader
This. datagrid1.databind (); // D1 Data Binding
Conn. Close (); // close the connection
}
Catch (mysqlexception ex) // catch exceptions
{
Response. Write (ex. Message); // An error occurred while writing data to the page.

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.