Follow Me via SMS for FREE

hi, If you want to receive free daily updates and notification from SSUET you can subscribe me for free
Write follow SSUETHelp and send it to 40404
And you can continue receiving Free Updates..

[Tutorial][VB.Net/MsSQL] How to connect and perform Insertion using MsSQL in VB.net Windows Application?

. Friday 11 May 2012
  • Agregar a Technorati
  • Agregar a Del.icio.us
  • Agregar a DiggIt!
  • Agregar a Yahoo!
  • Agregar a Google
  • Agregar a Meneame
  • Agregar a Furl
  • Agregar a Reddit
  • Agregar a Magnolia
  • Agregar a Blinklist
  • Agregar a Blogmarks

To connect with MsSQL server you need to call some libraries which contains information regarding db connection and execution

1) Call these two libraries at top of the coding page
Imports System.Data
Imports System.Data.SqlClient



2) Now we have to initialize some of our server detail to initialize our connection with our database.

Add this code in your main class of your form and modify it according to your database details.

Dim connstring As String = "Data Source=.;Initial Catalog=sxeInventory;Integrated Security=True"
Dim conn As New SqlConnection(connstring)


Where Data Source=.; automatically gets the default server name and Initial Catalog=sxeInventory; refers to the name of database

3) Now inserting records from user controls i-e Textbox, ComboBox etc.

I assume a form having following fields


Now After creating your form, Double click on the button on which you want to perform insertion.

Modify Insert Query according to your columns  in database.

Try
            conn.Open() 'Opening the connection to perform SQL Queries

            'Performing an Insert Query to INSERT data into db using USER CONTROLS
            Dim command As New SqlCommand("INSERT INTO sxe_Products ([sxe_pName],[sxe_pDescription],[sxe_pCatId],[sxe_pPrice],[sxe_pCompanyID],[sxe_pDateAdded])" &
                                          "VALUES('" & Me.txt_pName.Text & "','" & Me.txt_pDesc.Text & "','" & Me.ComboBox2.SelectedValue & "'," &
                                          "'" & Me.txt_pPrice.Text & "','" & Me.ComboBox1.SelectedValue & "','" & DateTime.Now.ToString() & "')", conn)

            'Finally Executing Insert Query in a IF fuction!
            'If Successfully EXECUTED then show MSG.
            If (command.ExecuteNonQuery()) Then
                MsgBox("Product Successfully Added!", MsgBoxStyle.OkOnly, "Added")

                clearFields()

            End If

            conn.Close() 'Closing the Open DB Connection


        Catch ex As Exception
            'If exception occured it will be show in a MsgBox
            MsgBox(ex.Message)

        End Try


All set..  Run.! Hope it will work, For any issue feel free to ask me.. :)

0 comments: