【问题标题】:Creating 'connection string' in CRM SDK 4 programmatically以编程方式在 CRM SDK 4 中创建“连接字符串”
【发布时间】:2011-02-27 18:31:32
【问题描述】:

我正在编写应用程序,它使用 CRM SDK 4 连接到 CRM。在第一个版本中,我一直在使用 app.config 文件(“connectionStrings”部分)

<connectionStrings>
  <add name="mycrm" connectionString="Authentication Type=Integrated; Server=http://ServerName/OrganizationName;"/>
</connectionStrings>

和(在代码中):

DataContext ctx = new DataContext("mycrm");

而且效果很好。但是现在,应用程序应该让用户选择身份验证类型,输入服务器名称,用户ID,密码等。我已经创建了连接字符串,但我不知道如何使用它。 DataContext 和 CrmConnection 对象,AFAIK,只有带有 connectionString 名称(来自 app.config 文件)作为参数的构造函数。当我尝试使用我的 connectionString 时出现异常:

    System.ApplicationException was unhandled

Message="无法加载连接字符串名称'Authentication Type=Integrated; Server=http://ServerName/OrganizationName;'" 来源="Microsoft.Xrm.Client" 堆栈跟踪: 在 Microsoft.Xrm.Client.CrmConnection.GetConnectionStringFrom(字符串 connectionStringName) 在 Microsoft.Xrm.Client.CrmConnection..ctor(字符串 connectionStringName,字符串 connectionString) 在 Microsoft.Xrm.Client.CrmConnection..ctor(字符串连接字符串名称) 在 C:\Users\mrobaszynski\Desktop\PU\PaymentsUploader\MainWindows.xaml.cs:line 38 中的 ARP.EstateExtensions.PaymentsUploader.MainWindows..ctor(String connectionString) 在 C:\Users\mrobaszynski\Desktop\PU\PaymentsUploader\LoginWindow.xaml.cs:line 92 中的 ARP.EstateExtensions.PaymentsUploader.LoginWindow.bOK_Click(Object sender, RoutedEventArgs e) 在 System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs args,布尔 reRaised) 在 System.Windows.UIElement.RaiseEventImpl(DependencyObject 发件人,RoutedEventArgs 参数) 在 System.Windows.Controls.Button.OnClick() 在 System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 在 System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标) 在 System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs args,布尔 reRaised) 在 System.Windows.UIElement.ReRaiseEventAs(DependencyObject 发件人,RoutedEventArgs 参数,RoutedEvent newEvent) 在 System.Windows.UIElement.OnMouseUpThunk(对象发送者,MouseButtonEventArgs e) 在 System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标) 在 System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs args,布尔 reRaised) 在 System.Windows.UIElement.RaiseEventImpl(DependencyObject 发件人,RoutedEventArgs 参数) 在 System.Windows.UIElement.RaiseEvent(RoutedEventArgs 参数,布尔值信任) 在 System.Windows.Input.InputManager.ProcessStagingArea() 在 System.Windows.Input.InputManager.ProcessInput(InputEventArgs 输入) 在 System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) 在 System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd,InputMode 模式,Int32 时间戳,RawMouseActions 动作,Int32 x,Int32 y,Int32 轮) 在 System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& 处理) 在 System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& 处理) 在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& 处理) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象参数,布尔 isSingleParameter) 在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,委托回调,对象 args,布尔 isSingleParameter,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级,TimeSpan 超时,委托方法,对象参数,布尔 isSingleParameter) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(味精和味精) 在 System.Windows.Threading.Dispatcher.TranslateAndDispatchMessage(味精和味精) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 框架) 在 System.Windows.Application.RunInternal(窗口窗口) 在 C:\Users\mrobaszynski\Desktop\PU\PaymentsUploader\obj\Release\App.g.cs:line 0 中的 PaymentsUploader.App.Main() 在 System.AppDomain._nExecuteAssembly(程序集程序集,字符串 [] 参数) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:System.NullReferenceException Message="对象引用未设置为对象的实例。" 来源="Microsoft.Xrm.Client" 堆栈跟踪: 在 Microsoft.Xrm.Client.CrmConnection.GetConnectionStringFrom(字符串 connectionStringName) 内部异常:

【问题讨论】:

    标签: c# dynamics-crm dynamics-crm-4


    【解决方案1】:

    不,其他帖子是错误的 - 不要使用反射。执行此操作的正确方法是使用 CrmConnection.Parse(customConnectionString) 来构建 crm 连接。然后,您可以在构造函数中使用该连接构造数据上下文。

    var crm = new XrmDataContext(CrmConnection.Parse(customConnectionString));

    山·麦克阿瑟

    www.shanmcarthur.net

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      相关资源
      最近更新 更多