February 9th, 2012, 05:03 AM
Invalid object name
I have an SQL Express file with a single table People. People has six or seven columns, but no rows. The database is a resource in my Visual Studio 2010 Express project. I am trying to connect with this code:
However, I am getting the following error:
SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename=C:\\someLocation\\someDatabase.mdf; Integrated Security=True; User Instance=True ");
SqlCommand cmd = new SqlCommand(@"select * from People;", conn);
Based on some casual googling I tried to precede the table name with dbo (dbo.People) but that did not resolve the issue. What might I be doing wrong?
Invalid object name 'People'.
February 9th, 2012, 08:17 AM
Looks like you don't have a table named [People].
Verify that the table does exist and is spelled correctly.
February 9th, 2012, 08:55 AM
Thank you. Actually, there is most certainly a table named People, spelling verified by copy-and-paste.
February 13th, 2012, 05:56 AM
It can be seen here that the table does exist, and is populated with data:
Originally Posted by dotancohen
February 21st, 2012, 10:48 AM
Most of the time this is because you are looking in the wrong database.
Are you positive your pointing to the correct database?
Comments on this post
February 21st, 2012, 12:58 PM
I want to say that I'm sure, but I'm not! I now see that since my project is on a network drive Visual Studio is creating the database inside the project, then silently making a copy in my profile folder (I'm not running as Admin) and using that one. So the database created in the project is not the same database that is shown in the Database Explorer.
What a mess! At least I've found the issue. Thanks.
February 21st, 2012, 01:03 PM
Yeah no problem. It's very common.
I don't remember how many times I did that to myself in the management studio. I had master db selected but was trying to query tables from another database.