VB.Net : how can you save Record to MySQL-Table

Create  your database to MySQL

codetpoint

 


Table (book) Structure

dd2

 


Interface design of the form

dd3


VB.Net Code

Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class addBookfrm
    Private Sub addBookfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Focus()
    End Sub
    Private Function SaveData(ByVal code As String, ByVal bname As String, ByVal bauthor As String,
                              ByVal bpub As String, ByVal bqty As String) As Boolean
        Dim flag As Boolean = False
        Dim cnn As New MySqlConnection
        Dim cmd As New MySqlCommand
        cnn.ConnectionString = "Server=127.0.0.1;Database=library;Uid='root';Pwd='';"
        cmd.CommandText = "insert  into book(bcode,bname,bauthor,bpublisher,bquantity) values (@bcode,@bname,@bauthor,@bpublisher,@bquantity)"
        cmd.Parameters.AddWithValue("@bcode", code)
        cmd.Parameters.AddWithValue("@bname", bname)
        cmd.Parameters.AddWithValue("@bauthor", bauthor)
        cmd.Parameters.AddWithValue("@bpublisher", bpub)
        cmd.Parameters.AddWithValue("@bquantity", bqty)
        cmd.Connection = cnn
        cnn.Open()
        Dim rows As Integer
        rows = cmd.ExecuteNonQuery()
        If (rows <> 0) Then
            flag = True
        Else
            flag = False
        End If
        cnn.Close()
        Return flag
    End Function
    Private Function IsEmpty() As Boolean
        Dim flag As Boolean = False
        If (String.IsNullOrEmpty(TextBox1.Text) Or String.IsNullOrEmpty(TextBox2.Text) Or
            String.IsNullOrEmpty(TextBox3.Text) Or String.IsNullOrEmpty(TextBox4.Text) Or
            String.IsNullOrEmpty(TextBox5.Text)) Then
            flag = True
        End If
        Return flag
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (IsEmpty()) Then
            MessageBox.Show(" Some  fields has no data!")
            Exit Sub
        Else
            If (SaveData(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text)) Then
                MessageBox.Show(" Yahoo!  Data Saved Sucessfully")
                allClear()
            Else
                MessageBox.Show(" Oops!  Data  Not Saved ")
            End If

        End If

    End Sub
    Private Sub allClear()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox1.Focus()
    End Sub

    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class

Check again your table in PHPmyAdmin

dd4

 

Find out .Net/MySQL connector..

http://dev.mysql.com/downloads/connector/net/6.4.html