Tag: c# 4.0

SQL Server比较具有相同列的两个表行并返回更改的列

我想比较两个具有相同列的表: product – Id, Name, Description Temp_Product – Id, Name, Description 现在用户完成的更新将保存到Temp_Product 。 当管理员看到该产品的详细信息时,我需要显示用户完成的更改。 我想将两个表与查询进行比较,并返回已从Product更改为Temp_Product 。 请建议我更好的方法吗?

用C#调用cURL

我需要执行cURL请求以从Web获取CSV文件。 我之前从未使用过cURL,到目前为止,我通过谷歌看到的东西似乎没有意义: cURL调用需要如下所示: curl –user username:password http://somewebsite.com/events.csv 我试图使用的代码(我使用它来获取基本的auth XML文件) string URL = “http://somewebsite.com/events.csv”; string Username = “username”; string Password = “password”; WebClient req = new WebClient(); CredentialCache myCache = new CredentialCache(); myCache.Add(new Uri(URL), “Basic”, new NetworkCredential(Username, Password)); req.Credentials = myCache; string results = null; results = Encoding.UTF8.GetString(req.DownloadData(URL)); 这只是让我返回登录屏幕的html,因此身份validation不会发生。

在FrameWork级别捕获WPF中的exception

我正在开发一个轻量级的WPF MVVM框架,并且希望能够捕获未处理的exception,并且理想地从它们中恢复。 暂时忽略所有不这样做的好论据,我遇到以下情况: 如果我在App.xaml.cs的OnStartup方法中注册AppDomain.CurrentDomain.UnhandledException的处理程序,如下所示… App.xaml.cs: protected override void OnStartup(StartupEventArgs e) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(this.AppDomainUnhandledExceptionHandler); base.OnStartup(e); } void AppDomainUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs ea) { Exception e = (Exception)ea.ExceptionObject; // log exception } 然后在我的一个VM中引发exception,处理程序按预期调用。 到目前为止这么好,除了我无法使用这种方法恢复的事实,我所能做的就是记录exception,然后让CLR终止应用程序。 我真正想要做的是恢复,并将控制权返回给主框架VM。 (再次抛开反对这样做的动机)。 所以,做一些阅读,我决定在同一个地方为AppDomain.CurrentDomain.UnhandledException注册一个事件处理程序,这样代码现在看起来像这样…… protected override void OnStartup(StartupEventArgs e) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(this.AppDomainUnhandledExceptionHandler); this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(DispatcherUnhandledExceptionHandler); base.OnStartup(e); } void AppDomainUnhandledExceptionHandler(object sender, […]

编写扩展方法来调用控件的更好方法是什么?

我有这个通用函数来调用WinForm控件: public static void Invoke(this Control c, Action action) { if (c.InvokeRequired) c.TopLevelControl.Invoke(action); else action(); } 我正在考虑通过采用更严厉的限制来防止无意义的事情,使其变得更好,可能是这样的: button1.Invoke(() => list.Add(1)); 还可以进行冗余打字,例如: button1.Invoke(() => button1.Hide()); 因为我们已经指定this是button1 。 所以我做到了: public static void Invoke(this T c, Action action) where T : Control { if (c.InvokeRequired) c.TopLevelControl.Invoke(action); else action(c); } 现在我要打电话, button1.Invoke((c) => c.Hide()); 要么 button1.Invoke((c) => button1.Hide()); 现在我觉得即便如此,还有一些不仅仅是需要打字。 […]

ObjectDataSource找不到非generics方法

我有这个ASP.NET代码: 我的DAL代码: public DataTable GetBrokers(bool? hasImport=null) { SqlCommand cmd = new SqlCommand(“usp_GetBrokers”); if (hasImport.HasValue) cmd.Parameters.AddWithValue(“@hasImport”, hasImport); return FillDataTable(cmd, “brokers”); } 当表单加载时,我收到此错误: ObjectDataSource ‘srcBrokers’ could not find a non-generic method ‘GetBrokers’ that has no parameters. 这是导致问题的可选参数吗? 我该如何解决这个问题? 是否可以使用声明性ASP.NET代码具有可选参数?

如何在WPF中向后播放video?

我希望在WPF中顺利播放video。 我正在使用MediaElement播放video。 我读过这篇文章建议定期更改MediaElement.Position以模仿倒带行为。 我尝试使用代码来更改MediaElement.Position位置 private void Button_Click(object sender, RoutedEventArgs e) { mePlayer.Pause(); //Pause the media player first double m = 1 / frameRate; //Calculate the time for each frame double t = 120; //Total length of video in seconds mePlayer.Position = TimeSpan.FromMinutes(2); //Start video from 2 min while (t >= 60) //Check if time exceeds […]

StreamWriter中的Windows-1252编码返回ANSI编码文件

我尝试使用StreamWriter在Windows-1252中编码字符串。 输入字符串(dataString)以UTF8编码。 StreamWriter sw = new StreamWriter(@”C:\Temp\data.txt”, true, Encoding.GetEncoding(1252)); sw.Write(dataString); sw.Close(); 当我在Notepad ++中打开文件时,我得到一个ANSI文件。 我需要一个Windows-1252编码的文件。 有人有想法吗?

在C#中获取ACTUAL日期时间而不是系统日期时间

我正在构建的应用程序的一部分应仅在特定时间段内访问。 我知道我们可以使用C#中的DateTime属性获取当前系统时间。 如果我使用DateTime属性,那么用户可以随时更改系统时间并访问应用程序的一部分。 请告诉我如何在C#中获取ACTUAL当前时间。

创建配置节处理程序时发生错误

我有一个dot.NET 4.0 Web应用程序,其中定义了自定义部分: …. 在web.config文件的末尾我有相应的部分: …. ….. 每次我调用System.Configuration.ConfigurationManager.GetSection(“registrations”); 我得到以下exception: 为注册创建配置节处理程序时发生错误:给定的程序集名称或代码库无效。 (来自HRESULT的exception:0x80131047)(C:\ … \ web.config第13行) 我也在使用Unity,但不知道这是否与错误有关。 你以前遇到过这个错误吗? 我该如何解决? 我是否需要用其他东西替换IgnoreSectionHandler ?

IEnumerable的随机顺序

我有一个IEnumerable集合如下 var result1 = GetResult1() // Returns 2,4,5,6 我必须以随机方式处理元素并创建另一个集合,其结果如下: var result2 = GetResult2(result1) // Returns 2,4,5,6 in a random order. // An example output would be 4,6,2,5 in the resultant collection. 我通过以下方式完成此操作: var result1 = GetResult1(); var random = new Random(); var result2 = result1.OrderBy(order=>random.Next()); 然而,问题在于,如果我访问result2,result2中的元素会再次被洗牌,即如果我将result2的结果输出到控制台两次,则元素会再次变为juggled。 你能告诉我如何保持这种制服。 也就是说,一旦我处理了这个集合,它应该保持相同的方式。 我必须使用懒惰的评估,因为结果非常大。