小编dav*_*mos的帖子

你能从SqlDataReader获取列名吗?

连接到数据库后,我可以获取在我的所有列中返回的列的名称SqlDataReader吗?

c# ado.net sqldatareader

265
推荐指数
7
解决办法
21万
查看次数

为什么Thread.Sleep如此有害

我经常看到它提到Thread.Sleep();不应该使用,但我不明白为什么会这样.如果Thread.Sleep();可能造成麻烦,是否有任何替代解决方案具有相同的安全性?

例如.

while(true)
{
    doSomework();
    i++;
    Thread.Sleep(5000);
}
Run Code Online (Sandbox Code Playgroud)

另一个是:

while (true)
{
    string[] images = Directory.GetFiles(@"C:\Dir", "*.png");

    foreach (string image in images)
    {
        this.Invoke(() => this.Enabled = true);
        pictureBox1.Image = new Bitmap(image);
        Thread.Sleep(1000);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# multithreading sleep

117
推荐指数
6
解决办法
17万
查看次数

从C#发送附带附件的电子邮件,附件在Thunderbird中作为第1.2部分到达

我有一个C#应用程序,它使用SMTP通过Exchange 2007服务器通过电子邮件发送Excel电子表格报告.这些适用于Outlook用户,但对于Thunderbird和Blackberry用户,附件已重命名为"第1.2部分".

我发现这篇文章描述了这个问题,但似乎没有给我一个解决方法.我无法控制Exchange服务器,因此无法在那里进行更改.我能在C#端做什么吗?我尝试过对身体使用短文件名和HTML编码,但两者都没有区别.

我的邮件发送代码就是这样:

public static void SendMail(string recipient, string subject, string body, string attachmentFilename)
{
    SmtpClient smtpClient = new SmtpClient();
    NetworkCredential basicCredential = new NetworkCredential(MailConst.Username, MailConst.Password);
    MailMessage message = new MailMessage();
    MailAddress fromAddress = new MailAddress(MailConst.Username);

    // setup up the host, increase the timeout to 5 minutes
    smtpClient.Host = MailConst.SmtpServer;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = basicCredential;
    smtpClient.Timeout = (60 * 5 * 1000);

    message.From = fromAddress;
    message.Subject = subject;
    message.IsBodyHtml = false;
    message.Body = body;
    message.To.Add(recipient);

    if …
Run Code Online (Sandbox Code Playgroud)

c# email smtp attachment

100
推荐指数
4
解决办法
28万
查看次数

将数据注释添加到由实体框架生成的类

我有实体框架生成的以下类:

public partial class ItemRequest
{
    public int RequestId { get; set; }
    //...
Run Code Online (Sandbox Code Playgroud)

我想把它变成一个必填字段

[Required]
public int RequestId { get;set; }
Run Code Online (Sandbox Code Playgroud)

但是,因为这是生成的代码,这将被消灭.我无法想象一种创建分部类的方法,因为属性是由生成的分部类定义的.如何以安全的方式定义约束?

c# entity-framework asp.net-mvc-4

92
推荐指数
4
解决办法
6万
查看次数

如何创建/编辑清单文件?

我有一个来自同事的代码(可能是从网上某处获得的),但是他出去度假,我需要将它添加到清单文件中

<?xml version="1.0" encoding="utf-8" ?> 
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" 
    xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" 
    xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <assemblyIdentity version="1.0.0.0" name="MyApplication" />
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="requireAdministrator" 
        uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>
</asmv1:assembly>  
Run Code Online (Sandbox Code Playgroud)

我对此比较陌生,所以任何帮助都会受到赞赏.谢谢

我正在使用Visual Studio 2010

.net c# manifest visual-studio-2010 visual-studio-2008

88
推荐指数
3
解决办法
12万
查看次数

使用Resharper自定义Brace格式

我正在使用Resharper 4.5,我在编写数组或对象初始化程序时需要自定义大括号格式.Resharper支持一些样式:

Gnu风格:

int[] array = new int[]  
                  {  
                      1, 2, 3  
                  }  
Run Code Online (Sandbox Code Playgroud)

但是我需要:

int[] array = new int[] 
{  
    1, 2, 3  
}  
Run Code Online (Sandbox Code Playgroud)

有没有办法自定义这个模板?

c# resharper indentation auto-indent

78
推荐指数
2
解决办法
7681
查看次数

在SQL LIKE子句中使用SqlParameter不起作用

我有以下代码:

const string Sql = 
    @"select distinct [name] 
      from tblCustomers 
      left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId  
      where (tblCustomer.Name LIKE '%@SEARCH%' OR tblCustomerInfo.Info LIKE '%@SEARCH%');";

using (var command = new SqlCommand(Sql, Connection))
{       
    command.Parameters.AddWithValue("@SEARCH", searchString);
    ...
}
Run Code Online (Sandbox Code Playgroud)

这不起作用,我也试过这个:

const string Sql = 
    @"select distinct [name] 
     from tblCustomers 
     left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId  
     where (tblCustomer.Name LIKE @SEARCH OR tblCustomerInfo.Info LIKE @SEARCH );";

using (var command = new SqlCommand(Sql, Connection))
{       
    command.Parameters.AddWithValue("@SEARCH", "'%" + searchString + "%'");
    ... …
Run Code Online (Sandbox Code Playgroud)

c# t-sql sql-server ado.net sql-like

65
推荐指数
3
解决办法
6万
查看次数

使用ConfigurationManager加载System.ServiceModel配置节

使用C#.NET 3.5和WCF,我试图在客户端应用程序中写出一些WCF配置(客户端连接到的服务器的名称).

显而易见的方法是使用ConfigurationManager加载配置部分并写出我需要的数据.

var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");
Run Code Online (Sandbox Code Playgroud)

似乎总是返回null.

var serviceModelSection = ConfigurationManager.GetSection("appSettings");
Run Code Online (Sandbox Code Playgroud)

完美的工作.

配置部分存在于App.config中,但由于某种原因ConfigurationManager拒绝加载该system.ServiceModel部分.

我想避免手动加载xxx.exe.config文件并使用XPath,但如果我不得不求助于我.看起来有点像黑客.

有什么建议?

.net c# xml wcf configurationmanager

60
推荐指数
5
解决办法
6万
查看次数

增加SQL命令的命令超时

我有一点问题,希望有人可以给我一些建议.我正在运行一个SQL命令,但它似乎需要这个命令大约2分钟才能返回数据,因为有很多数据.但是默认连接时间是30秒,如何增加它,并将其应用于此命令?

public static DataTable runtotals(string AssetNumberV, string AssetNumber1V)
{
    DataTable dtGetruntotals;

    try
    {
        dtGetruntotals = new DataTable("Getruntotals");

        //SqlParameter AssetNumber = new SqlParameter("@AssetNumber", SqlDbType.VarChar, 6);
        //AssetNumber.Value = AssetNumberV; 

        SqlParameter AssetNumber = new SqlParameter("@AssetNumber", SqlDbType.VarChar, 10);
        AssetNumber.Value = AssetNumberV;

        SqlParameter AssetNumber1 = new SqlParameter("@AssetNumber1", SqlDbType.VarChar, 10);
        AssetNumber1.Value = AssetNumber1V;

        SqlCommand scGetruntotals = new SqlCommand("EXEC spRunTotals @AssetNumber,@AssetNumber1 ", DataAccess.AssetConnection); 
        // scGetruntotals.Parameters.Add(AssetNumber);
        scGetruntotals.Parameters.Add(AssetNumber);
        scGetruntotals.Parameters.Add(AssetNumber1);

        SqlDataAdapter sdaGetruntotals = new SqlDataAdapter();
        sdaGetruntotals.SelectCommand = scGetruntotals;
        sdaGetruntotals.Fill(dtGetruntotals);

        return dtGetruntotals;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error Retriving totals Details: …
Run Code Online (Sandbox Code Playgroud)

c# sql ado.net connection-timeout

60
推荐指数
2
解决办法
18万
查看次数

SQL Server:将表达式转换为数据类型int的算术溢出错误

我收到了这个错误

msg 8115,level 16,state 2,line 18
将表达式转换为数据类型int的算术溢出错误.

使用此SQL查询

DECLARE @year VARCHAR(4);                       
DECLARE @month VARCHAR(2);                      

-- START OF CONFIGURATION SECTION                       
-- THIS IS THE ONLY SECTION THAT SHOULD BE MODIFIED                     
-- SET THE YEAR AND MONTH PARAMETERS                        

SET @year = '2013';                     
SET @month = '3';  -- 1 = January.... 12 = Decemeber.                       

-- END OF CONFIGURATION SECTION                     

DECLARE @startDate DATE                     
DECLARE @endDate DATE                       
SET @startDate = @year + '-' + @month + '-01 00:00:00';                     
SET @endDate = DATEADD(MONTH, 1, @startDate);                       

SELECT …
Run Code Online (Sandbox Code Playgroud)

sql t-sql sql-server

39
推荐指数
3
解决办法
18万
查看次数