據我所知,沒有辦法從組織服務獲取數據庫服務器名稱。您可以使用部署服務(請參閱http://worldofdynamics.blogspot.com/2011/12/dynamics-crm-2011-c-code-for-retrieving.html)檢索它,但除非您是部署管理員 - 即使是檢索,該服務也沒有用處。
我知道,查看數據庫信息的唯一方法是將視圖/高級查找導出到動態電子表格 - 連接信息嵌入在Excel XML中。
我開發了一個靜態方法,給定Org Url,將下載一個動態電子表格並解析出連接字符串。
/// <summary>
/// Given a Dynamics CRM Org URL, retrieve the Database Connection string
/// </summary>
/// <param name="crmOrgUrlBase"></param>
/// <returns></returns>
private static string GetCrmDatabaseConnectionString(string crmOrgUrlBase)
{
string cleanurl = crmOrgUrlBase.ToLowerInvariant().Trim().Replace("/xrmservices/2011/organization.svc", "");
{
int mainPos = cleanurl.IndexOf("/main.aspx");
if (mainPos > 0)
{
cleanurl = cleanurl.Substring(0, mainPos);
}
}
string requestPayload = @"xdpi=96&exportType=list&useSqlQuery=1&fetchXml=%3Cfetch+distinct%3D%22false%22+no-lock%3D%22false%22+mapping%3D%22logical%22+page%3D%221%22+count%3D%2250%22+returntotalrecordcount%3D%22true%22%3E%3Centity+name%3D%22systemuser%22%3E%3Cattribute+name%3D%22systemuserid%22%2F%3E%3Cattribute+name%3D%22fullname%22%2F%3E%3Cattribute+name%3D%22fullname%22%2F%3E%3Corder+attribute%3D%22fullname%22+descending%3D%22false%22%2F%3E%3C%2Fentity%3E%3C%2Ffetch%3E%0D%0A&layoutXml=%3Cgrid+name%3D%22excelGrid%22+select%3D%220%22+icon%3D%220%22+preview%3D%220%22%3E%3Crow+name%3D%22result%22+id%3D%22systemuserid%22%3E%3Ccell+name%3D%22fullname%22+width%3D%22100%22%2F%3E%3C%2Frow%3E%3C%2Fgrid%3E%0D%0A";
string url = cleanurl + "/_grid/print/export_live.aspx";
string response = null;
using (var wc = new System.Net.WebClient())
{
wc.UseDefaultCredentials = true;
wc.Headers.Add("Accept-Encoding", "gzip, deflate");
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.Headers.Add("DNT", "1");
response = wc.UploadString(url, requestPayload);
}
var xe = System.Xml.Linq.XElement.Parse(response);
System.Xml.Linq.XNamespace nn = "urn:schemas-microsoft-com:office:spreadsheet";
System.Xml.Linq.XNamespace nn2 = "urn:schemas-microsoft-com:office:excel";
var connectionEl = xe.Element(nn + "Worksheet").Element(nn2 + "QueryTable").Element(nn2 + "QuerySource").Element(nn2 + "Connection");
string rawString = connectionEl.Value;
Regex rex = new Regex("SERVER=([^;]+);DATABASE=([^;]+)", RegexOptions.Compiled);
var m = rex.Match(rawString);
string server = m.Groups[1].Value;
string database = m.Groups[2].Value;
return "DATA SOURCE=" + server +";INITIAL CATALOG=" + database + ";Integrated Security=SSPI";
}
它使用針對用戶一個簡單的查詢 - 查詢其實並不重要,因爲我們需要的是數據庫連接字符串。
我原來的博客文章是在這裏: http://www.shulerent.com/2015/01/19/get-the-sql-server-and-database-name-from-dynamics-crm/
服務器名稱是癥結所在。我處理場所安裝,並使用集成身份驗證,因此這些都不是(至少對我而言)。 – 2011-06-17 13:09:01