Beginner:
Hi Guys - looking for some help to see how should i open and close database connections.
Problem i am trying to resolve: I have a set of stored procedures which needs to be executed in the data access layer.
My service call the DA method Get(Request req) as:
public Data Get(Request request)
{
var data = new Data();
data = GetData();
data.AppleData = GetGrapeData();
data.OrangeData = GetGrapeData();
data.GrapeData = GetGrapeData();
return data;
}
where all the getmethods getdata, getgrapedata etc are private methods in the Data access class and different SP's are called in each methods.
Now in each method i am opening and closing the database connection as:
{ try{
using (var connection = new SqlConnection(connectionString)
using (var command = connection.CreateCommand())
{
connection.open();
ExecuteSP();
connection.Close();
}
}catch()
{
}
}
Now Is there any way i can do this so i have to open/ close the connection just once? I am doing try catch in each private method. is that ok? Is there any issue in the way i am doing it above?