C # ways to connect to MySQL and solutions to the GUID problems that arise

Source: Internet
Author: User

My environment:

Win7 64-bit

MySQL (version: Mysql-installer-community-5.7.4.0-m14.1411728256.msi)

VS2012

Using C # to connect to MySQL, I personally think it is a very bad thing, but in practice, I really have encountered such a situation. In order to complete a simple test, I made such an attempt.

1. Install MySQL

Simple Choice Plus Next, the fool-style operation, seemingly simple, but in the last step of the configuration when the problem occurs. Tip Let me try again, uninstall and reinstall the same. Finally I in the root password set up when the simple 1234, although still prompt the same error, I cancel the installation, open MySQL Workbench CE, unexpectedly can log in, appeared the test database. I'm surprised, and I don't know why, it's really amazing. If the great God knows why, please also tell.

2.c# connecting MySQL Database

Baidu A search connection database method is indeed many, I chose a relatively simple and convenient way to use the official MySQL connector/net.

1) First download mysql-connector-net file, file for Http://dev.mysql.com/downloads/connector/net/6.6.html#downloads (Hope also find), version of the optional, Platform selection. Net&mono, download zip-free version.

2) Unzip the zip file you just downloaded, there are several versions, here I chose V4.

3) Add several DLL link libraries under the V4 folder in the C # Reference.

4) Code Connection

1 stringConstr ="server=127.0.0.1;user=root;password=1234;database=test;port=3306";2Mysqlconnection Mycon =Newmysqlconnection (constr);3 Mycon. Open ();4 stringCmdstr ="SELECT * from Test.art;";5Mysqlcommand myCMD =NewMysqlcommand (Cmdstr, mycon);6Mysqldatareader reader =myCMD. ExecuteReader ();7  while(reader. Read ())8 {9    .......Ten } OneMycon. Close ();

The code to connect to MySQL is similar to the database attached to MSSQL. At the end of the connection you also have to release mycon resources. Of course you can also use the using () method to relieve the trouble of Mycon.

Note: You cannot use the ExecuteNonQuery () method to return the number of bars for a select query because the result is 1.

3. When you finish writing the code, you suddenly notice that the GUID should contain a 32-digit number with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx), instantly feeling helpless. What's wrong with the world?

My first feeling was that there was a problem connecting to the MySQL database. But online some people say is the problem of VS itself, need to delete the C disk under a certain three files, I delete after a pity or not. Finally found that the original is MySQL char (36) is mistaken for a GUID, char (36) is changed to char (40) (not char (36)), error is not prompt. It's really amazing.

Looks like the official MySQL connector. The field defined as char (36) is considered a GUID type.

C # ways to connect to MySQL and solutions to the GUID problems that arise

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.