ExcelConnection Class Library  

ExcelConnection.State Property

Gets the current state of the connection.

[Visual Basic]
Public Overrides ReadOnly Property State As ConnectionState
[C#]
public override ConnectionState State {get;}

Property Value

A bitwise combination of the ConnectionState values. The default is Closed.

Remarks

The allowed state changes are:

Example

[C#, Visual Basic] The following example creates an ExcelConnection and sets some of its properties.

[C#] 

using VM.xPort.ExcelClient;

private void OpenConnections()
{
    ExcelConnection connection;

    //Open connection to Excel 2007 XLSX file. Specify that first row of spreadsheets is a 
    //header row that contains column names 
    connection = GetConnection("AuthorsInfo.xlsx", "xlsx", true);

    //Print some properties of opened connection 
    Debug.WriteLine("Connection string: " + connection.ConnectionString);
    Debug.WriteLine("Excel file name: " + connection.Database);
    Debug.WriteLine("Excel file name: " + connection.DataSource);
    Debug.WriteLine("Connection state: " + connection.State.ToString());

    //Close connection 
    connection.Close();
    Debug.WriteLine("Connection state: " + connection.State.ToString());

    //Open connection to Excel file stored in XLS file format. Specify that first row of 
    //spreadsheets is NOT a header row. 
    connection = GetConnection("AuthorsInfo.xls", "xls", false);

    //Print some properties of opened connection 
    Debug.WriteLine("Connection string: " + connection.ConnectionString);
    Debug.WriteLine("Excel file name: " + connection.Database);
    Debug.WriteLine("Excel file name: " + connection.DataSource);
    Debug.WriteLine("Connection state: " + connection.State.ToString());

    //Close connection and release all resources 
    connection.Close();

    Debug.WriteLine("Connection state: " + connection.State.ToString());

    connection.Dispose();
    connection = null;
}

private ExcelConnection GetConnection(string filePath, string format, bool hasHeader)
{
    //Sample code creates and opens connection to workbook file 
    ExcelConnection connection;

    //Open connection to the Excel file using provided connection values 
    connection = new ExcelConnection();
    connection.ConnectionString = "Data Source=" + filePath + ";Format=" + format + ";HDR=" + hasHeader.ToString() + ";";
    connection.Open();

    return connection;
} 

[Visual Basic] 
			
Imports VM.xPort.ExcelClient			

Private Sub OpenConnections()

    Dim connection As ExcelConnection

    'Open connection to Excel 2007 XLSX file. Specify that first row of spreadsheets is a 
    'header row that contains column names
    connection = GetConnection("AuthorsInfo.xlsx", "xlsx", True)

    'Print some properties of opened connection
    Debug.WriteLine("Connection string: " & connection.ConnectionString)
    Debug.WriteLine("Excel file name: " & connection.Database)
    Debug.WriteLine("Excel file name: " & connection.DataSource)
    Debug.WriteLine("Connection state: " & connection.State.ToString())

    'Close connection
    connection.Close()
    Debug.WriteLine("Connection state: " & connection.State.ToString())

    'Open connection to Excel file stored in XLS file format. Specify that first row of 
    'spreadsheets is NOT a header row.
    connection = GetConnection("AuthorsInfo.xls", "xls", False)

    'Print some properties of opened connection
    Debug.WriteLine("Connection string: " & connection.ConnectionString)
    Debug.WriteLine("Excel file name: " & connection.Database)
    Debug.WriteLine("Excel file name: " & connection.DataSource)
    Debug.WriteLine("Connection state: " & connection.State.ToString())

    'Close connection and release all resources
    connection.Close()

    Debug.WriteLine("Connection state: " & connection.State.ToString())

    connection.Dispose()
    connection = Nothing

End Sub

Private Function GetConnection(ByVal filePath As String, ByVal format As String, ByVal hasHeader As Boolean) As ExcelConnection

    'Sample code creates and opens connection to workbook file
    Dim connection As ExcelConnection

    'Open connection to the Excel file using provided connection values
    connection = New ExcelConnection()
    connection.ConnectionString = "Data Source=" & filePath & ";Format=" & format & ";HDR=" & hasHeader.ToString() & ";"
    connection.Open()

    Return connection

End Function

[C++, JScript] No example is available for C++ or JScript.

.NET Framework

Supported in: 4.0, 3.5, 3.0, 2.0

Assembly: 

VM.xPort.ExcelClient (in VM.xPort.ExcelClient.dll)

See Also

ExcelConnection Class | ExcelConnection Members | VM.xPort.ExcelClient Namespace