2015-10-16 73 views
2

我有一個db版本控制應用程序來更新部署的數據庫。它幾乎可以和所有的SQL語句一起運行,但是當我決定使用FullText Search和生成的腳本來使用像下面這樣的語句時,它就會崩潰。CREATE FULLTEXT INDEX/CATALOG語句不能在用戶事務中使用

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

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(); 

如何運行我的劇本沒有一個事務中執行SQL文件?

+2

如果我瞭解清楚你想,只是註釋此行是什麼:cmd.Transaction =萬億; –

+0

@Keith抱歉,編輯問題時輸入錯誤 –

回答

2

我修改我的代碼來執行腳本

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();