1

My issue is that I keep getting an error on console that says

Invalid Object Name 'dbo.Products'

I am using Visual C# 2008 Express Edition and SQL Server 2008 Express.

I have had some issue with preparing/installing the Northwind sample database, so I'm unsure if that has any weight on the issue though. Our teacher gave a test program which is the program I am receiving this error.

static void Main()
{
    string connectionString =
            "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Andrew\\Desktop\\SQL Server  2000 Sample Databases\\PUBS.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";

    string queryString =
        "SELECT ProductID, UnitPrice, ProductName FROM dbo.Products "
            + "WHERE UnitPrice > @pricePoint "
            + "ORDER BY UnitPrice DESC;";

    int paramValue = 5;

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Parameters.AddWithValue("@pricePoint", paramValue);

        try
        {
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Console.WriteLine("\t{0}\t{1}\t{2}",
                    reader[0], reader[1], reader[2]);
            }

            reader.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadLine();
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andrew Ricci
  • 475
  • 5
  • 21

1 Answers1

0

Your database is not Northwinds but Pubs.

Check the Connection String

 string connectionString =
        "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Andrew\\Desktop\\SQL Server  2000 Sample Databases\\PUBS.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";

Specifically on AttachDBFileName:

AttachDbFilename=C:\\Users\\Andrew\\Desktop\\SQL Server  2000 Sample Databases\\PUBS.MDF

Try to change it to Northwinds (given it is in the same folder as Pubs), like:

AttachDbFilename=C:\\Users\\Andrew\\Desktop\\SQL Server  2000 Sample Databases\\Northwind.MDF

Update:

Try to use SqlCeConnection instead of Sqlconnection because it looks like you are using compact or SDF:

 using (SqlConnection connection =
    new SqlConnection(connectionString))

To:

 using (SqlCeConnection connection =
    new SqlCeConnection(connectionString))
Edper
  • 9,144
  • 1
  • 27
  • 46
  • In the folder I have "Notherwind.sdf", "NORTHWND.LDF", "PUBS.MDF", "PUBS_LOG.LDF", "instnwnd.sql", "instpubs.sql" and a ReadMe text file. The default of the code what indeed Northwind.MDF but I did not have it. The error is get now is "An attempt to attach an auto-named database for file C:\path\to\file\Notherwind.SDF failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share". – Andrew Ricci Sep 17 '14 at 23:08
  • Look's like you're using a compact edition of sql and not the regular SQL expre3ss server. Check my update above. – Edper Sep 18 '14 at 00:11
  • I am sorry to have given you faulty information. But I have fixed the issue regarding the "NORTHWND.MDF". Unfortunately, I have a new error, before I paste it keep in mind. I copy and pasted the Northwind.sdf file from the SQL Server 3.5 samples folder, which Im assuming is the cause of this error. – Andrew Ricci Sep 18 '14 at 00:25
  • ERROR: One or more files fo not match the primary file of the databse. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup. Cannot open user default database. Login failed. Login failed for user 'LENOVO-PC\Andrew'. Log file 'C:\SQL Server 2000 Sample Databases\NORTHWND_log.ldf' does not match the primary file. It may be from a different database or the log may have been rebuilt previously. – Andrew Ricci Sep 18 '14 at 00:26
  • The .ldf file is the log file for your database and is associated with our .mdf file which is the main file for your database. So, when it says it does not match it means that your log file is not the actual log file of your mdf file. – Edper Sep 18 '14 at 00:28
  • 1
    DUDE YOUR THE MAN. Just to explain to anyone who is reading this, while fixing the .MDF and pathing issue I re-installed the Sample Databases. When you uninstall it leaves the log file in the original folder still....delete that. Bang it works Thanks very much dude. I very much appreciate it. And thanks for being my first to the start of joining this awesome community. – Andrew Ricci Sep 18 '14 at 00:38