Click here to Skip to main content
16,017,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some questions here,i'm sorry if I ask the wrong questions,but I just want to know how do we connect the C# programming using Visual Studio and the database and what is the actually database used by C# programming if i'm asking the wrong questions

thank you for your kindness
Posted

1 solution

To answer your questions in reverse order:
"what is the actually database used by C# programming"

There isn't one, in the sense you mean. You do not have to use a database of any form in your program, although you can use any you have a valid connection to and the appropriate classes. Visual Studio itself does keep some database information to help it with processing your code, and this is stored in an SQL database.

"how do we connect the C# programming using Visual Studio and the database"

There are a number of ways, you can create a Connection, and a Command, and then use a Reader, or you could use a DataAdapter and DataTables with DataBinding.

The easier way to do it is probably with a Connection and a Command:
C#
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);
                }
            }
        }
    }
To work out the connection string, try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.
 
Share this answer
 
Comments
phongsuk wangdu 2-Aug-12 5:21am    
So,what is you trying to say is that C# language has the same concept as we doing the database for Visio Basic right?the different is just the language that we used to programme.ok i've got it..thanks for your answer.
OriginalGriff 2-Aug-12 5:29am    
If you mean Visual Basic (rather than Visio Basic) then yes, exactly that.
Visual Basic and C# are intimately related - you can even convert code between the two languages pretty easily. There are even a few web sites that do it online.

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