CsharpGears Framework : Using Different SQL Providers

Hello, I am about to present you how easily can you connect different types of databases with the C# Gears Framework. First of all, note that the IDatabase interface implementators do not care to what type of database they are connected to, they only care about the SQL query they need to execute. For example, here is how would you use C# Gears Framework in order to connect MySQL Database with .NET:
Connecting MySQL with C#
Step 1:Download and install the MySQL provider for .NET. Simply go to MySQL's download site and create an account if you already don't have one. The installation is pretty straightforward - there is a MSI installer.
Step 2:Add the MySQL.data.dll to your project. Ordinarily create new project in Visual Studio, go to References, right click on it and choose Add Reference. Then navigate to the path where the installation of the MySQL provider resides, and choose the mentioned dll.
Step 3: Start using the MySQL data provider. Now I will paste the whole code from my Default.aspx.cs file:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using CsharpGears.DataBase;
using System.Data;
namespace MySQLTest
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            DataBaseEntity dbEntity = new DataBaseEntity("Select * FROM `product`",
CommandType.Text, 
"server=localhost;database=testdb;uid=root;password=", "MySql.Data.MySqlClient");
            DataTable dt = (DataTable)dbEntity.Select();
            foreach (DataRow dr in dt.Rows) 
            {
                Response.Write(dr["ID"].ToString() + ": " + dr["Description"].ToString() + 
" - " + dr["Date"].ToString());
            }
        }
    }
}
And that is it! You see, there is no difference in the usage of the DataBaseEntity object, you only use a different constructor which takes the connection string and the data provider as its parameters. The rest of the parameters are explained in the previous posts - it's just an ordinary ad-hoc SQL query call. I hope that the C# Gears Framework is helpful to you, as it saves a lot of time that would be otherwise spent on writing tedious code.
One last note before I go, this article assumes that you already know how to create MySQL database and it is not intended to be MySQL.NET tutorial. The author is sure you can find plenty of sources throughout the Internet on how to create MySQL database. Personally, i prefer the phpMyAdmin application for managing MySQL databases. PhpMyAdmin comes with the installation of WAMP (Windows-Apache-MySQL-PHP). You should also keep in mind that you need to start Apache in order to access the database from ASP.NET

No comments:

Post a Comment