Search code examples
.netc++sql-servermsdnusing-statement

using a SqlConnection in C++ / .NET


When I go to the MSDN page for the SqlConnection class, it only shows examples in C# and VB. Why doesn't MSDN show a C++ example?

I am particularly interested in how a C++ example would get around the lack of the using keyword in C++.


Solution

  • Hmmm... after reading What is the Managed C++ equivalent to the C# using statement it seems that the equivalent of:

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        // SqlCommand and so...
    }
    

    is actually:

    {
      SqlConnection conn(connectionString);
    
      // SqlCommand and so...
    }
    

    That is quite impressive, since C++ does not "lack" theusing statement as much as it removes the need for it entirely! I don't think that C#/VB programmers sufficiently appreciate that advantage of C++ (I certainly didn't :) .