title: Visual Studio C# and SQL Server : connection property has not been initialized
body: <p>I'm trying to connect to a local database (service-based database created in Visual Studio 2013). </p>
  
  <p>I use this C# code:</p>
  
  <pre><code>string connectionstring = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"|DataDirectory|\\InvDB.mdf\";Integrated Security=True";
  
  public int testpripojeni()
  {
      using(SqlConnection pripojeni  = new SqlConnection(connectionstring))
      {
          pripojeni.Open();
  
          SqlCommand prikaz = new SqlCommand();
          prikaz.CommandText = " SELECT COUNT (*) FROM HlavniTab";
  
          int pocet = (int)prikaz.ExecuteScalar();
  
          pripojeni.Close();
          return pocet;
      }
  }
  </code></pre>
  
  <p>This function should connect to the database and count rows in table <code>HlavniTab</code>. But I get error on line</p>
  
  <pre><code>int pocet = (int)prikaz.ExecuteScalar();
  </code></pre>
  
  <p>It says</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll</p>
    
    <p>Additional information: ExecuteScalar: Connection property has not been initialized.</p>
  </blockquote>
  
  <p>What should I do to fix it?</p>