C# 为什么我得到“方法没有重载需要两个参数”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19042757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting "no overload for method takes two arguments"?
提问by Developer
I have a method in one DLL:
我在一个 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;
}
And I have written a method in another DLL:
我在另一个 DLL 中编写了一个方法:
public static DataSet GetDeptDetails()
{
string strcommandText = "sp_DeptDetails";
return SqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure);
}
Here, I'm getting this error:
在这里,我收到此错误:
no overload for method takes two arguments.
方法没有重载需要两个参数。
What am I doing wrong?
我究竟做错了什么?
回答by Mr.LamYahoo
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);
}
回答by Mohit
You are expecting 3 parameter in
你期待 3 个参数
`public static DataSet ExecuteDataset(string strcommandText,CommandType commandType,SqlParameter[] p)`
method.
But you send only two parameter when you call this method
但是当你调用这个方法时你只发送了两个参数
return SqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure);
So when you call same method by passing two parameters only Sqlhelper
object looking for an another method with same name having two parameter which cause this error.
因此,当您通过传递两个参数调用相同的方法时,只会Sqlhelper
寻找另一个具有两个参数的同名方法,这会导致此错误。
you can resolve problem by passing third parameter or simply set third parameter as null by default.
您可以通过传递第三个参数或简单地将第三个参数默认设置为 null 来解决问题。
回答by Developer
Thanks for Reply Guys.. In above program we can give in sqlhelper class in ExecuteDataset Method parameters as optional parameters or we can give as default parameters. so that sqlparamter passing is not mandatory to other method, we can pass only if require.
感谢您的回复。在上面的程序中,我们可以在 ExecuteDataset 方法中的 sqlhelper 类中提供参数作为可选参数,也可以作为默认参数提供。这样 sqlparamter 传递不是其他方法的强制性要求,我们只能在需要时传递。