简体   繁体   中英

Why do I get Invalid Object Name for a SQL Server table, when the object is valid?

I am trying to execute an SQL query at runtime:

        SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader reader;
        cmd.CommandText = "SELECT * FROM dbo.Books_in_Storage WHERE author = 'myself'";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = connection;
        connection.Open();
        reader = cmd.ExecuteReader();
        connection.Close();

Execution reaches reader = cmd.ExecuteReader(); and gives me the error:

Invalid object name dbo.Books in Storage .

If I look at my SQL Server Object Explorer, I can see that dbo.Books in Storage is present: 在此处输入图片说明

Why is this happening?

在SQL Server中,您可以使用[]来查询带有空格的表,如下所示,但是,在选择数据库表名称时通常不包含空格。

SELECT * FROM [table name]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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