Sqlcommand.Parameters에 숫자를 추가하는 방법?
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 문에 도달하면 오류가 발생합니다.
The SqlParameterCollection only accepts non-null SqlParameter type objects, not Int32 objects.
그러면 어떻게 모든 이 클래스 외부의 매개 변수를 가져온 다음 일반 배열에 모두 배치하고이 메서드로 전달하여 마법을 수행합니다. 어떤 도움이 필요합니까?
편집 : 친구가이 코드를 보냈습니다. 작동합니까? 나는 그것이 무엇을하는지 이해할 수 없다. : 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;
}
}
은'Parameters.AddWithValue'를 사용하지 왜 다른 질문 http://stackoverflow.com/questions/1355643/trouble-using-my-stored-procedure –