Tuesday, July 22, 2008

Connect VB.NET with SQL DataBase

Hi friends my this Particular article will tell u about how to Connect Your Application With SQL DataBase.

(But before writing any thing I would like to confess one thing that I have wasted a lot of time searching for this particular thing but I never tried it by my self and one fine lucky day I found it on WEB and come to know that it is nothing more then a piece of Cake)

For this Example U need to

1. Visual studio 2005

2. SQL Server 2000

/*Before starting this example will only tell u how to connect to your database and is in very Crude format*/

Hence u need to modify it According to your needs

Steps for Connecting

1. Create a new Windows Project by clicking on to File->New Project and then selecting a new windows Application

2. Now Just Copy this Code in Your Code Window With a required Changes

Imports System.Data.OleDb
Imports System.Data.sqlclient
Imports System.Data.Odbc


Public Class Form1


Dim myconnection As SqlConnection
Dim mycommand As SqlCommand
Dim dr As SqlDataReader

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

myconnection = New SqlConnection("Server=localhost;uid=sa;pwd =sa;database=FilmyGroup")
Try
myconnection.Open()
mycommand = New SqlCommand("Select * from films", myconnection)
dr = mycommand.ExecuteReader
While dr.Read()
MessageBox.Show(dr(0).ToString)
MessageBox.Show(dr(1).ToString)

End While
dr.Close()
myconnection.Close()
Catch ex As Exception

End Try
End Sub
End Class

No comments: