. Net connection to MySQL database solution and example

Source: Internet
Author: User
Tags msdr

The following articles mainly describe to you.. net to connect to the MySQL database. net to connect to the MySQL database. We mainly divide it into three parts. The following article describes the detailed content.

Http://dev.csdn.net/develop/article/73/73226.shtm

Method and example for connecting to the MySQL database

Method 1:

MySQL Connector/Net is an ADO. NET driver for MySQL

This component is a. NET access component designed for accessing the MySQL database by using ADO. NET.

After the component is installed, reference the namespace MySQL. Data. MySQLClient;

When using command line compilation: csc/r: MySQL. Data. dll test. cs

Method 2:

Access the MySQL database through ODBC

Download two components: the ODBC driver (MySQL Connector/ODBC (MyODBC) driver) of odbc.net and MySQL. The current version is 3.51.

After the installation is complete, you can access the MySQL database through ODBC

Method 3:

Use the MySQL access component released by CoreLab for. NET

After the installation is complete, reference the namespace: CoreLab. MySQL;

Run the command: csc/r: CoreLab. MySQL. dll test. cs

Access the MySQL database instance

Compilation command: csc/r: CoreLab. MySQL. dll/r: MySQL. Data. dll test. cs

 
 
  1. using System;  
  2. using System.Net;  
  3. using System.Text;  
  4. using CoreLab.MySQL;  
  5. using System.Data.Odbc;  
  6. using MySQL.Data.MySQLClient;  
  7. class ConnectMySQL  
  8. {  
  9. public void Connect_CoreLab()  
  10. {  
  11. string constr = "User Id=root;Host=localhost;Database=qing;password=qing";  
  12. MySQLConnection mycn = new MySQLConnection(constr);  
  13. mycn.Open();  
  14. MySQLCommand mycm = new MySQLCommand("select * from shop",mycn);  
  15. MySQLDataReader msdr = mycm.ExecuteReader();  
  16. while(msdr.Read())  
  17. {  
  18. if (msdr.HasRows)  
  19. {  
  20. Console.WriteLine(msdr.GetString(0));  
  21. }  
  22. }  
  23. msdr.Close();  
  24. mycn.Close();  
  25. }  
  26. public void Connect_Odbc()  
  27. {  
  28. //string MyConString ="DSN=MySQL;UID=root;PWD=qing";   
  29. string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +   
  30. "SERVER=localhost;" +  
  31. "DATABASE=test;" +  
  32. "UID=root;" +  
  33. "PASSWORD=qing;" +  
  34. "OPTION=3";  
  35. OdbcConnection MyConn = new OdbcConnection(MyConString);  
  36. MyConn.Open();  
  37. OdbcCommand mycm = new OdbcCommand("select * from hello",MyConn);  
  38. OdbcDataReader msdr = mycm.ExecuteReader();  
  39. while(msdr.Read())  
  40. {  
  41. if (msdr.HasRows)  
  42. {  
  43. Console.WriteLine(msdr.GetString(0));  
  44. }  
  45. }  
  46. msdr.Close();  
  47. MyConn.Close();  
  48. }  
  49. public void Connect_Net()  
  50. {  
  51. string myConnectionString = "Database=test;Data Source=localhost;User Id=root;Password=qing";  
  52. MySQLConnection mycn = new MySQLConnection(myConnectionString);  
  53. mycn.Open();  
  54. MySQLCommand mycm = new MySQLCommand("select * from hello",mycn);  
  55. MySQLDataReader msdr = mycm.ExecuteReader();  
  56. while(msdr.Read())  
  57. {  
  58. if (msdr.HasRows)  
  59. {  
  60. Console.WriteLine(msdr.GetString(0));  
  61. }  
  62. }  
  63. msdr.Close();  
  64. mycn.Close();  
  65. }  
  66. public static void Main()  
  67. {  
  68. ConnectMySQL ms = new ConnectMySQL();  
  69. ms.Connect_CoreLab();  
  70. ms.Connect_Odbc();  
  71. Connect_Net();  
  72. }  
  73. }  

The above content is an introduction to. net methods and examples for connecting to the MySQL database.

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.