[英]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: 我正在尝试在运行时执行SQL查询:
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();
执行到达
reader = cmd.ExecuteReader();
and gives me the error: 并给我错误:
Invalid object name
dbo.Books in Storage
.dbo.Books in Storage
对象名称dbo.Books in Storage
无效。
If I look at my SQL Server Object Explorer, I can see that dbo.Books in Storage
is present: 如果查看我的SQL Server对象资源管理器,可以看到
dbo.Books in Storage
存在dbo.Books in Storage
中:
Why is this happening? 为什么会这样呢?
在SQL Server中,您可以使用[]来查询带有空格的表,如下所示,但是,在选择数据库表名称时通常不包含空格。
SELECT * FROM [table name]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.