How do I query MySQL database in ASP.NET?


In order to query your mySQL Database with ASP.NET script, you need to organize an ODBC connection with us first. The following the snippet of the code:

<%@ Page Language="VB" %>
<%@ import Namespace="System.Data.Odbc" %>

<script runat="server">

Public Sub Page_Load()
     Dim mySelectQuery As String = "SELECT * FROM tblTest"
     Dim myConnection As New OdbcConnection("DSN=<username>_mysqlConn")

     Dim myCommand As New OdbcCommand(mySelectQuery, myConnection)
     myConnection.Open()
     Dim myReader As OdbcDataReader = myCommand.ExecuteReader()
     Try 
          While myReader.Read()
          Response.Write (myReader("name") & "<br>")
     End While
     Finally
          //always call Close when done reading.
          myReader.Close()
          //always call Close when done with connection.
     myConnection.Close()
     End Try
End Sub

</script>



Article ID: 84
Created On: Tue, May 1, 2012 at 11:15 PM
Last Updated On: Tue, May 1, 2012 at 11:15 PM
Authored by: ASPHostServer Administrator [asphostserver@gmail.com]

Online URL: http://faq.asphosthelpdesk.com/article.php?id=84