Database Connectivity of VB.NET with MYSQL
In this article we will learn how to create Database connectivity with MySql.
Use the following reference library for MySQl
Imports 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> <asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox><br /><br /><asp:Label ID="Label2" runat="server" Text="Address"></asp:Label> <asp:TextBox ID="TextBox2" runat="server">
</asp:TextBox><br /><br /><asp:Label ID="Label3" runat="server" Text="Age"></asp:Label> <asp:TextBox ID="TextBox3" runat="server">
</asp:TextBox><br /><br /> <asp:Button ID="Button1" runat="server" Text="Button" />
4.Now create the connection Use the following code in Default.aspx.vb page.
Partial Class _Default
Inherits System.Web.UI.Page
Dim con As MySqlConnection
Dim cmd As MySqlCommand
Dim str As String
5.Write the Code on Page_load event.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
con = New MySqlConnection("Data Source=localhost;Database=brijesh;User ID=root;Password=sys123")
con.Open()
Here Local host is the server name , brijesh is the database 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 Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
con = New MySqlConnection("Data Source=localhost;Database=brijesh;User ID=root;Password=sys123")
con.Open()
str = (("insert into emp_info1 values ('" + TextBox1.Text & "','") + TextBox2.Text & "','") + TextBox3.Text & "')"
cmd = New MySqlCommand(str, con)
cmd.ExecuteNonQuery()
OUTPUT OF THIS APPLICATION
the name , address and age automatically added in MySQl database server and if you want you check it using MySql command line.