SIGN UP MEMBER LOGIN:    
ARTICLE

MySql database connectivity with ASP.Net

Posted by Brijesh Jalan Articles | ADO.NET in C# July 27, 2010
In this we will learn how to connect MySql database with ASP.NET.
Reader Level:

HTML clipboard

Use the following reference for MySQl

using MySql.Data.MySqlClient;

1.Open the visual Studio

2.Create the new website and save it

3.Now open the Default.aspx form and drag some Labels, text boxes and button.

<asp: Label ID="Label1" runat="server" Text="Name"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br/> <br/><asp:Label ID="Label2" runat="server" Text="Address"></asp:Label>&nbsp;&nbsp;&nbsp;<asp:TextBox ID="TextBox2" runat="server"></asp: Textbox> <br /> <br /><asp:Label ID="Label3" runat="server" Text="Age"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBox ID="TextBox3" runat="server"></asp: Textbox> <br /> <br /> <br /> <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <br /> <br /> <br /> <br />

4.Now create the connection

public partial class _Default : System.Web.UI.Page

{
MySqlConnection con;
MySqlCommand cmd;
string str;
}

5.Write the Code on Page_load event.

protected void Page_Load(object sender, EventArgs e)
{
con = new MySqlConnection("Data Source=localhost;Database=brijesh;User ID=root;Password=sys123");
con.Open();
Response.Write("connect");
}

Here Local host is the server name , brijesh is the batabase name , root is the user id and password is sys123.You must ensure that you have installed MYSQL Administrator. It's a great little application that provides a GUI to help you manage your new database server. While you can get up and running using only the command line, for users who are used to using Windows applications and wince at the thought of editing configuration files by hand or using a command prompt it's almost a necessity. For the rest of this article, I'll assume you've installed MySQL Administrator and I'll be using it for illustration.

6.Now write the code on button_click event

protected void Button1_Click(object sender, EventArgs e)
{
str = "insert into emp_info1 values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
cmd = new MySqlCommand(str, con);
cmd.ExecuteNonQuery();
}

OUTPUT OF THIS APPLICATION:

untitled.jpg
The name , address and age automatically added in MySQl database server and if you want you check it using MySql command line.

output.jpg

Now the name ELISHA is inserted Successfully.

Login to add your contents and source code to this article
share this article :
post comment
 

I am new to C# and mysql database connectivity. What I want to do is ....to connect my c# window based application to the mysql database which is online. Please send the solution to my id pushpendra7974@gmail.com thanks in advance..

Posted by pushpendra singh Feb 03, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor