mousemat24
asked on
Close SQL connection
Hi there
I have this code, my problem is when I have lots of users accessing the site, I get
the connection was not closed. The connection state is opened --> The connection was not closed. The connections current state is open.
How to i change the code so that it opens and closes the connection as quickly as possible?
Thanks
I have this code, my problem is when I have lots of users accessing the site, I get
the connection was not closed. The connection state is opened --> The connection was not closed. The connections current state is open.
How to i change the code so that it opens and closes the connection as quickly as possible?
Thanks
internal static void uploadFile(Attachment aAttachment)
{
using (SqlConnection objConnection = new SqlConnection(_connectionstring))
{
using (SqlCommand objCommand = CreateSqlCommand("Attachment", objConnection))
{
objCommand.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = aAttachment.FileName;
objConnection.Open();
objCommand.ExecuteNonQuery();
}
}
}
Try this... Close connection after execute.
internal static void uploadFile(Attachment aAttachment)
{
using (SqlConnection objConnection = new SqlConnection(_connectionstring))
{
using (SqlCommand objCommand = CreateSqlCommand("Attachment", objConnection))
{
objCommand.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = aAttachment.FileName;
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
}
}
}
ASKER
is it same for code like this
internal static DataSet getDocOwner(int admid)
{
using (SqlConnection objConnection = new SqlConnection(_connections tring))
{
using (SqlCommand objCommand = CreateSqlCommand("Categori es", objConnection))
{
objCommand.Parameters.Add( "@AdminID" , SqlDbType.Int).Value = admid;
using (DataSet objDS = new DataSet())
using (SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand) )
{
objDataAdapter.Fill(objDS) ;
return objDS;
}
}
}
}
do I place
objConnection.Close();
after return objDS;
so
return objDS;
objConnection.Close();
internal static DataSet getDocOwner(int admid)
{
using (SqlConnection objConnection = new SqlConnection(_connections
{
using (SqlCommand objCommand = CreateSqlCommand("Categori
{
objCommand.Parameters.Add(
using (DataSet objDS = new DataSet())
using (SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand)
{
objDataAdapter.Fill(objDS)
return objDS;
}
}
}
}
do I place
objConnection.Close();
after return objDS;
so
return objDS;
objConnection.Close();
Hi,
if you are using it like this,
using (SqlConnection objConnection = new SqlConnection(_connections tring))
{
}
you do not need to close it.
Thanks,
Krunal
if you are using it like this,
using (SqlConnection objConnection = new SqlConnection(_connections
{
}
you do not need to close it.
Thanks,
Krunal
A DataAdapter should take care of opening and closing the DB connection for you (when you call Fill()). Are you sure you are not manually opening a connection somewhere else?
ASKER
i have this code too, dont know if this helps?
private static string _connectionstring = "connString";
static DALClass()
{
ConnectionStringSettings conString = ConfigurationManager.Conne ctionStrin gs[_connec tionstring ];
if (conString == null) throw new ConfigurationErrorsExcepti on(string. Format("Yo u must have a {0} connection string in the config file", _connectionstring));
_connectionstring = conString.ConnectionString ;
}
private static string _connectionstring = "connString";
static DALClass()
{
ConnectionStringSettings conString = ConfigurationManager.Conne
if (conString == null) throw new ConfigurationErrorsExcepti
_connectionstring = conString.ConnectionString
}
It's always advisable to close a connection inside finally as below..,
internal static void uploadFile(Attachment aAttachment)
{
using (SqlConnection objConnection = new SqlConnection(_connectionstring))
{
using (SqlCommand objCommand = CreateSqlCommand("Attachment", objConnection))
{
objCommand.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = aAttachment.FileName;
objConnection.Open();
objCommand.ExecuteNonQuery();
}
}
}
finally
{
objConnection.Close();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hi i would prefer this code
internal static void uploadFile(Attachment aAttachment)
{
SqlConnection objConnection ;
try
{
objConnection = new SqlConnection(_connectionstring)
using (SqlCommand objCommand = CreateSqlCommand("Attachment", objConnection))
{
objCommand.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = aAttachment.FileName;
objConnection.Open();
objCommand.ExecuteNonQuery();
}
}
catch(Exception ex)
{
}
finally
{
objConnection.Close();
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes zadeveloper, I looked at my code, and the problem was because of methods and variables set as static.
And kaufmed, you too were right, I shouldnt have to close it, because the using block should take care of it.
Thanks to all the people who helped me out
Take care
And kaufmed, you too were right, I shouldnt have to close it, because the using block should take care of it.
Thanks to all the people who helped me out
Take care
no problems ... good luck.
NP. Glad to help :)
modify like,
objConnection.Open();
objCommand.ExecuteNonQuery
objConnection.Close();