Saturday, March 17, 2012

Database Programming with ASP.NET C# & MySql

Today I find some time to discuss Database Programming with ASP.NET C# & MySql

A BRIEF INTRODUCTION ON ASP.NET C#, MYSQL AND WINDOWS STACK

Software Requirements
  1. MySQL DB with Work Bench
    DOWNLOAD LINK: http://www.mysql.com/downloads/
  2. MySql Connector for .NET
    DOWNLOAD LINK: http://www.mysql.com/downloads/connector/net/
    Download the binary and install to your machine where you need to run your application.
  3. .NET Platform / Visual Studio
    DOWNLOAD LINK: http://msdn.microsoft.com/hi-in/netframework/default.aspx
    You may download and install the .NET framework of your required version 1.1/2/3.5 or 4.
    This article supports all the above mentioned versions.
MySql Database Creation and Table Creation
I assume that you can create MySql Database and Tables I proceed to the C# Programming part

Web.config File
The following is a simple connection string statement I use in my development practice. You may also ignore Web.config and write the connection string inline on the code page. But it is not recommended as it is vulnerable to disclose your database credentials (Database Username and Password) in the code page. Say you may have more than one developers working on the project and you many not want to share the database credentials with them, this practice will help you the most. Apart from this the content in the web.config file is more secured than content in other pages by the ASP.NET architecture.

Syntax:

<add  name="<give some name to call this connection string>"
              connectionstring="server=<server ip address or name>
              initial catalog=<database name>
              uid=<database username="">;
              pwd=<database password>"
              providerName="MySql.Data.MySqlClient"/>

Example:

<add  name="MyConnection1"
              connectionstring="server=localhost;
              initial catalog=mysampledb; 
              uid=root;
              pwd=mypassword" 
              providerName="MySql.Data.MySqlClient"/>

ASP.NET C#
Here I use ASP.NET and C# to explain the database programming with MySql. I assume that you have basic knowledge of ASP.NET and C# programming.

Go to the code behind and proceed with the following code.

  1. Incluse the usings.

    using MySql.Data.MySqlClient;

  2. Declare the private variables inside the main class (pls not the class not the main function)

    //CREATING CONNECTION INSTANCE / NOTE: CONNECTION WILL NOT OPEN HERE
    MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings[
                          "myconnstr"].ConnectionString);

    //CREATING INSTANCE OF COMMAND AND DATA READER / REQUIRED FOR ACCESSING DATA MANUALLY

    MySqlCommand cmd;
    MySqlDataReader dr;
    //CREATING INSTANCE OF DATA SET / REQUIRED FOR BINDING DATA WITH DATAGRID/DATALIST
    DataSet ds = new DataSet();
  3. Opening Database Connection

    if (con.State == ConnectionState.Closed)
    {
      con.Open();
    }
  4. Adding Data to the Database

    string qry = "INSERT INTO sample_table(first_name, last_name)";
    cmd = new MySqlCommand(qry, con);
                cmd.ExecuteNonQuery();

Introduction to C# / ASP.NET with C#

ASP.NET is a Microsoft technology for web applications. ASP stands for Active Server Pages. As the name specifies the pages are programs that are stored and executed on the server. When a call is made to the page the page gets compiled (if not already compiled) and returned to the client browser as a file in HTML and JavaScript combination. The .NET platform is a proven Microsoft technology for applications. ASP.NET is limited to Windows platform. We will discuss more about the ASP.NET and C# in a separate article soon.

Introduction to MySql Database
MySql is an opensource database backed by Oracle Corporation. It is a powerful and efficient database that you can rely on it for your enterprise needs. Most companies prefer MySql for their data storage needs and high transaction processing. MySql is reliable and programmer friendly too. It has a large user base and strong community to support. However, the PHP & MySql combination is the best pair on internet! It is not that C# doesn't go well with MySql but the opensource PHP beat the C# or any other language on Internet. Let us not worry about that! We have enough articles and resources for C# integration and programming with MySql and abundant help content for MySql. We will discuss more about the MySql in a separate article soon.

3 comments:

  1. Should be ConnectionState.Closed
    and string qry =

    ReplyDelete
    Replies
    1. Thanks Nexus. I have updated the post with your corrections!

      Delete
  2. Good !! thanks it is very helpful.......

    ReplyDelete

Share your comments