ARTICLE

Database Connectivity of VB.NET with MYSQL

Posted by Brijesh Jalan Articles | Visual Basic 2010 July 29, 2010
In this article we will learn how to create Database connectivity with MySql.
Reader Level:

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>&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;<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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

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
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor