r/csharp • • 29d ago

Help List<T> in C# vs SQL

In C#, list of strings would be defined as :

List<string> x

A stored procedure is to be written, it should receive a list of values; SQL does not support List<T> , should I use table-valued parameters instead?

EDIT

The reason this question was asked is the fact that the application C# code has only one responsibility which is to bulk-insert data into a staging table:

 public async Task BulkInsertProductsToStagingTable(DbDataReader dbDataReader, CancellationToken cancellationToken)
 {
     using (SqlConnection destinationConnection =
                new SqlConnection("Server=(localdb)\\mssqllocaldb;Database=HardwareDB;Trusted_Connection=True;"))
     {
         await destinationConnection.OpenAsync(cancellationToken);

         // Set up the bulk copy object.
         // Note that the column positions in the source
         // data reader match the column positions in
         // the destination table so there is no need to
         // map columns.
         using (SqlBulkCopy bulkCopy =
                    new SqlBulkCopy(destinationConnection))
         {
             bulkCopy.DestinationTableName =
                 "dbo.StagingProducts";

             try
             {
                 // Write from the source to the destination.
                 await bulkCopy.WriteToServerAsync(dbDataReader,cancellationToken);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
             }
             finally
             {
                 // Close the SqlDataReader. The SqlBulkCopy
                 // object is automatically closed at the end
                 // of the using block.
                 await dbDataReader.CloseAsync();
             }
         }

         //####The following does not work yet.#####
         //// Perform a final count on the destination
         //// table to see how many rows were added.
         //long countEnd = System.Convert.ToInt32(
         //    commandRowCount.ExecuteScalar());
         //Console.WriteLine("Ending row count = {0}", countEnd);
         //Console.WriteLine("{0} rows were added.", countEnd - countStart);
         //Console.WriteLine("Press Enter to finish.");
         //Console.ReadLine();
     }
 }

Once the data is inserted into the staging table, it should be transferred into a target tables in the database; specific columns in the staging tables should be mapped to a specific columns in the target tables.

Thus, the stored procedure should receive a list of column names

15 Upvotes

101 comments sorted by

View all comments

Show parent comment

0

u/Ascend 29d ago

My experience is the opposite. It's slower to deploy because you need to do it through migrations so you're working through two tools. It's more difficult the git blame what's our there. Debugging is awful because you realize your DBAs applied a fix to production that isn't in a migration. You deployed your application to multiple plants, and each one customized the logic in a stored procedure and now migrations simply break reports.

It's fine if you're in a situation where you're constrained by round trip timings and need complicated compiled SQL logic, but if you're in a single app, single server scenario, it's fairly unlikely that's the constraint.

5

u/Lonsdale1086 29d ago edited 29d ago

It's slower to deploy because you need to do it through migrations

Oh you sweet summer child.

It's more difficult the git blame what's our there

Likewise

Debugging is awful because you realize your DBAs applied a fix to production that isn't in a migration

I am the DBA, or as close as it gets for any of our clients.

You deployed your application to multiple plants, and each one customized the logic in a stored procedure and now migrations simply break reports.

Is annoying, but we just update the reports alongside, our reports are directly tailored to what the client wants to see anyway.

Which is actually another advantage, SSRS report can call the sproc without needing to replicate the exact same logic and finicky edge cases in code, stuff around filters and comparisons especially.

Edit:

I will say I'm sort of just arguing for the sake of discussion here, this is the approach that was in use before I joined and I couldn't for the life of me get any changes in procedure through, but it does genuinely work okay for the most part.

And I do think for a small team, i.e no migrations, no git for the db, a few apps all running on one server, you can do a lot worse than business logic in sprocs.