VB.Net And MySQL Search

How To Search Data In MySQL Database Using VbNet

vb.net search in database

In This VB.Net Tutorial  We Will See How To Get The ID From TextBox And Search In Database Data With This Specific ID And Display The Other Information From MySQL DataBase Table Using 2 Way (MySqlDataReader, MySqlDataAdapter + DataTable) In Visual Basic.Net  Programming Language And Visual Studio Editor.


Project Source Code:

Imports MySql.Data.MySqlClient

Public Class Search_Data_In_MySQL

    Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=s_t_d")

    Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click

        ' using dataReader 

        'Dim command As New MySqlCommand("SELECT * FROM `student` WHERE `Id` = @id", connection)

        'command.Parameters.Add("@id", MySqlDbType.Int64).Value = TextBox1.Text

        'Dim reader As MySqlDataReader

        'connection.Open()

        'reader = command.ExecuteReader()

        'If reader.Read() Then

        '    TextBox2.Text = reader(1)
        '    TextBox3.Text = reader(2)
        '    DateTimePicker1.Value = reader(3)

        'End If

        'connection.Close()

        '---------------------------------------

        ' using dataTable 

        Dim command As New MySqlCommand("SELECT * FROM `student` WHERE `Id` = @id", connection)

        Command.Parameters.Add("@id", MySqlDbType.Int64).Value = TextBox1.Text

        Dim adapter As New MySqlDataAdapter(command)

        Dim table As New DataTable()

        Try

            adapter.Fill(table)

            If table.Rows.Count > 0 Then

                'TextBox1.Text = table(0)(0)
                TextBox2.Text = table(0)(1)
                TextBox3.Text = table(0)(2)

                DateTimePicker1.Value = table(0)(3)

            Else

                TextBox2.Text = ""
                TextBox3.Text = ""

                DateTimePicker1.Value = Now()

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message.ToString())

        End Try


    End Sub
End Class
      
///////////////OUTPUT:

vbnet search data in mysql database




Share this

Related Posts

Previous
Next Post »

2 comments

comments