SQL Server reports 'Invalid column name', but the column is present and the query works through management studio

前端 未结 12 613
生来不讨喜
生来不讨喜 2020-12-04 11:35

I\'ve hit a bit of an impasse. I have a query that is generated by some C# code. The query works fine in Microsoft SQL Server Management Studio whe

相关标签:
12条回答
  • 2020-12-04 11:54

    In my case it seems the problem was a weird caching problem. The solutions above didn't work.

    If your code was working fine and you added a column to one of your tables and it gives the 'invalid column name' error, and the solutions above doesn't work, try this: First run only the section of code for creating that modified table and then run the whole code.

    0 讨论(0)
  • 2020-12-04 11:55

    In my case I was trying to get the value from wrong ResultSet when querying multiple SQL statements.

    0 讨论(0)
  • 2020-12-04 11:57

    If you are using variables with the same name as your column, it could be that you forgot the '@' variable marker. In an INSERT statement it will be detected as a column.

    0 讨论(0)
  • 2020-12-04 11:57

    Also happens when you forget to change the ConnectionString and ask a table that has no idea about the changes you're making locally.

    0 讨论(0)
  • 2020-12-04 12:02

    If you are running this inside a transaction and a SQL statement before this drops/alters the table you can also get this message.

    0 讨论(0)
  • 2020-12-04 12:03

    Including this answer because this was the top result for "invalid column name sql" on google and I didn't see this answer here. In my case, I was getting Invalid Column Name, Id1 because I had used the wrong id in my .HasForeignKey statement in my Entity Framework C# code. Once I changed it to match the .HasOne() object's id, the error was gone.

    0 讨论(0)
提交回复
热议问题