Hi.I am very new to the vb.net programming.
Now I am trying to connect the database that is sql server 2005 to the vb.net code so that I can add the data to my database, which is already in the sql server and retrieve the required data from the database.
Please help me out by providing the complete working sample code.
with which i will be able to understand how things work.
Expecting some hep from the experts.
Thanks in advance to all.

Edited 6 Years Ago by ecrockers: n/a

You need to import System.Data.SqlClient namespace.

Imports System.Data.SqlClient
....

Use SqlConnection class to connect,

Dim Cn as New SqlConnection("your_connection_string")
Cn.Open()
..

Hope this help u..

Retrieving Data into a Datagridview

dim cn as new sqlclient.sqlconnection

dim bd as new bindingsource

cn.connectionstring=”data source=.; initial catalog=customers; integrated security=true”

cn.open

dim co as new sqlclient.sqlcommand("Select * from customers",cn)

bd.datasource=co.executereader

datagridview1.datasource=bd

cn.close

ADDING DATA

dim cn as new sqlclient.sqlconnection

dim bd as new bindingsource

cn.connectionstring=”data source=.; initial catalog=customers; integrated security=true”

cn.open

dim co as new sqlclient.sqlcommand("Insert into customers(No, Name, Phone) values('1', 'Rudi', '001')",cn)

co.executenonquery

Yep it did work:)..Sorry for the late reply:$..I thought I have already replied u:-/..Anyways Thank U very much buddy..;):)

This question has already been answered. Start a new discussion instead.