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.

1 comment: