6
我已經在C#中構建了一個使用Microsoft.SqlServer.Management.Smo對象導出數據庫模式的簡單實用程序。這一直很好,直到我添加了全文索引。如何以編程方式導出SQL模式使用Microsoft.SqlServer.Management.Smo
當它導出用於創建全文索引的SQL時,它不包括在索引中定義的列。例如,假設我有一個名爲「食譜」的表,其中2列包含在名爲「RecipeName」「Description」的FT索引中。傾倒用我的工具架構生成以下SQL:
CREATE FULLTEXT INDEX ON [dbo].[Recipes]
KEY INDEX [PK_Recipes]ON ([ft], FILEGROUP [PRIMARY])
WITH (CHANGE_TRACKING = AUTO, STOPLIST = SYSTEM)
我希望轉儲是這樣(注意列):
CREATE FULLTEXT INDEX ON [dbo].[Recipes](
[Description] LANGUAGE [English],
[RecipeName] LANGUAGE [English]
)
KEY INDEX [PK_Recipes]ON ([ft], FILEGROUP [PRIMARY])
WITH (CHANGE_TRACKING = AUTO, STOPLIST = SYSTEM)
這裏是C#是從數據庫文件生成模式:
static void GenerateScript(string sourceDbPath, string destinationScriptPath)
{
try
{
string connString = string.Format(@"Data Source=.;AttachDbFilename={0};Integrated Security=True;User Instance=True", sourceDbPath);
SqlConnection sqlConn = new SqlConnection(connString);
ServerConnection serverConn = new ServerConnection(sqlConn);
Server server = new Server(serverConn);
Database database = server.Databases[sourceDbPath];
Transfer transfer = new Transfer(database);
ScriptingOptions options = new ScriptingOptions();
options.AppendToFile = false; // Overwrite file
options.ClusteredIndexes = true;
options.Indexes = true;
options.DriAll = true;
options.Triggers = true;
options.Bindings = true;
options.Default = true;
options.IncludeDatabaseContext = false;
options.IncludeHeaders = true;
options.FullTextIndexes = true;
options.SchemaQualify = true;
options.SchemaQualifyForeignKeysReferences = true;
options.ScriptSchema = true;
options.ScriptData = false;
options.ScriptDrops = false;
options.FileName = destinationScriptPath;
transfer.Options = options;
transfer.CopyAllFullTextCatalogs = true;
transfer.CopyAllFullTextStopLists = true;
transfer.CopyAllTables = true;
transfer.ScriptTransfer();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
Environment.Exit(-1);
}
}
任何人都可以發現我失蹤的東西嗎?
可能你必須單獨編寫列腳本?不過你不應該這樣做。 http://technet.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.fulltextindexcolumn.script(v=sql.100).aspx –
嘿,你最終開放源代碼的任何地方?我有興趣做類似的事情。 – Gili
不,對不起。我無法開源這段代碼。 – BitsEvolved