Posted by: Sourav | June 1, 2015

Connecting SQL Server on VB.Net


Sql connection

 

Create a database named test and a table named test inside it using sql server management studio,test table should have 2 columns ,ID and name,ID is the primary key

 

Open vsexpress for desktop

 

Add a class file named sqlcon.vb

 

Imports System.Data.Sql

Imports System.Data.SqlClient

 

Public Class Sqlcon

   Public sqlcon As New SqlConnection With {.ConnectionString = “Server=HOMEPC\SQLEXPRESS;Database=test;User=sa;Pwd=******;”}

 

   Public sqlcomand As New SqlCommand

 

 Public Function hasconnection() As Boolean

       Try

           sqlcon.Open()

           sqlcon.Close()

           Return True

 

       Catch ex As Exception

           MsgBox(ex.Message)

           Return False

 

       End Try

   End Function

End Class

 

 

Now in the form under form load event

 

Dim sqlcon As New Sqlcon

 

   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

       If sqlcon.hasconnection = True Then

           MsgBox(“connected”)

       Else

           MsgBox(“not”)

       End If

   End Sub

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: