[Solved] Why is this SqlCeCommand ExecuteReader call failing?


You forgot to open the connection.

string connString = "Data Source=\"\\My Documents\\HHSDB003.sdf\"";
string query = "SELECT * FROM MyTable";

SqlCeConnection conn = new SqlCeConnection(connString);
SqlCeCommand cmd = new SqlCeCommand(query, conn);

conn.Open();  // <--- THIS
SqlCeDataReader rdr = cmd.ExecuteReader();

try
{
    // Iterate through the results
    while (rdr.Read())
    {
    }
}
finally
{
    // Always call Close when done reading
    rdr.Close();
    // Always call Close when done reading
    conn.Close();
}

4

solved Why is this SqlCeCommand ExecuteReader call failing?