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?
- 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 construction
- Public MySqlClass ()
- {
- Try
- {
- Conn = new MySqlConnection (ConfigurationManager. etettings ["mysqlconn"]);
- Conn. Open ();
- Com = new MySqlCommand ();
- Com. Connection = conn;
- }
- Catch (Exception ee)
- {
- Throw new Exception ("database connection error ");
- }
- }
- # Endregion
- # Region obtaining 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
- }
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?
- <Asp: GridView runat = "server" ID = "GirdView1"> </asp: GridView>
Write the following code on the aspx. cs page of aspx:
View plaincopy to clipboardprint?
- 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 ();
- }
- }
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