MySql database connectivity with ASP.Net

HTML clipboard

Use the following reference for MySQL
  1. 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.
  1. <asp: Label ID="Label1" runat="server" Text="Name">  
  2.      </asp:Label>  
  3.      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <br />  
  4.      <asp:Label ID="Label2" runat="server" Text="Address"></asp:Label>  
  5.      <asp:TextBox ID="TextBox2" runat="server">  
  6. </asp: Textbox> <br /> <br />  
  7. <asp:Label ID="Label3" runat="server" Text="Age"></asp:Label>  
  8. <asp:TextBox ID="TextBox3" runat="server">  
  9.      </asp: Textbox> <br /> <br /> <br /> <br />  
  10.      <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <br /> <br /> <br /> <br /> 
    4.Now create the connection
    1. public partial class _Default: System.Web.UI.Page    
    2. {    
    3.    MySqlConnection con;    
    4.    MySqlCommand cmd;    
    5.    string str;    
    6. }   
    5.Write the Code on Page_load event.
    1. protected void Page_Load(object sender, EventArgs e) {  
    2.     con = new MySqlConnection("Data Source=localhost;Database=brijesh;User ID=root;Password=sys123");  
    3.     con.Open();  
    4.     Response.Write("connect");  
    Here Localhost is the server name, brijesh is the database name, the 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
    1. protected void Button1_Click(object sender, EventArgs e) {  
    2.     str = "insert into emp_info1 values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";  
    3.     cmd = new MySqlCommand(str, con);  
    4.     cmd.ExecuteNonQuery();  
    OUTPUT OF THIS APPLICATION:
     
    untitled.jpg
     
    The name, address, and age automatically added to the MySQL database server, and if you want you to check it using the MySql command line.
     
    output.jpg
     
    Now the name ELISHA is inserted Successfully.


    Similar Articles