When Microsoft.BizTalk.ExplorerOM namespace is used to set/get the artifacts of BTS, the first property that you need to set is BtsCatalogExplorer.ConnectionString. BtsCatalogExplorer uses this property to connect to bts configuration database (BizTalkMgmtDb).
Many people save this connection string in a configuration file (.config) or directly in the code. For example:
BtsCatalogExplorer catalog = new BtsCatalogExplorer();
catalog.ConnectionString = System.Configuration.Settings.AppSettings["BtsCofigDatabase"];
- or -
catalog.ConnectionString = "server=SQLServer;database=BizTalkMgmtDb;integrated security=true";
This code is not reusable or may be it'll need administration to set the value in the config file when the solution is installed in another bts server.
You can get the connection string dynamically using WMI or Windows Registry to delete this issue. I developed a little helper class (see code below) with one static method that return the connection string of the configuration database (BizTalkMgmtDb). Now, you don't need use any config file and your code'll be reusable. For example:
BtsCatalogExplorer catalog = new BtsCatalogExplorer();
catalog.ConnectionString = BtsHelper.BtsConfigurationDatabase.GetConnectionString();
Code: BtsHelper Class
using System;
using System.Data.SqlClient;
using System.Management;
using Microsoft.Win32;
namespace BtsHelper
{
public class BtsConfigurationDatabase
{
private string _database;
private string _server;
private static BtsConfigurationDatabase _btsConfig = null;
private BtsConfigurationDatabase()
{
this._server = string.Empty;
this._database = string.Empty;
}
private bool GetUsingWMI()
{
bool regFounded = false;
this._server = string.Empty;
this._database = string.Empty;
try
{
using (ManagementObjectSearcher searcherGroupSetting = new ManagementObjectSearcher())
{
searcherGroupSetting.Scope = new ManagementScope(@"root\MicrosoftBizTalkServer");
searcherGroupSetting.Query = new SelectQuery("select * from MSBTS_GroupSetting");
using (ManagementObjectCollection.ManagementObjectEnumerator enumGroupSetting = searcherGroupSetting.Get().GetEnumerator())
{
while (enumGroupSetting.MoveNext())
{
ManagementObject groupSetting = (ManagementObject)enumGroupSetting.Current;
this._server = groupSetting["MgmtDbServerName"] as string;
this._database = groupSetting["MgmtDbName"] as string;
regFounded = true;
break;
}
return regFounded;
}
}
}
catch (Exception) { }
return regFounded;
}
private bool GetUsingRegistry()
{
bool regFounded = false;
this._server = string.Empty;
this._database = string.Empty;
using (RegistryKey keyBts = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\BizTalk Server\3.0\Administration"))
{
if (keyBts == null)
return regFounded;
this._server = keyBts.GetValue("MgmtDBServer") as string;
if (this._server == null)
this._server = string.Empty;
this._database = keyBts.GetValue("MgmtDBName") as string;
if (this._database == null)
this._database = string.Empty;
regFounded = true;
}
return regFounded;
}
public static string GetConnectionString()
{
if (_btsConfig == null)
_btsConfig = new BtsConfigurationDatabase();
if (!_btsConfig.GetUsingWMI())
_btsConfig.GetUsingRegistry();
if ((_btsConfig._server == string.Empty) || (_btsConfig._database == string.Empty))
throw new ApplicationException("Error when get the connection string!");
SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
sb.DataSource = _btsConfig._server;
sb.InitialCatalog = _btsConfig._database;
sb.IntegratedSecurity = true;
return sb.ToString();
}
}
}
** Don't forget reference the System.Management.dll assembly to build the class.