Need a query builder for Deleting specific Rows

Indudhar Gowda 426 Reputation points
2022-04-22T14:24:47.88+00:00

Need a query builder for BulkDeleting specific Rows by using SqlBulkCopy.

 List<Part> parts = new List<Part>();      
parts.Add(new Part() { PartName = "crank arm", PartId = 1234 });
parts.Add(new Part() { PartName = "chain ring", PartId = 1334 });
parts.Add(new Part() { PartName = "regular seat", PartId = 1434 });
parts.Add(new Part() { PartName = "banana seat", PartId = 1444 });
parts.Add(new Part() { PartName = "cassette", PartId = 1534 });
parts.Add(new Part() { PartName = "shift lever", PartId = 1634 });


 var DeleteQuery = new StringBuilder();
foreach (Part aPart in parts)
{
      DeleteQuery = "DELETE FROM tablename WHERE TestcolumnID IN aPart";
}

 using (var bulkCopy = new SqlBulkCopy(destinationConnection, SqlBulkCopyOptions.Default, transaction as SqlTransaction))
                {
                    var command = new SqlCommand
                    {
                        Connection = destinationConnection,
                        CommandType = CommandType.Text,
                        CommandText = DeleteQuery,
                        Transaction = transaction as SqlTransaction
                    };
                    await command.ExecuteNonQueryAsync();
    }

Kindly let me know what has to changed here

Azure SQL Database
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,396 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,756 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,554 questions
0 comments No comments
{count} votes

10 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2022-04-22T14:48:22.06+00:00

    If you want to perform a DELETE FROM ... WHERE IN with parameters for each key, see my dynamic where in repository.

    I intentionally did not show a DELETE WHERE IN but that doesn't matter, the key is

    DELETE FROM SomeTable WHERE SomeColumn in ({0})

    Granted you need to add the library but it will keep data secure from tampering.

    0 comments No comments

  2. Erland Sommarskog 101.4K Reputation points MVP
    2022-04-22T21:15:44.213+00:00

    Normally I don't recommend using strings directly in queries as this is a SQL injection attack target but in your case you are in complete control over the string being injected so this should be OK

    But SQL injection is only one of the issues with this pattern.

    One issue is cache littering. That is, if this query runs frequently, with different lists, each command will gets it entry in the SQL Server cache. While this can be mitigated with "optimize for ad hoc workloads", I think it would be wrong as a developer to assume that this setting is in force. And it still creates a cache entry, if a smaller one. But admittedly, if this query runs only once a day, it's not a big issue.

    Another possible issue is performance if these lists can be huge. A few hundred are not a problem, but if you send in a list of hundred thousand items, the parse and compile time can be severe.

    There are some more alternatives.

    One is indeed SqlBulkCopy, but then you need to create a table to copy the values to, and then use that table in the query. This can be a little bulky, if you pardon the pun.

    You can also use a table-value parameter. You would need to create a table type for this. I have an article on my web site that shows some examples with table-valued parameters.

    Yet other options are to package the parameters into XML or JSON and then shred the values in a temp table in the SQL code.


  3. Erland Sommarskog 101.4K Reputation points MVP
    2022-05-01T12:06:33.647+00:00

    No, that code does not work. It seems to be cobbled from various pieces somewhat at random and with not too much coherence.

    You add parameters to SQL command, but the SQL statement does not have any parameters. And furthermore, it is not legal T-SQL. Those braces are out of place. I can see that you took it from Karen's post, but I think she meant the braces to be placeholders for replacement in C#.

    The text for QueryTemplate should read:

    DELETE Recognitions WHERE RecognitionID IN (@0, @1, @2, ....)
    

    and there should be at as many @n as there are elements in idBatches. In fact, the best would be to build the string as you add the parameters.

    Beware that you cannot have more that 2100 parameters a time.


  4. AgaveJoe 26,136 Reputation points
    2022-05-01T14:20:12.423+00:00

    If I understand the problem, you have a list of Items you wish to delete. I would import the list into a table. From there, writing a delete script is straight forward.

    Where does the list of items come from? If it is a file or a stream then you can use standard SQL tooling to import the data.

    0 comments No comments

  5. Indudhar Gowda 426 Reputation points
    2022-05-02T11:49:51.32+00:00

    Finally I got to know the mistake ..

       IEnumerable<string> ids = new List<string>();  
                var count2 = 0;  
                idList.ForEach(p =>  
                {  
                    ids = ids.Append("@id" +  count2);  
                    ++count2;  
                });  
      
                  
                var DeleteQuery = $"DELETE FROM Recognitions WHERE RecognitionId IN ({string.Join(",", ids)})";  
    
    
    
    
     DELETE FROM Parts WHERE PartId IN (@p0,@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,@p12,@p13,@p14,@p15,@p16,@p17,@p18,@p19,@p20,@p21,@p22,@p23,@p24,@p25,@p26,@p27,@p28,@p29,@p30,@p31,@p32,@p33,@p34,@p35,@p36,@p37,@p38,@p39,@p40,@p41,@p42,@p43,@p44,@p45,@p46,@p47,@p48,@p49) This is the Missing Part.  
    

    Thanks for the Support : @Bruce (SqlWork.com) , @Erland Sommarskog ,@Karen Payne MVP ,@Naomi ,@Michael Taylor

    0 comments No comments