简体   繁体   中英

WCF - Foreach loop to insert records into a database

I'm working on WCF project. I am trying to insert multiple records into my SQL Server database from an array.

when calling the service, I get an exception :"Procedure or function has too many arguments specified", while my arguments in my function are in confirmity with those declared in my stored procedure :

Here is my function in WCF :

    public static string SetGaranties( List<int> CODE_GARANTIES,  string NUMERO_POLICE, string CODE_BRANCHE, int CODE_SOUS_BRANCHE)
    {
        string MSG_ACQUITEMENT = string.Empty;
        DbCommand com = GenericData.CreateCommand(GenericData.carte_CarteVie_dbProviderName, GenericData.Carte_CarteVie_dbConnectionString);

        com.CommandText = "SetGaranties";

        com.Parameters.Clear();

        foreach (int CODE_GARANTIE in CODE_GARANTIES)
        {
            com.Connection.Open();
            SqlParameter NUMERO_POLICE_Param = new SqlParameter("@NUMERO_POLICE", NUMERO_POLICE);
            com.Parameters.Add(NUMERO_POLICE_Param);

            SqlParameter CODE_BRANCHE_Param = new SqlParameter("@CODE_BRANCHE", CODE_BRANCHE);
            com.Parameters.Add(CODE_BRANCHE_Param);

            SqlParameter CODE_SOUS_BRANCHE_Param = new SqlParameter("@CODE_SOUS_BRANCHE", CODE_SOUS_BRANCHE);
            com.Parameters.Add(CODE_SOUS_BRANCHE_Param);

            SqlParameter CODE_POSTALE_Param = new SqlParameter("@CODE_GARANTIE", CODE_GARANTIE);
            com.Parameters.Add(CODE_POSTALE_Param);
            DbDataReader reader = com.ExecuteReader();
            com.Connection.Close();                     

        }

and here is my Stored procedure :

    ALTER PROCEDURE [dbo].[SetGaranties]    
@NUMERO_POLICE     varchar(12),
@CODE_BRANCHE   varchar(1),
@CODE_SOUS_BRANCHE  int,
@CODE_GARANTIE  int
AS  
BEGIN  
   SET NOCOUNT ON;
    INSERT INTO dbo.MVT_GARANTIES VALUES(
@NUMERO_POLICE,
@CODE_BRANCHE,
@CODE_SOUS_BRANCHE,
@CODE_GARANTIE
);
END

Does anybody know how to fix this?

Build the parameters outside the loop just once, set the invariant values outside the loop and inside the loop just set only the one value that changes at each loop

public static string SetGaranties( List<int> CODE_GARANTIES,  string NUMERO_POLICE, string CODE_BRANCHE, int CODE_SOUS_BRANCHE)
{
    string MSG_ACQUITEMENT = string.Empty;
    DbCommand com = GenericData.CreateCommand(GenericData.carte_CarteVie_dbProviderName, GenericData.Carte_CarteVie_dbConnectionString);
    com.CommandText = "SetGaranties";
    com.CommandType = CommandType.StoredProcedure;

    // These parameter's values don't change, set it once
    com.Parameters.Add("@NUMERO_POLICE", SqlDbType.VarChar).Value = NUMERO_POLICE;
    com.Parameters.Add("@CODE_BRANCHE",SqlDbType.VarChar).Value = CODE_BRANCHE;
    com.Parameters.Add("@CODE_SOUS_BRANCHE", SqlDbType.Int).Value = CODE_SOUS_BRANCHE;

    // This parameter's value changes inside the loop
    com.Parameters.Add("@CODE_GARANTIE",SqlDbType.Int);

    com.Connection.Open();
    foreach (int CODE_GARANTIE in CODE_GARANTIES)
    {
        com.Parameters["@CODE_GARANTIE"].Value = CODE_GARANTIE;
        com.ExecuteNonQuery();
    }
    com.Connection.Close();                     
}

Other things to say:

  • You are using a global connection object, this usually is a very bad idea. ADO.NET implements connection pooling and this means that you should create your connection when you need it and destroy it afterwards.
  • ExecuteNonQuery should be used when you INSERT/UPDATE/DELETE records. No need to build an SqlDataReader when you don't have anything to read back.
  • A Stored Procedure is executed only if you set the CommandType to StoredProcedure otherwise you get a syntax error because the CommandText is not a valid Sql Statement

This:

com.Parameters.Clear();

Should be inside your loop. With the current code the first iteration should have the correct number of parameters. But subsequent iterations will have too many because the the param list isn't being cleared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM