开发者

SqlCommand & SqlConnection

If I have something like this:

using( SqlCommand sqlCmd = CreateSqlCommand( "load_collection_get", false ) )
{
    // Code
}

CreateSqlCommand looks like:

public static SqlCommand CreateSqlCommand( string storedProc, bool transaction )
    {
        SqlCommand sqlCmd = new SqlCommand( storedProc, sqlHelper.GetConnection() );

        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        if( transaction )
            sqlCmd.Transaction = sqlHelper.GetTran开发者_StackOverflowsaction();

        return ( sqlCmd );
    }   

SqlHelper.GetConnection() returns an opened SqlCommand object

At the end of the using( SqlCommand ... ) will that close the connection for me? Or do i still need to call SqlConnection.Close before the end of the using?


No, you'll still have to close the SqlConnection yourself, the using there will only dispose of the SqlCommand object.

I tend to think about it like this: you may need the connection to be open for multiple commands, perhaps all running within an SqlTransaction. If disposing of the first command closed the connection then you'd have no way to run the subsequent commands, let alone commit the transaction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜