Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

How do you incorporate a SQL query into Visual Studio? Here is a multi join code as an example

Multi join using SQL server management studio SELECT S.companyname AS supplier, S.country, P.productid, P.productname, P.unitprice, C.categoryname FROM Production.Suppliers AS S LEFT OUTER JOIN Production.Products AS P ON S.supplierid = P.supplierid INNER JOIN Production.Categories AS C ON C.Categoryid = P.categoryid WHERE S.country = N'Japan';

2 Answers

Steven Parker
Steven Parker
231,261 Points

First you would establish a connection with the DB using SqlConnection.Open(). Then, you can create an SqlCommand object and use it to execute the command and create a SqlDataReader object which you can then use to retrieve the results.

    using (var connection = new SqlConnection("user id=username;password=password;" + 
                                              "server=serverurl;Trusted_Connection=yes;" + 
                                              "database=database;connection timeout=30"))
    {
       connection.Open();

       var cmd = new SqlCommand("your SQL command goes here",  connection);

       var reader = cmd.ExecuteReader();
       // Data is accessible through the SqlDataReader object "reader" here.

       connection.Close();
    }

But a more elegant way to access the database is through the Entity Framework. And there are several courses here covering that topic.

Hi Steven, Thank you for responding. I will definitely cover the entity framework course. You covered some of the holes in my knowledge that I was missing.