2/8/19

VB.NET DB Connectivity Coding Reference part - 6 | CRUD Operation Using Store Procedures

Previous post we have learned 
VB.NET Database Connectivity Coding Reference part - 1 | Database Connection
VB.NET DB Connectivity Coding Reference part - 2 | CRUD & Navigation Operation.
VB.NET DB Connectivity Coding Reference part - 4 | List Box, Combo Box View the Data
VB.NET DB Connectivity Coding Reference part - 5 | Login user account

In this post, We're going to learn how to List Box, Combo Box View the Data.
This tutorial have one file.
1. FormAddUserUsingSP

Resource File Download  that file have "Student" database which includes Store Procedures.


If you want to store procedure code, I 've put the down below. But I already created th
"SP" Which means Store Procedure.

SP_ADD - Store Procedure :

USE [Student]
GO
/****** Object:  StoredProcedure [dbo].[SP_ADD]    Script Date: 02/08/2019 15:57:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[SP_ADD]
@user Varchar(50),
@pass Varchar(50)
As 
INSERT INTO dbo.Login Values(@user,@pass)


SP_Delete - Store Procedure :

USE [Student]
GO
/****** Object:  StoredProcedure [dbo].[SP_Delete]    Script Date: 02/08/2019 16:21:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[SP_Delete]
@user varchar(50)
As
Delete from userlogin where username=@user


SP_Login - Store Procedure :

USE [Student]
GO
/****** Object:  StoredProcedure [dbo].[Sp_Login]    Script Date: 02/08/2019 16:21:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[Sp_Login]
@user varchar(50),
@pass varchar(50)
As
Select * From dbo.userlogin where username=@user and password=@pass

SP_Update - Store Procedure :

USE [Student]
GO
/****** Object:  StoredProcedure [dbo].[SP_Update]    Script Date: 02/08/2019 16:24:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[SP_Update]
@user Varchar(50),
@pass Varchar(50)
As
Update Login Set Username=@user, Password=@pass Where Username=@user



FormAddUserUsingSP (Design View):



FormAddUserUsingSP (Code View):

Imports System.Data.SqlClient
Public Class FormAddUserUsingSP

    Dim sqlConn As New SqlConnection("Server=Najathi\SQLEXPRESS;Database=Student;Integrated Security=True")

    Dim SqlCmd As SqlCommand
    Dim sqlDA As SqlDataAdapter
    Dim dt As DataTable

    Private Sub ButtonRegister_Click(sender As Object, e As EventArgs) Handles ButtonRegister.Click
        Dim para(1) As SqlParameter

        para(0) = New SqlParameter("@user", SqlDbType.VarChar)
        para(0).Value = TextBoxUsername.Text

        para(1) = New SqlParameter("@pass", SqlDbType.VarChar)
        para(1).Value = TextBoxPassword.Text

        SqlCmd = New SqlCommand
        SqlCmd.Connection = sqlConn
        SqlCmd.CommandType = CommandType.StoredProcedure
        SqlCmd.CommandText = "SP_Add"

        SqlCmd.Parameters.AddRange(para)

        sqlConn.Open()

        SqlCmd.ExecuteNonQuery()

        sqlConn.Close()

    End Sub

    Private Sub ButtonUpdate_Click(sender As Object, e As EventArgs) Handles ButtonUpdate.Click

        Dim para(1) As SqlParameter

        para(0) = New SqlParameter("@user", SqlDbType.VarChar)
        para(0).Value = TextBoxUsername.Text

        para(1) = New SqlParameter("@pass", SqlDbType.VarChar)
        para(1).Value = TextBoxPassword.Text

        SqlCmd = New SqlCommand
        SqlCmd.Connection = sqlConn
        SqlCmd.CommandType = CommandType.StoredProcedure
        SqlCmd.CommandText = "SP_Update"
        SqlCmd.Parameters.AddRange(para)

        sqlConn.Open()

        SqlCmd.ExecuteNonQuery()

        sqlConn.Close()


    End Sub

    Private Sub ButtonDelete_Click(sender As Object, e As EventArgs) Handles ButtonDelete.Click

        Dim para As SqlParameter

        para = New SqlParameter("@user", SqlDbType.VarChar)
        para.Value = TextBoxUsername.Text

        SqlCmd = New SqlCommand
        SqlCmd.Connection = sqlConn
        SqlCmd.CommandType = CommandType.StoredProcedure
        SqlCmd.CommandText = "SP_Delete"
        SqlCmd.Parameters.Add(para)


        sqlConn.Open()

        SqlCmd.ExecuteNonQuery()

        sqlConn.Close()

    End Sub

    Private Sub ButtonCancel_Click(sender As Object, e As EventArgs) Handles ButtonCancel.Click
        Me.Close()
    End Sub

End Class



No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *