I have a task to develop a console application that is a recon of values from a few tables in the DB. I have all the queries that I need for each value that is required and understand the logic on how the values will interact. The challenge I have is the best method to retrieve and store these values.
I have researched and successfully been able to create the static method to retrieve a single value from a single SQL query but I'm curious about the best method:
Create multiple methods to retrieve each value (upwards of 15 different select statements) summarised below (not complete code)
static double total1(int arg) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command1 = new SqlCommand(commandText1, connection)); return(response); } } static double total2(int arg) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command2 = new SqlCommand(commandText2, connection)); return(response); } }
Try to combine all select statements in a single method (I've been unsuccessful here) summarised below (not complete code)
using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command1 = new SqlCommand(commandText1, connection)) { } using (SqlCommand command2 = new SqlCommand(commandText2, connection)) { } // etc }
Create stored procedures in SQL and execute them and pass the parameters via the c# console app
I think method 1 is going to be taxing on the server as it would require the connection to open and close multiple times (although I don't know if that's as big a issue as I think it is). Method 2 seems more reasonable although I've followed the concepts here and I get stuck when trying to get the output of the commands (I'm using return). Method 3 seems smarter to me although I'd still be in a position where I need to choose between methods 1 & 2 to execute the SP's.
I would really appreciate advice and guidance here, I'm new to C# so this is a steep learning curve when tutorials don't cover the sort of thing (or at least I can't define my problem properly)