これは非常に些細なことだと思いますが、私には見えないようです。既存のデータベースにいくつかのビューとインデックスを設定するスクリプトがあります (この質問では、列とテーブルの詳細は重要ではありません...)
USE <dbname>
GO
CREATE VIEW [dbo].View1_
WITH SCHEMABINDING
AS
SELECT
[someTable].[Id]
FROM [dbo].[someTable]
WHERE [dbo].[someTable].[Str] != N'someString'
GO
CREATE UNIQUE CLUSTERED INDEX someTable_Index1
ON [dbo].View1_(ID)
GO
...
たとえば、SQL Server Management Studio から実行すると、これは正常に実行されます。しかし、特定の Web サイトが呼び出されたときに設定したいのです。上記のスクリプトを MVC4 プロジェクト内のフォルダーに保存し、次のスニペットを作成しました。
string connectionString = ConfigurationManager.ConnectionStrings["SomeConnectionString"].ToString();
string scriptCommand = System.IO.File.ReadAllText(Server.MapPath("~/Models/View1_IndexedViews.sql"));
using(SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = connection.CreateCommand())
{
connection.Open();
command.CommandText = scriptCommand;
try
{
command.ExecuteNonQuery();
}
catch(SqlException e)
{
}
}
}
しかし、その呼び出しをトリガーすると、ここで例外がスローされ、
"Incorrect syntax near 'GO'.\r\n'CREATE VIEW' must be the first statement in a query batch.\r\nIncorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
ここで何が欠けているのか、誰かが私にヒントを与えることができますか? 私はそのメッセージを知っていますCREATE VIEW must be the first statement
が、通常はGO
各の前に sを付けることで削除する必要がCREATE VIEW
ありWITH SCHEMABINDING
ます。
ヒント/ヘルプをいただければ幸いです。
ありがとう。