【问题标题】:CREATE FULLTEXT INDEX/CATALOG statement cannot be used inside a user transactionCREATE FULLTEXT INDEX/CATALOG 语句不能在用户事务中使用
【发布时间】:2015-10-16 14:26:37
【问题描述】:

我有一个数据库版本控制应用程序来更新已部署的数据库。它几乎可以在所有 SQL 语句中顺利运行,但是当我决定使用全文搜索并生成脚本来使用如下语句时,它就坏了。

CREATE FULLTEXT CATALOG [ProductCatalog]

--Create fulltext index on Product
CREATE FULLTEXT INDEX ON [tblProduct] KEY INDEX [PK_Catalog]
    ON ([ProductCatalog]) WITH (CHANGE_TRACKING AUTO)
ALTER FULLTEXT INDEX ON [tblProduct] ADD ([ProductName] LANGUAGE [English])
ALTER FULLTEXT INDEX ON [tblProduct] ENABLE

我使用以下代码在 C# SqlTransaction 中执行 SQL 文件

SqlConnection cnn = new SqlConnection("cstring");
SqlTransaction trn = cnn.BeginTransaction();

cnn.Open();

using (SqlCommand cmd = cnn.CreateCommand())
{
    cmd.Connection = cnn;       
    cmd.Transaction = trn;

    cmd.CommandText = SQLScript;
    cmd.CommandType = CommandType.Text;
    cmd.ExecuteNonQuery();
}

trn.Commit();        
cnn.Close();

如何在没有事务的情况下运行我的脚本?

【问题讨论】:

  • 如果我清楚地了解您的尝试,请评论此行:cmd.Transaction = trn;
  • @Keith 很抱歉在编辑问题时输入错误

标签: c# sql-server tsql transactions full-text-search


【解决方案1】:

我已经修改了我的代码来执行脚本

SqlConnection cnn = new SqlConnection("cstring");
SqlTransaction trn = null;

cnn.Open();

using (SqlCommand cmd = cnn.CreateCommand())
{
    cmd.Connection = cnn;

    if(!SQLScript.Contains("FULLTEXT"))
    {
        trn = cnn.BeginTransaction();
        cmd.Transaction = trn;
    }        

    cmd.CommandText = SQLScript;
    cmd.CommandType = CommandType.Text;
    cmd.ExecuteNonQuery();
}

if(trn!=null)
    trn.Commit();

cnn.Close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-21
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多