Monday, February 13, 2012

Insert data in MySql database using ASP.Net


Step 1) Download mysql connector for .net framework 2.0 from mysql website.

Step 2)Open visual studio editor and add reference the following dll file that may be located following path
 C:\Program Files\MySQL\MySQL Connector Net 5.1.7\Binaries\.NET 2.0\ MySql.Data.dll

Step 3)Now write the following code in code behind in your aspx page

using System;
using System.Data;
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;
using MySql.Data.MySqlClient;


public partial class mysql : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

string cnnString ="Server=localhost;Port=3306;Database=cdcol;Uid=root;Pwd=";
      
       MySqlConnection cnx = new MySqlConnection(cnnString);
       MySqlCommand cmd=cnx.CreateCommand();
      
       cnx.Open();
       cmd.CommandText = "insert into cds(titel,interpret,jahr,id) values('sourav','kayal',1,2)";
       cmd.ExecuteReader();
               
    }
}

N.B:- Don't forger to modify servername,database name and table name.

2 comments:

  1. I was searching for the best Dot Net materials and I came across this Blog. Honestly speaking this blog is very informative and treasure for the Dot Net Developers and many more. I will help others too the way you helped me :).

    ReplyDelete