0

Normally I use SQL parameters to avoid injection SQL by the hacker. But how can I use SQL parameters in my loop :

try
{
    using (var connectionWrapper = new Connexion())
    {

        var connectedConnection = connectionWrapper.GetConnected();
        string sSql = "";
        foreach (var oItem in LeListVoit_End)
        {
            //insert into Lettrvoit 
            if (sSql != "") sSql += " UNION ALL ";
            sSql += "SELECT '" + oItem.IdLettre + "', '" + oItem.Date_Cloture + "', '" + oItem.CodeDest + "', '" + oItem.ModalMode + "', '" + oItem.LibPort + "', '" + oItem.LibExpr + "', '" + oItem.LibUnite + "', '" + oItem.EnlvUnite + "', '" + oItem.NbrColis + "', '" + oItem.Poids.ToString().Replace(',', '.') + "', '" + oItem.LeCR.ToString().Replace(',', '.') + "', '" + oItem.LeVD.ToString().Replace(',', '.') + "', '" + oItem.CodeClient + "', '"
                                + oItem.RsNom_Exp + "', '" + oItem.Addr_Exp + "', '" + oItem.CP_Exp + "', '" + oItem.Ville_Exp + "', '" + oItem.Tel_Exp + "', '" + oItem.Fax_Exp + "', '"
                               + oItem.RsNom_Dest + "', '" + oItem.Addr_Dest + "', '" + oItem.CP_Dest + "', '" + oItem.CP_Dest + "', '" + oItem.Tel_Dest + "', '" + oItem.Fax_Dest + "', '" + oItem.InseeDest + "', '"
                               + Is_Print + "', '" + CHAUFFEUR + "'";
        }
        string sqlComm_Insert = "INSERT INTO LETTRE_VOIT_FINAL  ([NOID], [DATE_CLOTURE], [CODE_DEST] ,[MODAL_MODE], [LIBELLE_PORT] ,[LIBELLE_EXPR], [LIBELLE_UNITE],ENLEV_UNITE, [NBR_COLIS], [POID], [ENLEV_CREMB], [ENLEV_DECL], CODE_CLIENT, [RS_NOM_EXP] ,[ADDR_EXP]  ,[CP_EXP] ,[VILLE_EXP] ,[TEL_EXP] ,[FAX_EXP],[RS_NOM_DEST] ,[ADDR_DEST] ,[CP_DEST] ,[VILLE_DEST]  ,[TEL_DEST] ,[FAX_DEST],INSEE_DEST, IS_PRINT, CHAUFFEUR) " + sSql;
        SqlCommand comm_Insert = new SqlCommand(sqlComm_Insert, connectionWrapper.conn);
        comm_Insert.ExecuteScalar();
    }
}
catch (Exception excThrown)
{
    throw new Exception("Err", excThrown);
}

as you can see above the SQL parameters is outside my loop.

1
  • You are performing a table insert, have you thought of using a SQLDataAdapter instead? Commented Dec 21, 2011 at 9:58

3 Answers 3

2

Since SQL parameter names have to be unique you could use a for loop instead and append the index of the loop variable in each iteration to the SQL parameter name, i.e.:

for(int i=0; i< LeListVoit_End.Length; i++)
{
  string sql = string.Format("select foo from bar where baz = @FOO{0}" i);
  command.Parameters.Add(string.Format("@FOO{0}",i), SqlDbType.VarChar, 80).Value = "someValue";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Can you write a stored procedure instead of building a statement string? Either way I would execute a command per item in the list, and execute all commands in a single transaction.

  • start transaction
  • loop items, building and executing insert into command for each item
  • commit transaction (or rollback on fail)

Add parameter to the command using the Parameters collection:

command.Parameters.Add({several overloads})

Comments

0

You can create the connection, command and parameters outside of the loop and then just set the parameter values and execute the command within the loop:

 comm_Insert.Parameters[""].Value = "";
 comm_Insert.ExecuteScalar();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.