A simple ASP. NET Access to MySql database favorites

Source: Internet
Author: User
A simple ASP. NET access MySql database Favorites

Download ADO. NET Driver for MySQLMySql. Data. dll

After installation, find the Assemblies folder under the installation directory and find MySql. Data. dll


(This file is a. Net driver for accessing the MySQL database. It is in full ADO. NET data access mode and is officially provided by MySQL. Multiple versions are available .)

A link to learning: http://www.cnblogs.com/wcfgroup/articles/1242256.html

Create an asp.net website and add reference MySql. Data. dll to the website.

Add:

<Add key = "mysqlconn" value = "server = 192.168.1.102; database = database name; uid = user name; pwd = password;"> </add>

This user name and password are set during MySql installation.

Create a class file to access the MySql database: MySqlClass. cs

The file code is as follows:

View plaincopy to clipboardprint?

  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Web;
  4. Using MySql. Data. Types;
  5. Using MySql. Data. MySqlClient;
  6. Using System. Configuration;
  7. Using System. Data;
  8. /// <Summary>
  9. /// Summary of MySqlClass
  10. /// </Summary>
  11. Public class MySqlClass
  12. {
  13. Private MySqlConnection conn;
  14. Private MySqlCommand com;
  15. # Region construction
  16. Public MySqlClass ()
  17. {
  18. Try
  19. {
  20. Conn = new MySqlConnection (ConfigurationManager. etettings ["mysqlconn"]);
  21. Conn. Open ();
  22. Com = new MySqlCommand ();
  23. Com. Connection = conn;
  24. }
  25. Catch (Exception ee)
  26. {
  27. Throw new Exception ("database connection error ");
  28. }
  29. }
  30. # Endregion
  31. # Region obtaining DataSet
  32. Public DataSet GetDataSet (string sqlString)
  33. {
  34. DataSet ds = new DataSet ();
  35. Try
  36. {
  37. MySqlDataAdapter da = new MySqlDataAdapter (sqlString, conn );
  38. Da. Fill (ds );
  39. }
  40. Catch (Exception ee)
  41. {
  42. Throw new Exception ("SQL:" + sqlString + "<br/>" + ee. Message. ToString ());
  43. }
  44. Return ds;
  45. }
  46. # Endregion
  47. }

Using System; using System. collections. generic; using System. web; using MySql. data. types; using MySql. data. mySqlClient; using System. configuration; using System. data; // <summary> /// summary of MySqlClass /// </summary> public class MySqlClass {private MySqlConnection conn; private MySqlCommand com; # region construct public MySqlClass () {try {conn = new MySqlConnection (ConfigurationManager. appSettings ["mysqlconn"]); conn. open (); com = new MySqlCommand (); com. connection = conn;} catch (Exception ee) {throw new Exception ("database Connection error") ;}# endregion # region gets DataSet public DataSet GetDataSet (string sqlString) {DataSet ds = new DataSet (); try {MySqlDataAdapter da = new MySqlDataAdapter (sqlString, conn); da. fill (ds);} catch (Exception ee) {throw new Exception ("SQL:" + sqlString + "<br/>" + ee. message. toString ();} return ds ;}# endregion}

2. Create An aspx page and pull a Girdview control on the page,

View plaincopy to clipboardprint?

  1. <Asp: GridView runat = "server" ID = "GirdView1"> </asp: GridView>


Write the following code on the aspx. cs page of aspx:

View plaincopy to clipboardprint?

  1. Protected void Page_Load (object sender, EventArgs e)
  2. {
  3. If (! IsPostBack)
  4. {
  5. MySqlClass mysqlClass = new MySqlClass ();
  6. GirdView1. DataSource = mysqlClass. GetDataSet ("SELECT * FROM admin"). Tables [0]. DefaultView;
  7. GirdView1.DataBind ();
  8. }
  9. }

Protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {MySqlClass mysqlClass = new MySqlClass (); GirdView1. DataSource = mysqlClass. GetDataSet ("SELECT * FROM admin"). Tables [0]. DefaultView; GirdView1.DataBind ();}}


This is a simple example of ASP. NET accessing the MySql database.

Of course, you can also insert data, modify data, and perform other operations on the MySql database!

I will not write it here

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.