Reply to topic
Sample ASP.NET page for connecting to mySQL
brantonit


Joined: 30 Apr 2009
Posts: 1
Reply with quote
I have setup a mySQL database under my account but can't figure out how to make a connection with ASP.NET. I'm very new to ASP.NET and couldn't find a good example of how to make it work with Hostmysite.com.

Could someone post a simple page with instructions on how to make a connection to mySQL and display the records? Also, any best practice tips would be great.

Thanks,
rmathus


Joined: 09 Mar 2007
Posts: 44
Location: Baltimore, MD
Reply with quote
This article should help out:

http://www.15seconds.com/issue/050407.htm

<%@ 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=SERVERNAME; user id=USERNAME; password=PASSWORD; database=DATABASENAME; pooling=false;")

strSQL = "SELECT * FROM mytable;"

myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
myDataSet = New Dataset()
myDataAdapter.Fill(myDataSet, "mytable")

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>

Let us know if you need further assistance.
whitesites
Forum Regular

Joined: 05 Jul 2004
Posts: 310
Location: Houston, TX
Reply with quote
If you are using dreamweaver or something other than Visual Studio to build your websites. I would recommend making a code behind. Something like MyCodeBehind.cs

Then add this reference to your asp.net pages, to get access to all the functions in your code behind.

The easiest way I have found of using MySQL, is to build a bunch of helper functions in a code behind. One function may retrive a datatable, Another one may retrieve a single value. This will save you alot of time, because they you can just pass your Query through a helper function instead of having to write out all your MySQL connection stuff for every function
Sample ASP.NET page for connecting to mySQL
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic