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

我在一个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); } 

在这里,我收到此错误:

方法没有重载需要两个参数。

我究竟做错了什么?

 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); } 

你期待3个参数

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

但是,当您调用此方法时,您只发送两个参数

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

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

您可以通过传递第三个参数来解决问题,或者只是默认将第三个参数设置为null。

感谢回复人..在上面的程序中我们可以在ExecuteDataset中给出sqlhelper类的方法参数作为可选参数,或者我们可以给出默认参数。 所以sqlparamter传递对其他方法不是强制性的,我们只能在require时传递。