Just incase some of you aren’t a big fan of MS SQL, or you have MySQL at your disposal and want to use it with your .NET Applications:
In this scenario, I’m using an ancient build of MySQL 4.0.
Download MyODBC 3.51
Setting up MyODBC
- Run the MSI file
- Once complete go to Start>Control Panel>Administrative Tools>Data Sources (ODBC)
- Click Add
- Choose MySQL ODBC 3.51 Driver
- Now you may setup various information, such as host, user, pass, database, etc.
Connection Specifics
- Datasource name - what you will call the connection
- Description - description of connection (not necessarily needed)
- Server - localhost
- username - username of mysql
- password - password of mysql
- database - select the database you want to use with this connection
From there you may test your connection to see if everything works.
C# code for connecting and having fun.
What we will want to do first is call our system namespaces that will be required (Data, Data.Odbc, Data.SqlClient). However, you may call a few more references for other reasons, such as I have in the example:
using System;
using System.Data;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
From there we will want to create a method for the data retrieval. We will call this Build_Record().
public void Build_Record()
{
//Setup our connection string referring to our ODBC connection
OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings["MYSQL"].ConnectionString);
//Format our SQL query
string strSQL = "SELECT name, type, date, credit, debit FROM monthly";
//create a command variable
OdbcCommand com = new OdbcCommand(strSQL, con);
//try the connection and populate the gridview; else fail
try
{
//open connection
con.Open();
//execute the query
com.ExecuteNonQuery();
//alert user (optional)
lblrecordadded.Text = "Success";
//source of the data for the gridview
ITGridView1.DataSource = com.ExecuteReader();
//bind the data
ITGridView1.DataBind();
//close the connection
con.Close();
con.Dispose();
}
catch (Exception e)
{
lblrecordadded.Text = "Error Occured " + e.Message.ToString();
lblrecordadded.Visible = true;
return;
}
con.Close();
}
And then we will move on to call our method in the Page_Load method:
using System;
using System.Data;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Execute our method upon page load
Build_Record();
}
And a view at the Full C# Code
using System;
using System.Data;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Execute our method upon page load
Build_Record();
}
public void Build_Record()
{
//Setup our connection string referring to our ODBC connection
OdbcConnection con = new OdbcConnection(ConfigurationManager.ConnectionStrings["MYSQL"].ConnectionString);
//Format our SQL query
string strSQL = "SELECT first, last, date FROM tablename";
//create a command variable
OdbcCommand com = new OdbcCommand(strSQL, con);
//try the connection and populate the gridview; else fail
try
{
//open connection
con.Open();
//execute the query
com.ExecuteNonQuery();
//alert user (optional)
lblrecordadded.Text = "Success";
//source of the data for the gridview
MyGridView.DataSource = com.ExecuteReader();
//bind the data
MyGridView.DataBind();
//close the connection
con.Close();
con.Dispose();
}
catch (Exception e)
{
lblrecordadded.Text = "Error Occured " + e.Message.ToString();
lblrecordadded.Visible = true;
return;
}
con.Close();
}
}
Now, the Gridview could look like this:
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="first" HeaderText="First"/>
<asp:BoundField DataField="last" HeaderText="Last" />
<asp:BoundField DataField="date" HeaderText="Date" />
</Columns>
</asp:GridView>