Jun 3, 2017

VB.net program to insert,update,delete,display Data using the SQL server

Imports System.Data.SqlClient
Public Class Form1
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    Dim dr As SqlDataReader




    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn = New SqlConnection("Data Source=DESKTOP-TQDE2RV\SQLEXPR<Server>;Initial Catalog=trail;Integrated Security=True")

    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        conn.Open()
        Dim q1 As String = "insert into samp(name,ph)values('" + TextBox1.Text + "','" + TextBox2.Text + "')"

'samp is the table name

        cmd = New SqlCommand(q1, conn)
        cmd.ExecuteNonQuery()
        MessageBox.Show("inserted")
        conn.Close()
        TextBox1.Text = ""
        TextBox2.Text = ""
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        conn.Open()
        Dim q2 As String = "update samp set ph='" + TextBox2.Text + "' where name='" + TextBox1.Text + "'"
        cmd = New SqlCommand(q2, conn)
        cmd.ExecuteNonQuery()
        MessageBox.Show("updated")
        conn.Close()


    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        conn.Open()
        Dim q3 As String = "delete from samp where name='" + TextBox1.Text + "'"
        cmd = New SqlCommand(q3, conn)
        cmd.ExecuteNonQuery()
        MessageBox.Show("deleted")
        conn.Close()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        conn.Open()
        Dim q4 As String = "select ph from samp where name='" + TextBox1.Text + "'"
        cmd = New SqlCommand(q4, conn)
        dr = cmd.ExecuteReader()
        While (dr.Read())
            TextBox2.Text = dr.Item("ph")
        End While

    End Sub
End Class

No comments:

Post a Comment