<%@ Page language="c#" %>
<%@ Import Namespace="MySql.Data.MySqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html><head>
<title>MySQL <--> ASP.NET example</title></head>
<body>
<script Runat="Server" language="c#">
protected System.Web.UI.WebControls.LabellblList;
void Page_Load(object sender, System.EventArgs e) {
string connStr = "server=localhost; database=sitepoint";
MySqlConnectionconn = null;
MySqlDataAdapterda = null;
MySqlDataReader reader = null;
MySqlCommandcmd = null;
try {
conn = new MySqlConnection(connStr);
conn.Open();
cmd = new MySqlCommand("SHOW DATABASES", conn);
reader = cmd.ExecuteReader();
while (reader.Read()) {
lblList.Text = lblList.Text + reader.GetString(0) + "<br>";
} } catch (MySqlException ex) {
lblList.Text = "Exception accessing MySQL server: " + ex.Message;
} catch (Exception ex) {
lblList.Text = "Exception accessing database list: " + ex.Message;
} finally {
if (reader != null) reader.Close();
if (conn != null) conn.Close();
} }
</script>
<form id="frmExample" method="post">
<h1>Connecting to MySQL from C#</h1>
<asp:Label id="lblTitle">Databases</asp:Label>
<br /><br />
<asp:Label id="lblList" runat="server" />
</form></body></html>