Navigation

Search

Categories

 
 

On this page

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 21
This Year: 0
This Month: 0
This Week: 0
Comments: 29

Sign In
Pick a theme:

 Tuesday, April 04, 2006
Tuesday, April 04, 2006 3:24:03 AM (GMT Standard Time, UTC+00:00) ( )

When Microsoft.BizTalk.ExplorerOM namespace is used to set/get the artifacts of BTS, the first property that you need to set is BtsCatalogExplorer.ConnectionStringBtsCatalogExplorer 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.

Wednesday, May 30, 2007 2:14:46 PM (GMT Standard Time, UTC+00:00)
I will explore this more fully.
Wednesday, May 30, 2007 2:15:40 PM (GMT Standard Time, UTC+00:00)
This is very nice.
Wednesday, May 30, 2007 2:16:16 PM (GMT Standard Time, UTC+00:00)
This will serve as a great start.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview