C# 创建';连接字符串';以编程方式在CRM SDK 4中

C# 创建';连接字符串';以编程方式在CRM SDK 4中,c#,dynamics-crm,dynamics-crm-4,C#,Dynamics Crm,Dynamics Crm 4,我正在编写应用程序,它使用CRM SDK 4连接到CRM。在第一个版本中,我一直在使用app.config文件(“ConnectionString”部分) 而且效果很好。但现在,应用程序应该允许用户选择身份验证类型、类型服务器名称、用户id、密码等。我已经创建了连接字符串,但我不知道如何使用它。DataContext和CrmConnection对象AFAIK只有connectionString名称(来自app.config文件)作为参数的构造函数。当我尝试使用connectionString时,

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

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

    System.ApplicationException was unhandled
Message=“无法加载连接字符串名称‘身份验证类型=集成;服务器=;’” Source=“Microsoft.Xrm.Client” 堆栈跟踪: 位于Microsoft.Xrm.Client.CrmConnection.GetConnectionStringFrom(String connectionStringName) 在Microsoft.Xrm.Client.CrmConnection..ctor(字符串连接字符串名称,字符串连接字符串) 在Microsoft.Xrm.Client.CrmConnection..ctor(字符串连接字符串名) 在C:\Users\mrobaszynski\Desktop\PU\PaymentsUploader\MainWindows.xaml.cs中的ARP.EstateExtensions.PaymentsUploader.MainWindows..ctor(字符串连接字符串)处:第38行 在ARP.EstateExtensions.PaymentsUploader.LoginWindow.bOK_中,单击C:\Users\mrobaszynski\Desktop\PU\PaymentsUploader\LoginWindow.xaml.cs:第92行中的(对象发送方,路由目标) 在System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs参数,布尔值重新调高) 位于System.Windows.UIElement.RaiseEventImpl(DependencyObject发送方,RoutedEventTargets参数) 在System.Windows.Controls.Button.OnClick()中 在System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 位于System.Windows.RouteDevenTargets.InvokeHandler(委托处理程序,对象目标) 在System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs参数,布尔值重新调高) 在System.Windows.UIElement.ReraiseEvents(DependencyObject发送方、RoutedEventArgs参数、RoutedEvent newEvent) 在System.Windows.UIElement.OnMouseUpThunk(对象发送器,MouseButtonEventArgs e) 位于System.Windows.RouteDevenTargets.InvokeHandler(委托处理程序,对象目标) 在System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs参数,布尔值重新调高) 位于System.Windows.UIElement.RaiseEventImpl(DependencyObject发送方,RoutedEventTargets参数) 位于System.Windows.UIElement.RaiseEvent(RoutedEventArgs,布尔可信) 在System.Windows.Input.InputManager.ProcessStaginArea()中 位于System.Windows.Input.InputManager.ProcessInput(InputEventArgs输入) 位于System.Windows.Input.InputProviderSite.ReportInput(InputReport Input 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和handled) 在System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam、Boolean和handled) 在MS.Win32.HwndWrapper.WndProc(IntPtr-hwnd、Int32-msg、IntPtr-wParam、IntPtr-lParam、Boolean和handled) 在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)上 位于System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调、对象参数、布尔isSingleParameter) 位于System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源、委托回调、对象参数、布尔isSingleParameter、委托catchHandler) 位于System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority优先级、TimeSpan超时、委托方法、对象参数、布尔isSingleParameter) 位于MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam) 位于MS.Win32.unsafentivemethods.DispatchMessage(MSG&MSG) 在System.Windows.Threading.Dispatcher.TranslateAndDispatchMessage(MSG&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.\u nexecutestAssembly(程序集,字符串[]args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 位于System.Threading.ThreadHelper.ThreadStart()处 InnerException:System.NullReferenceException Message=“对象引用未设置为对象的实例。” Source=“Microsoft.Xrm.Client” 堆栈跟踪: 位于Microsoft.Xrm.Client.CrmConnection.GetConnectionStringFrom(String connectionStringName)
InnerException:

否,另一篇文章是错误的-不要使用反射。正确的方法是使用CrmConnection.Parse(customConnectionString)构建crm连接。然后可以在构造函数中使用该连接构造数据上下文

var crm=newxrmdatacontext(CrmConnection.Parse(customConnectionString))

珊麦克阿瑟


www.shanmarthur.net

不,另一篇文章是错误的-不要使用反射。正确的方法是使用CrmConnection.Parse(customConnectionString)构建crm连接。然后可以在构造函数中使用该连接构造数据上下文

var crm=newxrmdatacontext(CrmConnection.Parse(customConnectionString))

珊麦克阿瑟

www.shanmarthur.net

DataContext ctx = new DataContext("mycrm");
    System.ApplicationException was unhandled