我想创建一个接受任意数量参数的类和函数 (它也可以是一个数组)。
我想要实现的是,我想传递存储过程所需的所有参数及其数据类型,它应该将它们作为SqlParameter
数组返回
DAL CODE
public DataSet createProblemStatement()
{
DataSet createProblemStatement = SqlHelper.ExecuteDataset(CCSVas.Constants.ConnectionString.DBConnection, "usp_ProblemStatements_Add", ProblemStatement.createProblemStatement("d", "d", "d", "d", "d"));
return createProblemStatement;
}
将分配值的功能
public static SqlParameter[] createProblemStatement(string strProblemTitle, string strProblemOwner, string strBusinessPurpose, string strProblemDescription, string strAssociateId) {
SqlParameter ProblemTitle = new SqlParameter();
ProblemTitle.DbType = DbType.String;
ProblemTitle.Value = strProblemTitle;
ProblemTitle.ParameterName = "@strProblemTitle";
SqlParameter ProblemOwner = new SqlParameter();
ProblemOwner.DbType = DbType.String;
ProblemOwner.Value = strProblemOwner;
ProblemOwner.ParameterName = "@strProblemOwner";
SqlParameter BusinessPurpose = new SqlParameter();
BusinessPurpose.DbType = DbType.String;
BusinessPurpose.Value = strBusinessPurpose;
BusinessPurpose.ParameterName = "@strBusinessPurpose";
SqlParameter ProblemDescription = new SqlParameter();
ProblemDescription.DbType = DbType.String;
ProblemDescription.Value = strBusinessPurpose;
ProblemDescription.ParameterName = "@strProblemDescription";
SqlParameter AssociateId = new SqlParameter();
AssociateId.DbType = DbType.String;
AssociateId.Value = strBusinessPurpose;
AssociateId.ParameterName = "@strAssociateId";
SqlParameter[] createProblemStatement = { ProblemTitle, ProblemOwner, BusinessPurpose, ProblemDescription, AssociateId };
return createProblemStatement;
}
我对此功能的问题是,这只接受已定义的参数且不可重复使用
答案 0 :(得分:2)
使用 params 关键字。通过使用params关键字,您可以指定一个带有可变数量参数的方法参数。
答案 1 :(得分:2)
如果您使用的是Dot net Framework 4.0+,请使用dynamic
其他方式使用object
。
以下是代码段。
public static SqlParameter[] createProblemStatement(params dynamic[] test)
{
var lstParam = new List<SqlParameter>();
foreach (var item in test)
{
var paramType = item.GetType();
if (paramType == typeof(string))
{
//Do something.
lstParam.Add(obj);
}
else if (paramType == typeof(int))
{
//Do something.
lstParam.Add(obj);
}
}
return lstParam.ToArray();
}
答案 2 :(得分:2)
使用params
关键字获取&#34;不确定长度&#34;功能然后只是通过SqlParameters
public DataSet CreateProblemStatement(params SqlParamter[] paremeters)
{
DataSet createProblemStatement = SqlHelper.ExecuteDataset(CCSVas.Constants.ConnectionString.DBConnection, "usp_ProblemStatements_Add", paremeters);
return createProblemStatement;
}
你会这样称呼:
var problemTitle = new SqlParameter("@strProblemTitle", "Some Title");
var problemOwner = new SqlParameter("@strProblemOwner", "Owner Name");
...
var result = CreateProblemStatement(problemTitle, problemOwner, businessPurpose, problemDescription, associateId);
答案 3 :(得分:2)
您可以创建一个帮助程序类,使您可以以受控方式构造Parameter数组,而不会遇到每种情况的方法实现。
首先为List
的Extension方法创建一个类// helper for Adding parameters to a list
public static class SqlParameters
{
// or have strongly typed overloads
public static List<SqlParameter> Add(
this List<SqlParameter> list,
string paramName,
object value)
{
var p =new SqlParameter(
paramName,
Map(value.GetType()));
p.Value = value;
list.Add(p);
return list;
}
private static DbType Map(Type type)
{
DbType dbtype = DbType.Object;
if (type == typeof(System.String))
{
dbtype = DbType.String;
}
if (type == typeof(System.Int32))
{
dbtype = DbType.Int32;
}
return dbtype;
}
}
然后在Builder中使用它作为参数:
// builder
public class ProblemStatement
{
List<SqlParameter> list = new List<SqlParameter>();
// single parameter
public ProblemStatement Title(string value)
{
list.Add("strProblemTitle", value);
return this;
}
public ProblemStatement Owner(string value)
{
list.Add("strProblemOwner", value);
return this;
}
public ProblemStatement Description(string value)
{
list.Add("strProblemOwner", value);
return this;
}
// a couple of parameters together at once...
public ProblemStatement AddProblem(string title,
string owner,
string description)
{
return Title(title)
.Owner(owner)
.Description(description); // re-use methods
}
public SqlParameter[] ToArray()
{
return list.ToArray();
}
}
构建器的用法:
DataSet createProblemStatement = SqlHelper.ExecuteDataset(
CCSVas.Constants.ConnectionString.DBConnection,
"usp_ProblemStatements_Add",
new ProblemStatement().Title("d")
.Owner("d")
.Description("d")
.ToArray());
return createProblemStatement;
请注意,您可以在构建器中定义任意数量的参数,而无需指定和/或使用它们。