Q:

Data Reader Initialization (again)

I am getting a datareader initialization error. I know that this has been answered many times before but those cases don't seem to fit my situation. The error message begins "Execute Reader: Connection property has not been initialized."
The program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace BookList   

{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection dataConnection = new SqlConnection();
            dataConnection.ConnectionString = "Integrated Security = true; Initial Catalog = BIBLIOGRAPHY;Data Source = JBSHAPIRO-PC\\SQLEXPRESS";

            dataConnection.Open();
            Console.Write("Enter author's name: ");
            string person = Console.ReadLine();

            SqlCommand datacommand = new SqlCommand();
            datacommand.CommandText = "SELECT AUTHOR, TITLE, YEAR, KEYWORDS 

FROM BOOKS WHERE AUTHOR = ' " + person + " ' ";

            Console.WriteLine("About to Execute: {0}\n\n", 

datacommand.CommandText);

            SqlDataReader dataReader = datacommand.ExecuteReader();

            while (dataReader.Read())
            {
                string author1 = dataReader.GetString(0);
                string title1 = dataReader.GetString(1);
                int year1 = dataReader.GetInt32(2);
                string keywords1 = dataReader.GetString(3);

                Console.WriteLine(
                    "Author: {0}\n, Title: {1}\n, Year: {2}\n; Key Words: {4)\n\n",
                author1, title1, year1, keywords1);
                dataReader.Close();
            }
            dataConnection.Close();
        }
    }
}

The code works with a different database written for C# 3 but I am now trying to use C# 6.
Any suggestions?

A:

You never associated the SqlCommand with the connection:
SqlCommand datacommand = new SqlCommand();
datacommand.Connection = dataConnection;

Without it, the command (and by extension the data reader) has no knowledge of any connection to use.

