1

Here's my method:

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();
        }

Say I added an int to my object array PARAMETROS, when it reaches the foreach statement I get an error:

The SqlParameterCollection only accepts non-null SqlParameter type objects, not Int32 objects.

So, how can I load all of my parameters outside of this class, and then place them all into a generic array, and pass it on to this method to do it's magic. Any help?

Edit: A friend sent me this code, would it work? I cant understand what it's doing. :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これを呼び出すには、ストアド プロシージャに渡すかストアド プロシージャから取得する for each パラメータのインスタンスを作成する必要があります。単純に値を追加することはできません.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

これを行う必要があります:

SQL コマンドの例:

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

このコマンドのパラメータを追加するには:

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

私は専門家ではありませんが、パラメーターに名前を付ける必要があると思います。そのため、オブジェクトの配列だけでなく、キーと値のペアの配列を検討する必要があります。

次に、SqlParameter コンストラクターの 1 つを見てください: 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 に答える