![](/img/trans.png)
[英]Invalid object name when using Entity Framework to read SQL Server table
[英]Why do I get Invalid Object Name for a SQL Server table, when the object is valid?
我正在嘗試在運行時執行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();
執行到達reader = cmd.ExecuteReader();
並給我錯誤:
dbo.Books in Storage
對象名稱dbo.Books in Storage
無效。
如果查看我的SQL Server對象資源管理器,可以看到dbo.Books in Storage
存在dbo.Books in Storage
中:
為什么會這樣呢?
在SQL Server中,您可以使用[]來查詢帶有空格的表,如下所示,但是,在選擇數據庫表名稱時通常不包含空格。
SELECT * FROM [table name]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.