dotConnect for SQL Server Documentation
Devart.Common Namespace / DbConnectionBase Class / BeginOpen Method
The delegate to call when the asynchronous invoke is complete. If callback is a null reference (Nothing in Visual Basic), the delegate is not called.
State information that is passed on to the delegate.
Example

In This Topic
    BeginOpen Method
    In This Topic
    Starts an asynchronous invocation of an Open method.
    Syntax
    'Declaration
     
    Public Function BeginOpen( _
       ByVal callback As AsyncCallback, _
       ByVal stateObject As Object _
    ) As IAsyncResult
    public IAsyncResult BeginOpen( 
       AsyncCallback callback,
       object stateObject
    )

    Parameters

    callback
    The delegate to call when the asynchronous invoke is complete. If callback is a null reference (Nothing in Visual Basic), the delegate is not called.
    stateObject
    State information that is passed on to the delegate.

    Return Value

    An System.IAsyncResult interface that represents the asynchronous operation started by calling this method.
    Remarks

    BeginOpen method enables you to open a connection to server without having current thread blocked. In other words, your program can continue execution while the connection is establishing so you do not have to wait for it.

    To start opening a connection, you have to call BeginOpen method, which in turn invokes appropriate actions in another thread. Return value of this method must be assigned to System.IAsyncResult object. After executing this method program flow continues.

    When you are ready to use the connection, call EndOpen. If at the moment you call this method the negotiation process has not yet been finished, application stops and waits till the function returns which means that the connection is open.

    Refer to "Asynchronous Query Execution" article for detailed information.

    Example
    This sample shows how to implement asynchronous opening of a connection.
    public void AsyncOpen(DbConnectionBase myConnection)
    {
      IAsyncResult myAsyncResult = myConnection.BeginOpen(null, null);
      DbCommandBase myCommand = (DbCommandBase)myConnection.CreateCommand();
      myCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values('100', 'DEVELOPMENT')";
      //some operations here
      myConnection.EndOpen(myAsyncResult);
      try
      {
        myCommand.ExecuteNonQuery();
      }
      finally
      {
        myConnection.Close();
      }
    }
    Public Sub AsyncOpen(ByVal myConnection As DbConnectionBase)
      Dim myAsyncResult As IAsyncResult = myConnection.BeginOpen(Nothing, Nothing)
      Dim myCommand As DbCommandBase = myConnection.CreateCommand()
      myCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values('110', 'QA')"
      ' some operations here
      myConnection.EndOpen(myAsyncResult)
      Try
        myCommand.ExecuteNonQuery()
      Finally
        myConnection.Close()
      End Try
    End Sub
    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also