Miyerkules, Mayo 22, 2013

VB and SQL Connection

Hi Guys,

I've created a simple code of VB 2008 and SQL 2008 connection.

In the module area:


Imports System.Net.Sockets
Imports System.Text
Imports System.Data.SqlClient

Module Module1
    Public Con As SqlConnection
    Public constr As String
    Public usertype As String

    Public Sub main()
        constr = " Server = DONA-PC\SQLEXPRESS ; Database = master ; Trusted_Connection = true"
        Con = New SqlConnection(constr)
        Try
            Con.Open()
            Con.Close()

        Catch ex As Exception
            MsgBox("sorry no connection")

        End Try
    End Sub
End Module        
==========================================================

Retrieve records in Sql using Vb.net DataGridView:


 Private Sub CmdRetrieve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdRetrieve.Click

        Call main()
        Con.Open()
   
        Dim cmd As New SqlCommand("select * from Books", Con)

        Dim ada As New SqlDataAdapter(cmd)
        Dim ds As New DataSet

        ada.Fill(ds)

        DataGridView1.DataSource = ds.Tables(0)

        Con.Close()
==========================================================

Hope this may help you...
Please feel free to leave comments and suggestions...

Thank you Guys!.