Asp.net connection to mysql database

Source: Internet
Author: User
Tags mysql tutorial

The following provides a connection code for the mysql tutorial database tutorial. Now we use the asp tutorial. net C # to connect to the mysql database and display data. The code is implemented as follows.

<% @ Page Language = "C #" %>
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. Odbc" %>
<Script runat = "server">

Void Page_Load (object sender, EventArgs e ){

// TODO: Update the ConnectionString and CommandText values for your application
String ConnectionString = @ "driver = {MySQL ODBC 3.51 Driver}; server = localhost; database = books; uid = band; pwd = letmein ;";
String CommandText = "select * from Publisher ";

OdbcConnection myConnection = new OdbcConnection (ConnectionString );
OdbcCommand myCommand = new OdbcCommand (CommandText, myConnection );

MyConnection. Open ();

DataGrid1.DataSource = myCommand. ExecuteReader (CommandBehavior. CloseConnection );
DataGrid1.DataBind ();

// Close the connection
MyConnection. Close ();
}

</Script>
<Html>
<Head>
</Head>
<Body style = "FONT-FAMILY: arial">
<H2> Simple Data Report
</H2>
<Hr size = "1"/>
<Form runat = "server">
<Asp: datagrid id = "DataGrid1" runat = "server" CellSpacing = "1" GridLines = "None" CellPadding = "3" BackColor = "White" ForeColor = "Black" EnableViewState = "False ">
<HeaderStyle font-bold = "True" forecolor = "white" backcolor = "# 4A3C8C"> </HeaderStyle>
<ItemStyle backcolor = "# DEDFDE"> </ItemStyle>
</Asp: datagrid>
</Form>
</Body>
</Html>

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.