Click here to Skip to main content
16,013,338 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello every one...
I dont know how to connect to database in visual studio 2010...
Posted
Comments
Ankur\m/ 13-Jan-12 7:59am    
So get a book and start reading. Or searching on Google will definitely help too. Put some effort my friend!

Assuming this is an SQL database, try:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT iD, description FROM myTable", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                }
            }
        }
    }
You will need to specify the connection string, but you can get that quite easily.
1) Open the Server Explorer pane in VS
2) Look at the Data Connections list. If your data base is there, skip to step 4
3) Create a connection to your database.
3.1) Right Click "Data Connections", and select "Add Connection..."
3.2) Fill in the dialog that results, until the "Test Connection" button works - can't help you here, way too many variables
3.3) Click OK.
4) Click on the database name to highlight it.
5) In the properties pane, you will find the connection string - you can copy and paste this, but it is a good idea to keep it in the application config file.
 
Share this answer
 
its same as in vs2008.
use ado.net syntax

I hope you were asking for the way to use ado.net
 
Share this answer
 
Read from Koegent Publications Book of ASp.Net 4.0 in simple steps buy this book and see how you can use database in 4.0 framework
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900