Saturday, January 30, 2010

Get Organization Connection string

Hi,
Here is the example to get Organization connection string.

//get connection to mscrm_config DB
string connectionString = "Data Source="ServerName;Initial Catalog=MSCRM_CONFIG;Integrated Security=SSPI";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
//Select DB details for specific organization
string sqlString = string.Format("select databasename,* from organization with (nolock) where uniquename = '{0}'", organizationName);
SqlCommand command = new SqlCommand(sqlString, conn);
SqlDataReader reader = command.ExecuteReader();
if (reader == null)
return null;
if (!reader.Read())
return null;
object rslt = reader.GetValue(0);
//return organization connection string
return rslt;

Enjoy,
Rami heleg