G
Guy Noir
Hi. I have a quick question. I am trying to create a quick data access
layer for command CRUD tasks using stored procedures.
My question is more of a design point of view.
In each of my methods Create, Read, Update, and Delete, it seems I am
repeating a lot of code such as:
<Code>
// Create the connection and Open it
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
cmd = new SqlCommand(storedProcedure, conn);
// Set the type of command to a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// Add the SP parameters to the command
cmd.Parameters.AddRange(parameters);
</Code>
What's the opinion on this? Should I create a connection in a method
and pass it back or just repeat this chunk of code in each method? I
read some articles that mentioned that when passing connection
references around one needs to be careful to close connections, etc.
layer for command CRUD tasks using stored procedures.
My question is more of a design point of view.
In each of my methods Create, Read, Update, and Delete, it seems I am
repeating a lot of code such as:
<Code>
// Create the connection and Open it
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
cmd = new SqlCommand(storedProcedure, conn);
// Set the type of command to a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// Add the SP parameters to the command
cmd.Parameters.AddRange(parameters);
</Code>
What's the opinion on this? Should I create a connection in a method
and pass it back or just repeat this chunk of code in each method? I
read some articles that mentioned that when passing connection
references around one needs to be careful to close connections, etc.