在我的代码中,我一直在重复这样的代码块:
var returnData = MyDb.ExecuteDataSet(
MyDb.GetStoredProcCommand("MyTable_Insert", columnOne, columnTwo, columnThree),
transaction
);
var returnId = (int)returnData.Tables[0].Rows[0]["INSERTED_ID"];
在我看来,这是一个相当大的代码块,需要重复很多次,但我不知道如何将它概括为一个方法,因为每个存储过程的参数数量不同。
(对我而言)显而易见的解决方案是将我的参数传递给数组中的函数,然后展开它们以调用 GetStoredProcCommand
,但我找不到执行此操作的方法:
private int CallStoredProcedure(string procName, List<dynamic> parameterValues) {
..
MyDb.GetStoredProcCommand(procName, expand parameterValues);
// ERROR: Obviously "expand" doesn't exist
..
}
我可以使用使用 AddInParameter
的更冗长的方式传递参数,但是我需要将类型信息与参数值一起传递:
private int CallStoredProcedure(string procName, IDictionary<DbType, IDictionary<string, string>> paramsKeyValuesByType) {
..
MyDb.AddInParameter(myProcCommand, param.Value.Key, param.Key, param.Value.Value);
..
}
但是我必须在调用方法之前构建 IDictionary
对象,这比我最初调用过程的方式更加冗长。
有人能想出一个优雅的解决方案吗?
最佳答案
您可以使用params
关键字:
private int CallStoredProcedure(string procName, params object[] parameterValues) {
..
MyDb.GetStoredProcCommand(procName, parameterValues);
..
}
关于c# - 在 C# 中使用可变数量的参数调用 GetStoredProcCommand?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12915902/