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>