获取“最佳重载方法匹配...有一些无效参数”错误消息

时间:2012-02-24 12:31:30

标签: c# asp.net .net-3.5 asmx asp.net-3.5

我遇到以下代码问题:

public class ClientGroupDetails
{
    public DateTime Col2;
    public String Col3;
    public Int32 Col4;

    public ClientGroupDetails(DateTime m_Col2, String m_Col3, Int32 m_Col4)
    {
        Col2 = m_Col2;
        Col3 = m_Col3;
        Col4 = m_Col4;
    }

    public ClientGroupDetails() { }
}

[WebMethod()]
public List<ClientGroupDetails> GetClientGroupDetails(string phrase)
{
    var client_group_details = new List<ClientGroupDetails>();

    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
    {
        using (command = new SqlCommand(@"select col2, col3, col4 from table1 where col1 = @strSearch", connection))
        {
            command.Parameters.Add("@strSearch", SqlDbType.VarChar, 255).Value = phrase;

            connection.Open();
            using (reader = command.ExecuteReader())
            {
                int Col2Index = reader.GetOrdinal("col2");
                int Col3Index = reader.GetOrdinal("col3");
                int Col4Index = reader.GetOrdinal("col4");

                while (reader.Read())
                {
                    client_group_details.Add(new ClientGroupDetails(
                        reader.IsDBNull(Col2Index) ? (Nullable<DateTime>)null : (Nullable<DateTime>)reader.GetDateTime(Col2Index),
                        reader.IsDBNull(Col3Index) ? null : reader.GetString(Col3Index),
                        reader.GetInt32(Col4Index)));
                }
            }
        }
    }

    return client_group_details;
}
}

它给了我以下错误:

Compiler Error Message: CS1502: The best overloaded method match for 'Conflicts.ClientGroupDetails.ClientGroupDetails(System.DateTime, string, int)' has some invalid arguments
Line 184: client_group_details.Add(new ClientGroupDetails(

3 个答案:

答案 0 :(得分:2)

制作m_Col2 DateTime?Nullable<DateTime>

所以最后它会像这样

public class ClientGroupDetails
{
    public Nullable<DateTime> Col2;
    public String Col3;
    public Int32 Col4;

    public ClientGroupDetails(Nullable<DateTime> m_Col2, String m_Col3, Int32 m_Col4)
    {
        Col2 = m_Col2;
        Col3 = m_Col3;
        Col4 = m_Col4;
    }

    public ClientGroupDetails() { }
}

[WebMethod()]
public List<ClientGroupDetails> GetClientGroupDetails(string phrase)
{
    var client_group_details = new List<ClientGroupDetails>();
    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
    {
        using (command = new SqlCommand(@"select col2, col3, col4 from table1 where col1 = @strSearch", connection))
        {
            command.Parameters.Add("@strSearch", SqlDbType.VarChar, 255).Value = phrase;

            connection.Open();
            using (reader = command.ExecuteReader())
            {
                int Col2Index = reader.GetOrdinal("col2");
                int Col3Index = reader.GetOrdinal("col3");
                int Col4Index = reader.GetOrdinal("col4");

                while (reader.Read())
                {
                    client_group_details.Add(new ClientGroupDetails(
                        reader.IsDBNull(Col2Index) ? (Nullable<DateTime>)null : (Nullable<DateTime>)reader.GetDateTime(Col2Index),
                        reader.IsDBNull(Col3Index) ? (string)null : reader.GetString(Col3Index),
                        reader.GetInt32(Col4Index)));
                }
            }
        }
    }

    return client_group_details;
}
}

BTW:我建议你 1。使用POCO中的属性而不是公共实例变量,并为它们提供有意义的名称 2。使用小写的局部变量名< / p>

答案 1 :(得分:2)

ClientGroupDetails的构造函数没有Nullable<DateTime>(又名DateTime?) - 它需要持平DateTime。您需要更改ClientGroupDetails使用的内容,或者考虑默认值(可能是DateTime.MinValue)。

例如::

client_group_details.Add(new ClientGroupDetails(
    reader.IsDBNull(Col2Index) ? DateTime.MinValue : reader.GetDateTime(Col2Index),
    reader.IsDBNull(Col3Index) ? null : reader.GetString(Col3Index),
    reader.GetInt32(Col4Index)));

或者,保留现有的读者代码并更改POCO:

public class ClientGroupDetails
{
    public DateTime? Col2;
    ...    
    public ClientGroupDetails(DateTime? m_Col2, ...
    {
        Col2 = m_Col2;
        ...
    }   
    ...
}

或者 - 使用像&#34; dapper&#34;这将为你做到这一切:

var list = connection.Query<ClientGroupDetails>(
      @"select col2, col3, col4 from table1 where col1 = @phrase",
      new {phrase}).ToList();

答案 2 :(得分:1)

ctor签名为DataTime,您可以使用Nullable<DataTime>

进行调用
public ClientGroupDetails(DateTime m_Col2, String m_Col3, Int32 m_Col4)

你打电话给:

new ClientGroupDetails(
            reader.IsDBNull(Col2Index) ? (Nullable<DateTime>)null
        ...

将ctor更改为:

public ClientGroupDetails(DateTime? m_Col2, string m_Col3, int m_Col4)

财产:

public DateTime? Col2;

备注:

  • 您不必编写Int32,int是其“别名”。
  • Nullable<DateTime> =&gt;相同的事情DataTime?是别名。