Connecting to a MySQL database from an ASPX page
If you want to connect to a MySQL database from an ASPX page, the text below may be useful for first making a simple test connection before starting your own project.
As an example, the .NET Connector 6.0 has been installed. You need to place the file Mysql.Data.dll in your root directory (httpdocs/bin). Otherwise you will get this message when testing: Compiler Error Message: BC30002: Type 'MySqlConnection' is not defined.
Make sure that your web.config contains at least these settings, otherwise no error messages will be shown:
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
You can download the file Mysql.Data.dll from the link below. Choose download Windows Binaries, no installer (ZIP), copy the file out of it manually and place it in the bin directory mentioned earlier:
http://dev.mysql.com/downloads/connector/net
Next, create the file test_mysql.aspx in httpdocs with this content (replace test64user with your own database user, and also change the password and database in the string to your own values):
<%@ Page Language="VB" debug="true" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "MySql.Data.MySqlClient" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim myConnection As MySqlConnection
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As DataSet
Dim strSQL As String
Dim iRecordCount As Integer
myConnection = New MySqlConnection("server=localhost; user id=test64user; password=testing; database=webawere_test64db; pooling=false;")
strSQL = "SELECT * FROM testtabel;"
myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
myDataSet = New Dataset()
myDataAdapter.Fill(myDataSet, "testtabel")
MySQLDataGrid.DataSource = myDataSet
MySQLDataGrid.DataBind()
End Sub
</script>
<html>
<head>
<title>Simple MySQL Database Query</title>
</head>
<body>
<form runat="server">
<asp:DataGrid id="MySQLDataGrid" runat="server" />
</form>
</body>
</html>
As a test, we created a small MySQL database containing 2 records:
CREATE TABLE `webawere_test64db`.`testtabel` (
`id` SMALLINT NOT NULL AUTO_INCREMENT ,
`veld1` VARCHAR( 30 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = InnoDB
INSERT INTO `webawere_test64db`.`testtabel` (
`id` ,
`veld1`
)
VALUES (
NULL , 'Waarde1'
), (
NULL , 'Waarde2'
);
