问题
I'm kind of new to C# and am trying out the convenient looking adaper-dataset combo with local database. while I was amazed at how easy and useful it was I stumbled upon an issue:
Whenever I try to fill the Item table with the adapter I get Invalid object name SQLexception, I use the same Dataset for all my tables, dsOns.Users / dsOns.Items. The problem is, the users adapter does recognize and successfully works with Users database(dbo.Users) while the items adapter cannot find the table(dbo.Item).
Heres some sniplets:
UsersTableAdapter uAdapter = new UsersTableAdapter();
ItemTableAdapter iAdapter = new ItemTableAdapter();
Users select:
SELECT Id, name, password, sold, isLogged, lastLog FROM dbo.Users
Item select:
SELECT Id, name, sale_price, min_price, cog FROM dbo.Item
Both use the same connection string:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ons_data.mdf;Integrated Security=True;Connect Timeout=30
Also note that in Dataset designer, the adapter works fine, it selects successfully. The error only occurs in code.
Image of the schema:
What could possibly cause this?
回答1:
I am unsure if item may be considered a reserved word in some cases perhaps in the context of your connection. Might want to refer to is at [dbo].[item].
回答2:
Okay that was weird, I solved it by simply changing the view of the database explorer, to find that the app uses two databases, in UI(dataset designer) it uses my original db, while in code it uses the same db(but outdated since I changed it via dataset designer) that was copied to debug folder, hence containing only Users table alone and resulting in the error
回答3:
Try changing the query which you are passing to the SqlDataReader in my case it was "Select [Name],[Location],[Email] FROM [dbo].[CreateDB]"
but I tried "Select Name,Location,Email FROM YourTableName"
and it worked, you can also use "Select * FROM YourTableName"
.
来源:https://stackoverflow.com/questions/23729159/invalid-object-name-dbo-item