以编程方式在CRM SDK 4中创建“连接字符串”

时间:2010-06-04 12:12:13

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

我正在编写应用程序,它使用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(String connectionStringName)       在Microsoft.Xrm.Client.CrmConnection..ctor(String connectionStringName,String connectionString)       在Microsoft.Xrm.Client.CrmConnection..ctor(String connectionStringName)       在C:\ Users \ mrobaszynski \ Desktop \ PU \ PaymentsUploader \ MainWindows.xaml.cs中的ARP.EstateExtensions.PaymentsUploader.MainWindows..ctor(String connectionString):第38行       在ARP:.EstateExtensions.PaymentsUploader.LoginWindow.bOK_Click(Object sender,RoutedEventArgs e)中的C:\ Users \ mrobaszynski \ Desktop \ PU \ PaymentsUploader \ LoginWindow.xaml.cs:第92行       at System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)       在System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)       在System.Windows.Controls.Button.OnClick()       在System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)       在System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标)       at System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)       在System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender,RoutedEventArgs args,RoutedEvent newEvent)       在System.Windows.UIElement.OnMouseUpThunk(Object sender,MouseButtonEventArgs e)       在System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标)       at System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)       在System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)       在System.Windows.UIElement.RaiseEvent(RoutedEventArgs args,布尔值信任)       在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&amp; handling)       在System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&amp; processed)       在MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&amp; handling)       在MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)       在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,布尔isSingleParameter)       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Object args,Boolean isSingleParameter,Delegate catchHandler)       在System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Boolean isSingleParameter)       在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)       在MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&amp; msg)       在System.Windows.Threading.Dispatcher.TranslateAndDispatchMessage(MSG&amp; msg)       在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)       在System.Windows.Application.RunInternal(窗口窗口)       在C:\ Users \ mrobaszynski \ Desktop \ PU \ PaymentsUploader \ obj \ Release \ App.g.cs中的PaymentsUploader.App.Main():第0行       在System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)       在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()       在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)       在System.Threading.ThreadHelper.ThreadStart()   InnerException:System.NullReferenceException       Message =“对象引用未设置为对象的实例。”       来源= “Microsoft.Xrm.Client”       堆栈跟踪:             在Microsoft.Xrm.Client.CrmConnection.GetConnectionStringFrom(String connectionStringName)       的InnerException:

1 个答案:

答案 0 :(得分:5)

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

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

Shan McArthur

www.shanmcarthur.net