这是我的方法:
public void EjecutarGuardar(string ProcedimientoAlmacenado, object[] Parametros)
{
SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand Command = Connection.CreateCommand();
Command.CommandText = ProcedimientoAlmacenado;
Command.CommandType = CommandType.StoredProcedure;
foreach (object X in Parametros)
{
Command.Parameters.Add(X);
}
Connection.Open();
Command.ExecuteNonQuery();
Connection.Close();
Connection.Dispose();
}
假设我在我的对象数组 PARAMETROS 中添加了一个 int,当它到达 foreach 语句时,我得到一个错误:
SqlParameterCollection 只接受非 null SqlParameter 类型对象,不接受 Int32 对象。
那么,我怎样才能在这个类之外加载我的所有参数,然后将它们全部放入一个通用数组中,然后将它传递给这个方法来实现它的魔力。有什么帮助吗?
编辑:一个朋友给我发了这个代码,它会工作吗?我无法理解它在做什么。:S
protected void CargarParametros(SqlCommand Com, System.Object[] Args)
{
for (int i = 1; i < Com.Parameters.Count; i++)
{
SqlParameter P = (SqlParameter)Com.Parameters[i];
if (i <= Args.Length )
P.Value = Args[i - 1];
else
P.Value = null;
}
}