How to use MySql database in. net Environment

Source: Internet
Author: User
I have read an article about Asp.net and mysql on the Internet. For details about how to install mysql and connect to the MySQL database in. NET, see ASP. NET + MySQL simple illustration getting started.

Here I use EMS Mysql Manager lite tool to manage mysql5.0
:

Here, my database and table have been created. Now open vs. net2003 and create a project named mysqltest.

Now we have installed MySQL Connector/Net

For specific installation method, please see ASP. NET + MySQL simple illustration entry "http://www.yesky.com/431/1944431.shtml

If it is installed by default, you can go to C: \ Program Files \ MySQL Connector Net 1.0.4 \ bin \. NET 1.1 \ find MySql. data. dll, and then copy it to the bin directory under the mysqltest directory

Right-click the data in the toolbox and choose Add/Remove from the shortcut menu.

For example


Select Browse. Under the bin directory of the mysqltest project, select MySql. Data. dll

Reference MySql. Data. dll in the bin directory of the mysqltest project in solution Manager

Add the DataGrid Control to the aspx page

Here is the C # code

Using System. Data;

Using System. Drawing;

Using System. Web;

Using System. Web. SessionState;

Using System. Web. UI;

Using System. Web. UI. WebControls;

Using System. Web. UI. HtmlControls;

Using MySql. Data. MySqlClient;

Namespace mysqltest

{

/// <Summary>

/// Summary of WebForm1.

/// </Summary>

Public class WebForm1: System. Web. UI. Page

{

Protected System. Web. UI. WebControls. DataGrid DataGrid1;

Private void Page_Load (object sender, System. EventArgs e)

{

// Place user code here to initialize the page

MySqlConnection MyConn = new MySqlConnection ("Server = localhost; userid = root; password =; Database = guestbook ");

String SQL = "select * from guestbook ";

MySqlDataAdapter Myda = new MySqlDataAdapter (SQL, MyConn );

DataSet ds = new DataSet ();

Myda. Fill (ds, "guestbook ");

DataGrid1.DataSource = ds. Tables ["guestbook"]. DefaultView;

DataGrid1.DataBind ();

}

# Code generated by region Web Form Designer

Override protected void OnInit (EventArgs e)

{

//

// CODEGEN: This call is required by the ASP. NET Web form designer.

//

InitializeComponent ();

Base. OnInit (e );

}

/// <Summary>

/// The designer supports the required methods-do not use the code editor to modify

/// Content of this method.

/// </Summary>

Private void InitializeComponent ()

{

This. Load + = new System. EventHandler (this. Page_Load );

}

# Endregion

}

}

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.