1

这是我的方法:

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;
            }
        }
4

5 回答 5

4

使用 AddWithValue 方法,

string []para={"@eno","@ename","@edate"};
object []val={11,"A","1-1-2002"};

System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(@"");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandText = "proc_name";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cn;
for(int i=0;i<para.Length;i++){
  cmd.Parameters.AddWithValue(para[i], val[i]);
}

cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
于 2009-08-31T04:27:30.470 回答
1

您的 SqlCommand 包装了存储过程。为了调用它,您需要为SqlParameter传入或传出存储过程的每个参数创建一个实例。您不能只是简单地添加您的值 - ADO.NET 如何知道将哪个值分配给哪个参数?

每个都SqlParameter包含以下内容:

  • 一个名字
  • 它的数据类型
  • 可能的限制(大小,长度)
  • 并且可能是一个

所以在你的情况下,你的陈述应该是这样的:

SqlCommand Command = Connection.CreateCommand();
Command.CommandText = ProcedimientoAlmacenado;
Command.CommandType = CommandType.StoredProcedure;

foreach (object X in Parametros)
{
    SqlParameter param = new SqlParameter();
    param.ParameterName = Parametros.Name;
    // you need to find a way to determine what DATATYPE the
    // parameter will hold - Int, VarChar etc.
    param.SqlDbType = SqlDbType.Int;  
    param.Value = Parametros.Value;

    Command.Parameters.Add(param);
}           

因此,仅添加值是行不通的——您需要使用它们的名称、数据类型、长度等以及它们的值来捕获这些参数。

马克

于 2009-08-31T05:42:59.193 回答
0

我不是专家,但我想你应该命名你的参数;因此,您应该考虑拥有一个键值对数组,而不仅仅是一个对象数组。

然后,您应该看一下 SqlParameter 构造函数之一:http: //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.sqlparameter.aspx

于 2009-08-31T03:51:26.707 回答
0

您也可以使用缩写版本。如果您这样做,ADO.NET 将知道它是一个数字并插入正确的数据类型:

Command.Parameters.Add(new SqlParameter("@YourParameterID", 4));

等等

此外,请确保您没有将 NULL 插入到 NOT NULL 数据字段中,并且可以隐式转换为 SqlParameter 类型。

于 2009-08-31T03:52:10.853 回答
0

你需要这样做:

sql命令示例:

"SELECT * FROM YourTable WHERE FirstColumn = @YourParameterID;"

要为此命令添加参数:

Command.Parameters.Add(new SqlParameter("@YourParameterID", SqlDbType.Int).Value = X);
于 2009-08-31T03:50:39.967 回答