:オブジェクトが有効な場合、SQL Serverテーブルの無効なオブジェクト名を取得するのはなぜですか?私は、実行時に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();
に到達し、私にエラーを与える:
Invalid object name
dbo.Books in Storage
.
私は、SQL Serverのオブジェクトエクスプローラを見れば、私はそれを見ることができますdbo.Books in Storage
が存在します:
どうしてですか?
'Books in Storage'と' Books_in_Storage'は同じではありません。スペースを使用しないようにテーブルの名前を変更することをお勧めします。 –
これは、表名やオブジェクト名にアンダースコアやスペース(特にスペース)を使用しない約3,980,437の理由の1つに過ぎません。 – Yuck
[dbo.Books_in_Storage]? - 下線も避けてください – Rob