An often overlooked combination of scripting technologies is ASP.NET with MySQL (instead of Access [slow] or SQL Server [expensive]). MySQL is a fast and popular database and you can find numerous ISPs which offer it with ASP.NET (e.g. www.discountasp.net).
If you are installing it on your own server, you need to download MySQL and install it, then run c:\mysql\bin\winmysqladmin.exe
. You also need to download the ODBC driver (choose Windows Downloads Driver Installer) and install it. Then use this code to access your MySQL database.
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<%@ Page Language="C#" %>
<script runat="server">
public void Page_Load(Object sender, EventArgs e) {
DataTable dtRecords = GetDataTable("SELECT * FROM newone");
foreach(DataRow dr in dtRecords.Rows) {
Response.Write(dr["FirstName"].ToString() + " " + dr["LastName"].ToString() + "<br/>");
}
}
private static string GetConnection() {
return "DRIVER={MySQL ODBC 3.51 Driver};Server=localhost;Database=testdatabase";
}
public static DataTable GetDataTable(string sql) {
DataTable rt = new DataTable();
DataSet ds = new DataSet();
OdbcDataAdapter da = new OdbcDataAdapter();
OdbcConnection con = new OdbcConnection(GetConnection());
OdbcCommand cmd = new OdbcCommand(sql, con);
da.SelectCommand = cmd;
da.Fill(ds);
try {
rt = ds.Tables[0];
}
catch {
rt = null;
}
return rt;
}
</script>
Comments