-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?

user4970927
  • 179
  • 8
  • Use that connection string in the `SqlConnection` object when you initialize it. [Doc](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection%28v=vs.110%29.aspx) – OneFineDay Jun 05 '15 at 19:30
  • @OneFineDay Could you provide an example? – user4970927 Jun 05 '15 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. – OneFineDay Jun 05 '15 at 19:36

1 Answers1

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));
        }
    }
}