Sunday, November 20, 2016

Post #7 : Programming " CRUD Part 1 - How to Insert or Create data in SQL Server using VB.net"

Hi Jack,

in the programming there is a very important process, no matter what the product is made, for dealing with the database CRUD definitely used.

CRUD what the heck is that ?


CRUD stand for Create, Read, Update and Delete.

According to wiki here, CRUD are four basic function of data storage. for more detail just read here.

Okay we will begin to learn the basic.

IDE : Visual Studio 2013, actually it does not matter you use VS 2013, 2015 or even 2010.
Database : SQL Server 2008, you also can use MS Access or SQLite database.
Language : Visual Basic .Net (VB.net) , SQL

here i have the notion you understand and are able to create database and tables, if you have not been able to create a database or table you can visit here.

i would make a simple table like this.


save with table name "cars"

and in visual studio create a form like this.



i assume you understan how to make a connection string for sql server, if you do not know please click here.

after the form above is successfully created, put this script into the form.



Private Sub save()
        'call connection string
        Using ikon = New SqlConnection(server_connection.connection_string)
            'check connection string, if connection string already open then close it
            If ikon.State = ConnectionState.Open Then ikon.Close()
            'open new connection
            ikon.Open()
            'using sql transaction 
            Dim trans = ikon.BeginTransaction
            Try
                'in this line you'll create command to insert the date into database using parameter @
                Using iCmd = New SqlCommand("insert into cars values(@code,@merk,@type,@year)", ikon)
                    'declare transaction for this sql command 
                    iCmd.Transaction = trans
                    'declare sqlparameter and fillit with object (textbox,combobox, or data collection like list(of t)
                    With iCmd.Parameters
                        .Add(New SqlParameter("@code", tcode.Text))
                        .Add(New SqlParameter("@merk", tmerek.Text))
                        .Add(New SqlParameter("@type", ttype.Text))
                        .Add(New SqlParameter("@year", tyear.Text))
                    End With
                    iCmd.ExecuteNonQuery()
                End Using
                'if all command has been transfered to server than commit it
                trans.Commit()
                MsgBox("succedd", MsgBoxStyle.Information)
            Catch ex As Exception
                'if found error, cancel all transaction
                trans.Rollback()
                'give error message
                MsgBox("Error " & ex.Message, MsgBoxStyle.Critical)
            End Try
        End Using
    End Sub
put this script into command button to call save method

Private Sub Btn_save_Click(sender As Object, e As EventArgs) Handles Btn_save.Click
        save()
End Sub

CONGRATULATIONS !!!

Sample Project : Download here
Next : CRUD Part 2 - How to Select and Find data in SQL Server using VB.net"












No comments:

Post a Comment