search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

0 answers
Q&A Is SqlConnection.Close() need inside a Using statement?
Is SqlConnection.Close() need inside a Using statement?
P粉212114661 2025-12-28 12:43:11 Views 65

0

I'm using this code:

public void InsertMember(Member member)
{
    string INSERT = "INSERT INTO Members (Name, Surname, EntryDate) VALUES (@Name, @Surname, @EntryDate)";

    using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))
    {
        sqlConnection.Open();

        using (SqlCommand sqlCommand = new SqlCommand(INSERT, sqlConnection))
        {
            sqlCommand.Parameters.Add("@Name", SqlDbType.VarChar).Value = member.Name;
            sqlCommand.Parameters.Add("@Surname", SqlDbType.VarChar).Value = member.Surname;
            sqlCommand.Parameters.Add("@EntryDate", SqlDbType.Date).Value = member.EntryDate;

            sqlCommand.ExecuteNonQuery();
        }
    }
}

It's not showing any errors or problems at all. Is it wrong if I don't add sqlConnection.Close(); before disposing of it? If yes, why?

Your Answer
submit

0 answers

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

vc9-vc14 (32+64 bit) runtime library collection (link below)

Download the collection of runtime libraries required for phpStudy installation

VC9 32-bit

VC9 32-bit

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use