2

Ok, so I know there are other SO posts on this subject, but none of them are helping me on this. My code is throwing an exception saying:

Invalid Object Name 'Player'...

The Player table is included. I can see it in the Server Explorer. Where am I going wrong here? Thanks!

string connectString = @"Data Source=(LocalDB)\v11.0;
                         AttachDbFilename=C:\Users\Lisa\Desktop\CIS 365\BaseballDB\Baseball.mdf;
                         Integrated Security=True;Connect Timeout=30";

using (SqlConnection con = new SqlConnection(connectString))
{
    string selectQuery = @"SELECT * FROM PLAYER";

    using (SqlCommand com = new SqlCommand(selectQuery, con))
    {
        con.Open();

        SqlDataReader reader = com.ExecuteReader();

        if(reader.HasRows)
        {
            while(reader.Read())
            {
                Console.WriteLine("{0}\t{1}", reader.GetInt32(0), reader.GetString(1));
            }
        }
        else
            Console.WriteLine("No Rows Found.");

        reader.Close();
    }
}
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
lisabits
  • 1
  • 1
  • 8
  • Does it help if you prepend a "dbo." as in "SELECT * FROM dbo.PLAYER"? If not, try LINQPad - see if you can construct a connection (it has a wizard) and try your query in there. – B. Clay Shannon-B. Crow Raven Nov 30 '15 at 21:45
  • No, @B.ClayShannon, that just threw the same exception but with dbo.PLAYER instead. – lisabits Nov 30 '15 at 21:57
  • If LINQPad works for you, you can grab the connection string it will have created and use that to replace what you've assigned to connectString. – B. Clay Shannon-B. Crow Raven Nov 30 '15 at 21:59
  • Just execute the SQL Query into SSMS and chekc if you have same errors – CodeNotFound Nov 30 '15 at 22:09
  • 1
    I might try that, @B.ClayShannon. Thanks. I'm still very new to LINQ. This is for an assignment for extra credit. My professor hasn't really even been able to help me out with it. This is basically just a test I'm doing here and then I'll edit it to fulfill the specs. We're supposed to be able to type a query in a text box on WinForms, and return the result of that query in a dialog box. If I could just get the stupid query to work, haha... – lisabits Nov 30 '15 at 22:13
  • @lisabits: http://www.linqpad.net/download.aspx – B. Clay Shannon-B. Crow Raven Nov 30 '15 at 22:16
  • @CodeNotFound, I don't have SSMS and have never used it, but thanks for the suggestion. I'm very new to all of this. – lisabits Nov 30 '15 at 22:18
  • @B.ClayShannon... So how is LINQPAD different from LINQ? – lisabits Nov 30 '15 at 22:20
  • @lisabits: It's ostensibly primarily useful for LINQ, but it's really good for any kind of database work. I'm using it for connecting to databases, viewing their structure, looking at Stored Procs, etc. It has a connection wizard that allows you to select SQL Server and then point to the location of the database, and it builds the connection string for you. You then can test the connection directly from there to make sure it works. If it does, you know there's nothing wrong with the conn string. Then try your query in LINQPad (just remember to choose "SQL" and the database from the dropdowns). – B. Clay Shannon-B. Crow Raven Nov 30 '15 at 22:23
  • @B.ClayShannon, my connection is working fine. I just tested it. – lisabits Nov 30 '15 at 22:28
  • @lisabits: In LINQPad, or...? – B. Clay Shannon-B. Crow Raven Nov 30 '15 at 22:29
  • @B.ClayShannon In LINQ – lisabits Nov 30 '15 at 22:30
  • @lisabits: So can you query successfully there? If not, what err msg does it give you? – B. Clay Shannon-B. Crow Raven Nov 30 '15 at 22:31
  • @B.ClayShannon, No, I'm not able to query successfully. I just tested the connection... You mentioned about LinqPad replacing connection strings. I don't see how that would help when Linq says my connection works. – lisabits Nov 30 '15 at 22:34
  • @lisabits: I don't know what you mean by LINQ; I thought you meant LINQPad. – B. Clay Shannon-B. Crow Raven Nov 30 '15 at 22:36
  • Try adding that its a user instance. See if that helps. User Instance=True http://stackoverflow.com/questions/8926512/how-do-i-connect-to-an-mdf-database-file (this would be an issue with permissions and integrated auth etc, which you could then fix and take the user instance back out - sometimes an error will say the table doesn't exist when in fact it does but it's not "visible" to the creds calling it) – Nikki9696 Nov 30 '15 at 22:37
  • @Nikki9696, thanks, but that doesn't work. It throws a different exception. "The user instance login flag is not allowed when connecting to a user instance of sql server. The connection will be closed." – lisabits Nov 30 '15 at 22:45
  • Ah, okay, localdb thing. Sorry, I'm used to normal sql server. – Nikki9696 Dec 01 '15 at 17:51

0 Answers0