IBE.SQL.EliteDBIO.DeleteMultiplePrices C# (CSharp) Method

DeleteMultiplePrices() public method

function deletes all exept the newest price for a commodity on a station, if there exists more than one.
public DeleteMultiplePrices ( List idList = null ) : void
idList List if not null the function only examines the given commodity-ids /// (for improving performance)
return void
        public void DeleteMultiplePrices(List<Int32> idList = null)
        {
            String sqlString;
            DataSet data = new DataSet();
            Int32 deletedPricesByCommodities = 0;
            Int32 deletedPrices = 0;
            String idString = "";

            try 
	        {	        
                if ((idList!= null) && (idList.Count > 0))
                {
                    foreach (Int32 id in idList)
                    {
                        if(idString.Length > 0)    
                            idString += " or ";

                        idString += String.Format("commodity_id = {0}", 
                                                     id.ToString());
                    }

                    idString = " where (" + idString + ")";
                }

		        sqlString = "select C2.station_id, C2.commodity_id, count(*) as cnt" +
		                    " from tbCommodityData C2" +
                            idString +
		                    " group by C2.station_id, C2.commodity_id" +
		                    " having cnt > 1";
                Program.DBCon.Execute(sqlString, "tbFoundData", data);

                if (data.Tables["tbFoundData"].Rows.Count > 0)
                {
                    sqlString = "";
                    foreach (DataRow foundData in data.Tables["tbFoundData"].Rows)
                    {
                        Int32 counter = 0;
                        sqlString = String.Format("select id from tbCommodityData" +
                                                  " where station_id   = {0}" + 
                                                  " and   commodity_id = {1}" + 
                                                  " order by timestamp desc",
                                                  foundData[0].ToString(), foundData[1].ToString());

                        Program.DBCon.Execute(sqlString, "tbDeleteData", data);
                        
                        foreach (DataRow deleteItem in data.Tables["tbDeleteData"].Rows)
	                    {
                            // all but the first (=newest) price
		                    if (counter > 0)
                            {
                                sqlString = String.Format("delete from tbCommodityData" +
                                                          " where id = {0}", 
                                                          deleteItem["id"].ToString());
                        
                                Program.DBCon.Execute(sqlString, "tbDeleteData", data);
                                deletedPrices++;
                            }
                            counter++;
	                    }

                        deletedPricesByCommodities++;
                    }
                }
	        }
	        catch (Exception ex)
	        {
		        throw new Exception("Error while deleting mulitple prices", ex);
	        }
        }