Hi,
In Visual Studio 2008 there are two ways to connect database.
1.Traditional SqlClient objects (That one you are using in Visual Studio 2005)
2.LINQ to SQL (New technology introduced in .Net framework 3.5 supported by Visual Studio 2008)
Please look into LINQ tutorials for LINQ to SQL.
Here I will explain how to connect database through SQLClient objects with an example.
-
'Importing namespace which had sql objects
-
Imports System.Data.SqlClient
-
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
-
'Intitalizes connection object
-
Dim con As New SqlConnection
-
con.ConnectionString = "Server=MyOrgServer;database=CompanyDB;user id=sa;password=passo"
-
-
'Initializes data adapter to retrieve data
-
Dim adapt As New SqlDataAdapter("select * from customers", con)
-
-
'Executes select statement and fill data into dataset
-
Dim CustomerDataSet As New DataSet
-
adapt.Fill(CustomerDataSet)
-
-
'Initializes command object to execute DML(Insert,Update & Delete) statements.
-
Dim cmd As New SqlCommand
-
cmd.Connection = con
-
-
Dim qry As String
-
-
'Executes insert command
-
qry = "Insert into customers(Name,Address,DOB) Values('John','11,N.J.Street,New York','2-23-1980')"
-
cmd.CommandText = qry
-
con.Open()
-
cmd.ExecuteNonQuery()
-
con.Close()
-
-
'Executes update command
-
qry = "Update Customers Set Address='23,New Street,NJ' Where Name like 'John'"
-
cmd.CommandText = qry
-
con.Open()
-
cmd.ExecuteNonQuery()
-
con.Close()
-
-
'Executes delete command
-
qry = "Delete from customers where name like 'John'"
-
cmd.CommandText = qry
-
con.Open()
-
cmd.ExecuteNonQuery()
-
con.Close()
-
-
End Sub
Regards,
Balaji U