-1

So for a rundown of my problem, I am working for a company that uses SQL and Visual Studio, and I am in training since I have never used SQL before. The way they said that they connect to the SQL server using ADO.Net, however as I have been researching, there are a lot of different ways to go about doing this.

My question is, in a Console Application, if I already have a connection string and I do not need to build one, how do I connect to SQL that doesn't use an Entity Frame work or uses LINQ to SQL?

3
  • Use that connection string in the SqlConnection object when you initialize it. Doc Commented Jun 5, 2015 at 19:30
  • @OneFineDay Could you provide an example? Commented Jun 5, 2015 at 19:31
  • It depends on how you want the data back. A dataAdapter fills a DataTable which is great for DataGridViews. There is a dataReader which would allow you to get the info while in a loop. Commented Jun 5, 2015 at 19:36

1 Answer 1

1

This page shows a good example of how to go about it. Here's the piece of code that would be of most interest to you:

using (SqlConnection con = new SqlConnection(connectionString))
{
    con.Open();

    using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            //Use reader.GetInt32, reader.GetString, etc 
            //depending on the type of data in the table
            Console.WriteLine("{0} {1} {2}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.