为什么我得到“没有重载的方法需要两个参数”?

问题描述:

我有一个方法,一个DLL:为什么我得到“没有重载的方法需要两个参数”?

public static DataSet ExecuteDataset(string strcommandText,CommandType commandType,SqlParameter[] p) 
{ 
    CreateConnection(); 
    da = new SqlDataAdapter(strcommandText, con); 
    da.SelectCommand.CommandType = commandType; 
    da.SelectCommand.Parameters.AddRange(p); 
    ds = new DataSet(); 
    da.Fill(ds); 
    return ds; 
}  

而且我已经在另一个DLL写了一个方法:

public static DataSet GetDeptDetails() 
    { 
    string strcommandText = "sp_DeptDetails"; 
    return SqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure); 
    } 

在这里,我得到这个错误:

no overload for method takes two arguments.

我究竟做错了什么?

+2

那么我可以看到第三个参数'SqlParameter [] p',你错过了它们。 'ExecuteDataset'接受3个参数,并且没有超载有2个参数 – V4Vendetta

+0

SqlHelper.ExecuteDataset需要更多的两个参数。 – Muctadir

public static DataSet ExecuteDataset(string strcommandText,CommandType commandType,SqlParameter[] p=null) 
{ 
    CreateConnection(); 
    da = new SqlDataAdapter(strcommandText, con); 
    da.SelectCommand.CommandType = commandType; 
    if(p!=null) 
    { 
    da.SelectCommand.Parameters.AddRange(p); 
    } 
    ds = new DataSet(); 
    da.Fill(ds); 
    return ds; 
} 




public static DataSet GetDeptDetails() 
    { 
    string strcommandText = "sp_DeptDetails"; 
    return SqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure); 
    } 
+0

小心解释一下? –

+1

您在ExecuteDataset方法中有3个参数,并且您传递了其中2个参数。所以它给你编译错误。将缺省值提供给最后一个参数使其成为可选参数 – Patel

+0

感谢您的回复。但是,我认为我们可以在ExecuteDataset中给SqlParameter [] p作为可选参数,我们可以如何将它作为可选参数? –

你期待在

`public static DataSet ExecuteDataset(string strcommandText,CommandType commandType,SqlParameter[] p)` 
method. 

3参数但是,当你调用这个方法

return SqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure); 

所以,当你通过传递只有两个参数Sqlhelper调用相同的方法来发送只有两个参数对象查找另一个具有相同名称的方法,该方法具有导致此错误的两个参数。

您可以通过传递第三个参数或简单地将第三个参数设置为null来解决问题。

感谢您的回复。在上面的程序中,我们可以在ExecuteDataset Method参数的sqlhelper类中给出可选参数,或者我们可以给出默认参数。所以sqlparamter传递对其他方法来说不是强制性的,我们只有在需要时才能通过。