MySql Database In ASP.NET Using C# With ODBC Connector

ODBC stands for Open Database connectivity and using ODBC, we can connect any database. We all know that MySql database is an open source, so many companies prefer MySQL database for their uses. First of all, we need MySql database in our system. We can click here to download MySQL database. While installing MySQL database, it will prompt for the password, we can give any password but in this example, I am taking “password” as a password.

Now, we can use the command prompt to create the database and table operations, but generally people prefer GUI tools for this, as SQLyog, Workbench, etc. In this article, we are going use SQLyog to create the database and table operations. We can click here to download SQLyog. Now, we have to install ODBC connector for connecting MySQL database in C#, using ODBC. We can click here to download ODBC connector.

First, I am going to create the database for our sample example, as given below-

code

We can also create the database by right clicking on the root menu, which is on the left top side of SQLyog community and create the database. Afterwards, we can either write SQL query to create the table or we can click on the newly created database sample - > right click on the table, as given below-

table

We are creating Employee table for our sample database, as given below-

database

Here, we are giving table name as Employee. After giving column name, data type and length, click the save button.

After creating a table, we are going to insert some record into the employee table, as given below-

  1. INSERT INTO `sample`.`employee`(`id`,`name`,`department`)VALUES('1','vivek','IT');  
  2. INSERT INTO `sample`.`employee`(`id`,`name`,`department`)VALUES('2','ranjeet','IT');  
  3. INSERT INTO `sample`.`employee`(`id`,`name`,`department`)VALUES('3','sharath','IT');  
code

Now, start Visual Studio and choose new project as File -> New -> Project.

Project

We are giving our project name as Sample and click OK.

Now, select a template as Empty and Click OK, as given below-

Empty

Now, right click on the sample project from Solution Explorer and choose Add -> Web Form, as given below-

Web Form

Enter item name as default in the new Window screen and click OK, as given below-

default

Now, open Web.config page from the Solution Explorer and write the connection strings inside the configuration, as given below-
  1. <connectionStrings>  
  2.    <add name="conn" connectionString="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=sample;uid=root;pwd=password"/>  
  3. </connectionStrings>  
It is the best practice to put your connection string in Web.config in .NET project.

In the code, given above-
  • Driver is the version of MySQL ODBC, which is 5.1 in our case.
  • Server is the server’s address, which is localhost in our case.
  • Database is the database name, which is sample in our case
  • uid is the username of MySQL Server, which is root in our case.
  • Pwd is the password of MySql server, which is password in our case.

Now, click Default.aspx page and write the code for the grid view, as given below-

  1. <asp:GridView ID="gvEmployee" runat="server"></asp:GridView>  
Now, go to the Default.aspx.cs page and import some namespaces, as given below-
  1. using System.Data;  
  2. using System.Data.Odbc;  
  3. using System.Web.Configuration;  
Write the code, given below, to get all the employee data from the MySQL Server database and assign to the grid view in Page_Load method, as given below-
  1. string connString = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString;  
  2. using(OdbcConnection con = new OdbcConnection(connString))  
  3. {  
  4.     OdbcCommand cmd = new OdbcCommand("select * from Employee", con);  
  5.     OdbcDataAdapter da = new OdbcDataAdapter(cmd);  
  6.     DataSet ds = new DataSet();  
  7.     da.Fill(ds);  
  8.     gvEmployee.DataSource = ds;  
  9.     gvEmployee.DataBind();  
  10. }  
Now, run the project and see the result, as given below-

project
Working with the stored Procedure

Generally, we prefer the stored procedure for any CRUD operations. Below is the sample code for using the stored procedure.
  1. OdbcCommand cmd = new OdbcCommand("{CALL StoredProcedureName(?,?)}", con);   
  2. cmd.CommandType = CommandType.StoredProcedure;  
  3. cmd.Parameters.AddWithValue("ParameterName1", ParameterValue1);  
  4. cmd.Parameters.AddWithValue("ParameterName2", ParameterValue2);  
  5. cmd.ExecuteNonQuery();  
Where “?” is used for the number of parameters in the stored procedure.

In above example, we are taking two parameters as ParameterValue1 and ParameterValue2 due to which we are taking two question marks(?,?).

Quick view

Install MySQL database, click here to download MySQL database.
Install SQLyog, click here to download the SQLyog.
Install ODBC, click here to download the ODBC connector.
Use System.Data.Odbc namespace and start programming. 


Similar Articles