using()

hi,
What is the best way to rewrite this method with the keyword using()
I know I can use it for the connection and command.

 

public int EmployeeDetailsAdd(string strFirstName, string strLastName, string strTitle,

DateTime dtBirthDate, string strNotes)

{

string strSQL = "usp_EmployeeDetailsAdd";

string strConn = clsDataHandler.GetConnectionString;

try

{

SqlConnection oCon = new SqlConnection(strConn);

SqlCommand oCmd = new SqlCommand(strSQL, oCon);

oCmd.CommandType = CommandType.StoredProcedure;

oCmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10);

oCmd.Parameters["@FirstName"].Value = strFirstName;

oCmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 20);

oCmd.Parameters["@LastName"].Value = strLastName;

oCmd.Parameters.Add("@Title", SqlDbType.NVarChar, 30);

oCmd.Parameters["@Title"].Value = strTitle;

oCmd.Parameters.Add("@BirthDate", SqlDbType.DateTime);

oCmd.Parameters["@BirthDate"].Value = dtBirthDate;

oCmd.Parameters.Add("@Notes", SqlDbType.NText);

oCmd.Parameters["@Notes"].Value = strNotes;

oCmd.Parameters.Add("@EmployeeID", SqlDbType.Int, 4);

oCmd.Parameters["@EmployeeID"].Direction = ParameterDirection.Output;

oCon.Open();

oCmd.ExecuteNonQuery();

int intEmployeeID = (int)oCmd.Parameters["@EmployeeId"].Value;

oCon.Close();

oCon.Dispose();

return intEmployeeID;

}

catch(Exception ex)

{

throw ex;

}

}



Answer this question

using()

  • Sean Feldman

    No, you don't need to, because Using will take care of it in this case.

    Calling Dispose will also call Close internally, so calling Close and Dispose together is overkill if you know what you are doing.

    Otherwise, Omerkamal is right. Close your bottle if you don't want the spirts to evaporate! ;)



  • zisha

    I don't think you need to call the Close or Dispose method for the SqlConnection object

    if you are using using keyword

    Hope it helps,

    Ivan Wong


  • CJWalsh

    Should I still have oCon.Close() and oCon.Dispose()

    I was also told that I should have using around the sqlCommand too


  • Dave987654321

     

    Using keyword always come with connection.

    you code will look like this after;

    Using(SqlConnection oCon = new SqlConnection(strConn)){

    SqlCommand oCmd = new SqlCommand(strSQL, oCon);
    oCmd.CommandType = CommandType.StoredProcedure;


    oCmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10);
    oCmd.Parameters["@FirstName"].Value = strFirstName;
    oCmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 20);
    oCmd.Parameters["@LastName"].Value = strLastName;
    oCmd.Parameters.Add("@Title", SqlDbType.NVarChar, 30);
    oCmd.Parameters["@Title"].Value = strTitle;
    oCmd.Parameters.Add("@BirthDate", SqlDbType.DateTime);
    oCmd.Parameters["@BirthDate"].Value = dtBirthDate;
    oCmd.Parameters.Add("@Notes", SqlDbType.NText);
    oCmd.Parameters["@Notes"].Value = strNotes;
    oCmd.Parameters.Add("@EmployeeID", SqlDbType.Int, 4);
    oCmd.Parameters["@EmployeeID"].Direction = ParameterDirection.Output;


    oCon.Open();
    oCmd.ExecuteNonQuery();
    int intEmployeeID = (int)oCmd.Parameters["@EmployeeId"].Value;


    oCon.Close();
    oCon.Dispose();

    }

     



  • Craig Westlake

    Is this ok
    Notice that I am not closiing or disposing connection.

    Thanks

    public void EmployeeDetailsDelete(int intEmployeeID)

    {

    string strSQL = "usp_EmployeeDetailsDelete";

    string strConn = clsDataHandler.GetConnectionString;

    using(SqlConnection oCon = new SqlConnection(strConn))

    {

    using(SqlCommand oCmd = new SqlCommand(strSQL, oCon))

    {

    oCmd.CommandType = CommandType.StoredProcedure;

    oCmd.Parameters.Add("@EmployeeID", SqlDbType.Int, 4);

    oCmd.Parameters["@EmployeeID"].Value = intEmployeeID;

    oCon.Open();

    oCmd.ExecuteNonQuery();

    }

    }

    }


  • Francesco75

     

    Its good Excercise to close the dataset when you dont need it for the moment.( its like closing the lid of a bottle when you dont need it to drink any more from it for the time beeing)

    Dispose should be done once in the application. An it is When you dont need it for any more operations on DataBse.If you dispose the Dataset object then you wont be able to access it any more after.( its like you broke the bottle either it was empty or you had no more lust to drink from it)

    You can see that your SqlCommand is in the Using() keyword.



  • using()