当前位置: 首页 > 编程语言 > C#

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

时间:2023-04-10 22:01:34 C#

为什么我会得到“Nooverloadedmethodtakestwoarguments”?我在DLL中有一个方法:publicstaticDataSetExecuteDataset(stringstrcommandText,CommandTypecommandType,SqlParameter[]p){CreateConnection();da=newSqlDataAdapter(strcommandText,con);da.SelectCommand.CommandType=commandType;哒。SelectCommand.Parameters.AddRange(p);ds=新数据集();da.Fill(ds);返回ds;我在另一个DLL中写了一个方法:publicstaticDataSetGetDeptDetails(){stringstrcommandText="sp_DeptDetails";返回SqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure);在这里我得到这个错误:方法没有重载需要两个参数。我究竟做错了什么?publicstaticDataSetExecuteDataset(stringstrcommandText,CommandTypecommandType,SqlParameter[]p=null){CreateConnection();da=newSqlDataAdapter(strcommandText,con);da.SelectCommand.CommandType=commandType;如果(p!=null){da.SelectCommand.Parameters.AddRange(p);}ds=新数据集();da.Fill(ds);返回ds;}publicstaticDataSetGetDeptDetails(){stringstrcommandText="sp_DeptDetails";返回SqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure);}您期望来自`publicstaticDataSetExecuteDataset(stringstrcommandText,CommandTypecommandType,SqlParameter[]p)`方法的3个参数。然而,当你调用这个方法时,你只发送两个参数returnSqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure);因此,当您通过传递两个参数调用相同的方法时,只有Sqlhelper对象会查找另一个具有相同名称的方法,该方法具有导致此错误的两个参数。你可以通过传递第三个参数来解决这个问题,或者直接将第三个参数默认为null。感谢大家的回复。在上面的程序中,我们可以将ExecuteDataset中的sqlhelper类的方法参数作为可选参数,或者我们可以提供默认参数。所以sqlparamter传递对于其他方法不是必须的,我们只有在require的时候才可以传递。以上是C#学习教程:为什么会出现“Nooverloadedmethodtakestwoparameters”?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢