简体   繁体   English

C#中表名称的无效对象名称SQL异常

[英]invalid object name sql exception for table name in c#

I wrote this code, but it doesn't work. 我写了这段代码,但是没有用。 Gives Invalid object name 'Inventory'`. 给出无效的对象名称“库存”。 Any Suggestion? 有什么建议吗?

 using (SqlConnection cn = new SqlConnection())
        {
            cn.ConnectionString =
            @"Data Source=(local);Integrated Security=SSPI;" +
            "Initial Catalog=AutoLot";
            cn.Open();
            string strSQL = "Select * From Inventory";
            SqlCommand myCommand = new SqlCommand(strSQL, cn);
            // Obtain a data reader a la ExecuteReader().
            using (SqlDataReader mydatareader = mycommand.ExecuteReader())
            {}

签入数据库清单表是否存在,如果存在,则两个名称应该相同

If you've checked all your database settings, access rights and table definitions and still cannot get rid of that error? 如果您已经检查了所有数据库设置,访问权限和表定义,但仍然无法摆脱该错误?

It's because ASP.net cannot understand your SQL query - as shocking as that may sound. 这是因为ASP.net 无法理解您的SQL查询-听起来可能令人震惊。

I had the same problem, with UPDATE statements wanting to use aliases and not the usually expected dbo.myTable name definitions. 我有同样的问题, UPDATE语句想使用aliases而不是通常期望的dbo.myTable名称定义。

The simplest way to avoid this situation, where asp.net just doesn't parse SQL like SQL Server would (!!), is to place the code into a Stored Procedure . 避免这种情况的最简单方法是将代码放入Stored Procedure ,而asp.net不会像SQL Server(!!)那样解析SQL。

This was my SQL directly from SQL Server 2005 and it worked perfectly as is... 这是我直接来自SQL Server 2005的SQL,它按原样完美运行。

UPDATE tsa
SET tsa.ActionComplete = @ActionComplete,
    tsa.CompleteDate = GETDATE(),
    tsa.Notes = @ActionNotes
FROM blahblah
WHERE blahblah

But for what ever reason, the ASP.net parser inside the SqlDataSource control could not make sense of it (even though it ran perfectly inside the GUI's Test Query feature!). 但是无论出于何种原因, SqlDataSource控件中的ASP.net解析器都无法理解它(即使它在GUI的“ 测试查询”功能中完美运行!)。

I was getting 我正在

invalid object name in 'tsa' 'tsa'中的对象名称无效

So I placed the lot inside a Stored Procedure... 所以我把很多东西放在存储过程中...

USE [mydatabase];
GO
SET  ANSI_NULLS ON;
GO
SET  QUOTED_IDENTIFIER ON;
GO
ALTER PROCEDURE [dbo].[spTETupdateActions]
    @StudentID VARCHAR(10),
    @ActionComplete INT = 0,
    @ActionNotes VARCHAR(MAX),
    @TETmeetingID INT,
    @WeekNo INT,
    @TETID INT,
    @TETdate DATETIME,
    @ActionItem VARCHAR(MAX)
    WITH
    EXEC AS CALLER AS
UPDATE tsa
SET tsa.ActionComplete = @ActionComplete,
    tsa.CompleteDate = GETDATE(),
    tsa.Notes = @ActionNotes
FROM blahblah
WHERE blahblah
GO

And voila! 瞧! No more errors! 没有更多的错误! Why? 为什么? Because the parsing of the SQL is done inside SQL Server not ASP.net! 因为SQL的解析是在SQL Server而不是ASP.net中完成的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM