我想这是一件很小的事情,但我似乎没有看到它。我有一个脚本,可以在已经存在的数据库中设置一些视图和索引(列和表的详细信息对于这个问题应该无关紧要......)
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 运行它,这将执行得很好。但是我希望在调用某个网站时进行设置;我将上面的脚本保存在 MVC4-Project 内的文件夹中,并生成了以下代码段:
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
在 each 之前通过前面的 s来消除CREATE VIEW
它,这条消息似乎也表明它不喜欢WITH SCHEMABINDING
,但为什么会这样呢?
任何提示/帮助表示赞赏!
谢谢。