You are on page 1of 6

Search Data In SqlServer DataBase

Imports System.Data.SqlClient

Public Class VBNET_SQL_SEARCH

Private Sub BTN_SEARCH_Click(sender As Object, e As EventArgs) Handles


BTN_SEARCH.Click

Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB;


Integrated Security = true")

Dim command As New SqlCommand("select * from Users where Id = @id", connection)

command.Parameters.Add("@id", SqlDbType.Int).Value = TextBoxID.Text

Dim adapter As New SqlDataAdapter(command)

Dim table As New DataTable()

adapter.Fill(table)

TextBoxFN.Text = ""
TextBoxLN.Text = ""
TextBoxAGE.Text = ""

If table.Rows.Count() > 0 Then


' return only 1 row
TextBoxFN.Text = table.Rows(0)(1).ToString()
TextBoxLN.Text = table.Rows(0)(2).ToString()
TextBoxAGE.Text = table.Rows(0)(3).ToString()

Else
MessageBox.Show("NO Data Found")
End If
End Sub
End Class

Search Data In SQL And Show It On DataGridView

Imports System.Data.SqlClient

Public Class VBNET_SQL_Datagridview_Search

Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB;


Integrated Security = true")
Private Sub VBNET_SQL_Datagridview_Search_Load(sender As Object, e As EventArgs)
Handles MyBase.Load

FilterData("")

End Sub

Public Sub FilterData(valueToSearch As String)


'SELECT * From Users WHERE CONCAT(fname, lname, age) like '%F%'
Dim searchQuery As String = "SELECT * From Users WHERE CONCAT(fname, lname, age)
like '%" & valueToSearch & "%'"

Dim command As New SqlCommand(searchQuery, connection)


Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()

adapter.Fill(table)

DataGridView1.DataSource = table

End Sub

Private Sub BTN_FILTER_Click(sender As Object, e As EventArgs) Handles BTN_FILTER.Click

FilterData(TextBox1.Text)

End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles


TextBox1.TextChanged

FilterData(TextBox1.Text)

End Sub
End Class

Insert Update Delete Data From SQL Server

Imports System.Data.SqlClient

Public Class VBNET_SQL_Insert_Update_Delete

Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB;


Integrated Security = true")

Private Sub BTN_INSERT_Click(sender As Object, e As EventArgs) Handles


BTN_INSERT.Click
Dim insertQuery As String = "INSERT INTO Users (Fname,Lname,age) VALUES('" &
TextBoxFN.Text & "','" & TextBoxLN.Text & "'," & TextBoxAGE.Text & ")"

ExecuteQuery(insertQuery)

MessageBox.Show("Data Inserted")

End Sub

Public Sub ExecuteQuery(query As String)

Dim command As New SqlCommand(query, connection)

connection.Open()

command.ExecuteNonQuery()

connection.Close()

End Sub

Private Sub BTN_UPDATE_Click(sender As Object, e As EventArgs) Handles


BTN_UPDATE.Click

Dim updateQuery As String = "Update Users Set Fname = '" & TextBoxFN.Text & "'
,Lname = '" & TextBoxLN.Text & "',age = " & TextBoxAGE.Text & " WHERE Id =" &
TextBoxID.Text & ""
ExecuteQuery(updateQuery)
MessageBox.Show("Data Updated")

End Sub

Private Sub BTN_DELETE_Click(sender As Object, e As EventArgs) Handles


BTN_DELETE.Click

Dim deleteQuery As String = "delete from Users Where Id = " & TextBoxID.Text
ExecuteQuery(deleteQuery)
MessageBox.Show("User deleted")

End Sub
End Class
Login Form with SQL Server DataBase

Imports System.Data.SqlClient

Public Class VBNET_Login_Form_With_SQL

Private Sub BTN_LOGIN_Click(sender As Object, e As EventArgs) Handles BTN_LOGIN.Click

Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB;


Integrated Security = true")

Dim command As New SqlCommand("select * from Table_Login where Username =


@username and Password = @password", connection)

command.Parameters.Add("@username", SqlDbType.VarChar).Value =
TextBoxUsername.Text
command.Parameters.Add("@password", SqlDbType.VarChar).Value =
TextBoxPassword.Text

Dim adapter As New SqlDataAdapter(command)

Dim table As New DataTable()

adapter.Fill(table)

If table.Rows.Count() <= 0 Then

MessageBox.Show("Username Or Password Are Invalid")

Else

'MessageBox.Show("Login Successfully")

Dim frm As New VBNET_SQL_Insert_Update_Delete()

Me.Hide()

frm.Show()

End If

End Sub

Private Sub BTN_CANCEL_Click(sender As Object, e As EventArgs) Handles


BTN_CANCEL.Click

Me.Close()
End Sub
End Class

Save Image Into SQL Server DataBase

Imports System.Data.SqlClient
Imports System.IO

Public Class VBNET_Insert_Image_Into_SQL

Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB;


Integrated Security = true")
Private Sub BTN_BROWSE_Click(sender As Object, e As EventArgs) Handles
BTN_BROWSE.Click

Dim opf As New OpenFileDialog

opf.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"

If opf.ShowDialog = Windows.Forms.DialogResult.OK Then

PictureBox1.Image = Image.FromFile(opf.FileName)

End If

End Sub

Private Sub BTN_INSER_Click(sender As Object, e As EventArgs) Handles BTN_INSER.Click

Dim command As New SqlCommand("insert into


Table_Images(name,description,the_image) values(@name,@desc,@img)", connection)

Dim ms As New MemoryStream


PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)

command.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBoxName.Text


command.Parameters.Add("@desc", SqlDbType.VarChar).Value = TextBoxDesc.Text
command.Parameters.Add("@img", SqlDbType.Image).Value = ms.ToArray()

connection.Open()

If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("Image Inserted")
Else
MessageBox.Show("Image Not Inserted")
End If
connection.Close()
End Sub
End Class

Retrieve Image From SQL Server DataBase

Imports System.Data.SqlClient
Imports System.IO

Public Class VBNET_Get_Image_From_SQL

Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB;


Integrated Security = true")

Private Sub BTN_SHOW_Click(sender As Object, e As EventArgs) Handles BTN_SHOW.Click

Dim command As New SqlCommand("select * from Table_Images where id = @id",


connection)
command.Parameters.Add("@id", SqlDbType.VarChar).Value = TextBoxID.Text

Dim table As New DataTable()

Dim adapter As New SqlDataAdapter(command)

adapter.Fill(table)

If table.Rows.Count() <= 0 Then

MessageBox.Show("No Image For This Id")


Else

TextBoxID.Text = table.Rows(0)(0).ToString()
TextBoxName.Text = table.Rows(0)(1).ToString()
TextBoxDesc.Text = table.Rows(0)(2).ToString()

Dim img() As Byte

img = table.Rows(0)(3)

Dim ms As New MemoryStream(img)

PictureBox1.Image = Image.FromStream(ms)

End If

End Sub
End Class

You might also like