Questions tagged «c#»

C#(发音为“ See Sharp”)是由Microsoft开发的一种高级,静态类型的多范例编程语言。C#代码通常针对Microsoft的.NET系列工具和运行时,其中包括.NET Framework,.NET Core和Xamarin。使用此标记可解决有关用C#或C#正式规范编写的代码的问题。

7
如何从数据表中提取数据?
我有一个DataTable从SQL查询到本地数据库的数据,但是我不知道如何从中提取数据。主要方法(在​​测试程序中): static void Main(string[] args) { const string connectionString = "server=localhost\\SQLExpress;database=master;integrated Security=SSPI;"; DataTable table = new DataTable("allPrograms"); using (var conn = new SqlConnection(connectionString)) { Console.WriteLine("connection created successfuly"); string command = "SELECT * FROM Programs"; using (var cmd = new SqlCommand(command, conn)) { Console.WriteLine("command created successfuly"); SqlDataAdapter adapt = new SqlDataAdapter(cmd); …
78 c#  sql  ado.net 

3
System.Drawing.Image流C#
System.Drawing.Image我的程序中有一个。该文件不在文件系统中,而是保存在内存中。我需要从中创建一个流。我将如何去做呢?
78 c#  c#-3.0 

11
找不到命令“ dotnet ef”?
我在Arch VScode上使用.NET Core 2.0,并尝试使EF工具正常工作,但我不断收到该错误“找不到命令dotnet ef”。我只是到处看,所有建议都没有。因此,如果可以的话,请您提供帮助。 运行“ dotnet ef”的结果 [wasiim@wasiim-PC WebApiServerApp]$ dotnet ef --help Cannot find command 'dotnet ef', please run the following command to install dotnet tool install --global dotnet-ef [wasiim@wasiim-PC WebApiServerApp]$ dotnet tool list -g Package Id Version Commands --------------------------------------------------- dotnet-dev-certs 2.2.0 dotnet-dev-certs dotnet-ef 2.2.3 dotnet-ef [wasiim@wasiim-PC WebApiServerApp]$ 这是dotnet …


10
从WPF DataGrid复制粘贴数据时,OpenClipboard失败
我有一个使用datagrid的WPF应用程序。在我安装Visual Studio 2012和Blend + SketchFlow预览之前,该应用程序运行良好。现在,当我尝试使用Ctrl+ C(在任何应用程序中)将数据从网格复制到剪贴板时,出现以下异常: System.Runtime.InteropServices.COMException (0x800401D0): OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN)) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode, IntPtr errorInfo) at System.Windows.Clipboard.Flush() at System.Windows.Clipboard.CriticalSetDataObject(Object data, Boolean copy) at System.Windows.Controls.DataGrid.OnExecutedCopy(ExecutedRoutedEventArgs args) at System.Windows.Controls.DataGrid.OnExecutedCopy(Object target, ExecutedRoutedEventArgs args) at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e) at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, …


9
何时使用Cast或Convert
我很好奇,想知道有什么区别铸说的之间int相对于使用Convert.ToInt32()。使用它可以获得某种性能提升吗? 另外,每种情况都应用于哪些情况?目前,我更倾向于使用,Convert但我没有理由选择任何一种方式。在我看来,他们两个都实现了相同的目标。
77 c#  casting 

11
Asp.Net WebApi2启用CORS无法与AspNet.WebApi.Cors 5.2.3一起使用
我试图按照http://enable-cors.org/server_aspnet.html 上的步骤操作,以使我的RESTful API(使用ASP.NET WebAPI2实现)可以处理跨源请求(启用CORS)。除非我修改web.config,否则它将无法正常工作。 我安装了WebApi Cors依赖项: install-package Microsoft.AspNet.WebApi.Cors -ProjectName MyProject.Web.Api 然后在我App_Start的班级中WebApiConfig,如下所示: public static class WebApiConfig { public static void Register(HttpConfiguration config) { var corsAttr = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(corsAttr); var constraintsResolver = new DefaultInlineConstraintResolver(); constraintsResolver.ConstraintMap.Add("apiVersionConstraint", typeof(ApiVersionConstraint)); config.MapHttpAttributeRoutes(constraintsResolver); config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config)); //config.EnableSystemDiagnosticsTracing(); config.Services.Replace(typeof(ITraceWriter), new SimpleTraceWriter(WebContainerManager.Get<ILogManager>())); config.Services.Add(typeof(IExceptionLogger), new SimpleExceptionLogger(WebContainerManager.Get<ILogManager>())); config.Services.Replace(typeof(IExceptionHandler), new …

6
如何在C#for SqlServer中转义简单的SQL查询
我使用期望使用SQL字符串的API。我接受用户输入,对其进行转义并将其传递给API。用户输入非常简单。它要求列值。像这样: string name = userInput.Value; 然后,我构造一个SQL查询: string sql = string.Format("SELECT * FROM SOME_TABLE WHERE Name = '{0}'", name.replace("'", "''")); 这样够安全吗?如果不是,是否有一个简单的库函数使列值安全: string sql = string.Format("SELECT * FROM SOME_TABLE WHERE Name = '{0}'", SqlSafeColumnValue(name)); 该API使用SQLServer作为数据库。
77 c#  sql  sql-server 

9
String.Replace()与StringBuilder.Replace()
我有一个字符串,我需要用字典中的值替换标记。它必须尽可能高效。用string.replace进行循环只会消耗内存(字符串是不可变的,请记住)。因为StringBuilder.Replace()旨在用于字符串操作,所以它会更好吗? 我希望避免花费RegEx,但是如果这样可以提高效率,那就可以了。 注意:我不在乎代码的复杂性,只在乎它的运行速度和消耗的内存。 平均状态:长度255-1024个字符,字典中有15-30个键。
77 c#  .net  .net-4.0  replace 


4
首先使用实体​​框架代码-如何为生产数据库运行Update-Database
我想知道如何为生产数据库运行“ Update-Database”命令。 “ Update-Database”数据库可以在本地计算机上正常运行,但是如何使它用于生产数据? 因此,如果我对应用程序进行了更改,然后通过Visual Studio运行“发布”,那么对于代码方面的工作就很好了,但是如何对生产数据运行“ Update-Database”命令。 希望这个问题有道理... 谢谢,


10
在C#中获得字典最高价值的键的好方法
我正在尝试获取中的最大值的键Dictionary<string, double> results。 这是我到目前为止所拥有的: double max = results.Max(kvp => kvp.Value); return results.Where(kvp => kvp.Value == max).Select(kvp => kvp.Key).First(); 但是,由于这似乎效率不高,所以我想知道是否有更好的方法可以做到这一点。
77 c#  linq  dictionary  max 


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.