TxtConnection Class Library  

TxtConnection.Open Method

Opens connection to the folder (which contains text files) with the property settings specified by the ConnectionString.

[Visual Basic]
Public Sub Open()
[C#]
public void Open();

Exceptions

Exception Type Condition
ArgumentException Connection string was not supplied, an invalid connection string argument has been supplied, required connection string argument has not been supplied or mutually exclusive connection string arguments were supplied.

Example

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

[C#] 

using xPortTools.TxtClient;

private void LoadDataFromFile() {

    string connectionString;

    //Create connection string to text files locted at C:\SourceFiles folder, where
    //files are comma-delimited, have first rows as a header and each column has specific data type
    connectionString = GetConnectionString(@"C:\SourceFiles", ",", true, true, 
        true, 0, null, true, null, null, 
        new String[] {"String", "String", "String", "String", "String", "String", 
        "String", "Int32", "Boolean", "String", "String"});

    TxtConnection connection = new TxtConnection();
    connection.ConnectionString = connectionString;
    connection.Open();
    
    //Create TxtCommand to specify from which file to query data.
    TxtCommand command = new TxtCommand("Authors.csv", connection);
    command.CommandType = CommandType.TableDirect; //It is always TableDirect

    DataTable result = new DataTable();

    //Create TxtDataAdapter to load data into the DataSet
    TxtDataAdapter textAdapter = new TxtDataAdapter(command);

    //Get data from files
    textAdapter.Fill(result);

    dataGridView1.DataSource = result;

    //Release all opened resources 
    textAdapter = null;

    command.Dispose();
    command = null;
    connection.Close();
    connection.Dispose();
    connection = null;
}

private string GetConnectionString(string folder, string delimiter, bool? hasQuotes, bool? hasHeader,
    bool? trimSpaces, int? skipRows, string encoding, bool? ignoreEmptyLines, string commentPrefix,
    int[] columnsWidth, string[] columnsType) {

    string connectionString;

    if (String.IsNullOrEmpty(folder) || !Directory.Exists(folder)) {
        MessageBox.Show("Provided path value is not valid or path does not exist.");
        return null;
    }
    connectionString = "Data Source=" + folder + ";";

    //Add values only if they are provided, otherwise use default connection string settings
    if (!String.IsNullOrEmpty(delimiter)) {
        connectionString += "Delimiter='" + delimiter + "';";
    }

    if (hasQuotes.HasValue) {
        connectionString += "Has Quotes=" + hasQuotes.ToString() + ";";
    }

    if (hasHeader.HasValue) {
        connectionString += "Has Header=" + hasHeader.ToString() + ";";
    }

    if (trimSpaces.HasValue) {
        connectionString += "Trim Spaces=" + trimSpaces.ToString() + ";";
    }

    if (ignoreEmptyLines.HasValue) {
        connectionString += "Ignore Empty Lines=" + ignoreEmptyLines.ToString() + ";";
    }

    if (skipRows.HasValue) {
        connectionString += "Skip Rows=" + skipRows.ToString() + ";";
    }

    if (!String.IsNullOrEmpty(commentPrefix)) {
        connectionString += "Comment Prefix='" + commentPrefix + "';";
    }

    if (!String.IsNullOrEmpty(encoding)) {
        connectionString += "Encoding=" + encoding + ";";
    }

    if (columnsWidth != null && columnsWidth.Length > 0) {
        connectionString += "Column Width=" + String.Join(",",
            Array.ConvertAll(columnsWidth,
            new Converter(delegate(int value) { return value.ToString(); }))) + ";";
    }

    if (columnsType != null && columnsType.Length > 0) {
        connectionString += "Column Type=" + String.Join(",", columnsType) + ";";
    }

    return connectionString;

}

[Visual Basic] 
Imports xPortTools.TxtClient			

Private Sub LoadDataFromFile()

    Dim connectionString As String

    'Create connection string to text files locted at C:\SourceFiles folder, where
    'files are comma-delimited, have first rows as a header and each column has specific data type
    connectionString = GetConnectionString("C:\SourceFiles", ",", True, True, _
        True, 0, Nothing, True, Nothing, Nothing, _
        New String() {"String", "String", "String", "String", "String", "String", _
        "String", "Int32", "Boolean", "String", "String"})

    Dim connection As TxtConnection = New TxtConnection()
    connection.ConnectionString = connectionString
    connection.Open()
    
    'Create TxtCommand to specify from which file to query data.
    Dim command As TxtCommand = New TxtCommand("Authors.csv", connection)
    command.Connection = connection
    command.CommandType = CommandType.TableDirect 'It is always TableDirect

    Dim result As DataTable = New DataTable()

    'Create TxtDataAdapter to load data into the DataSet
    Dim textAdapter As TxtDataAdapter = New TxtDataAdapter(command)

    'Get data from files
    textAdapter.Fill(result)

    dataGridView1.DataSource = result

    'Release all opened resources 
    textAdapter = Nothing

    command.Dispose()
    command = Nothing
    connection.Close()
    connection.Dispose()
    connection = Nothing

End Sub

Private Function GetConnectionString(ByVal folder As String, ByVal delimiter As String, _
    ByVal hasQuotes As System.Nullable(Of Boolean), ByVal hasHeader As System.Nullable(Of Boolean), _
    ByVal trimSpaces As System.Nullable(Of Boolean), ByVal skipRows As System.Nullable(Of Integer), _
    ByVal encoding As String, ByVal ignoreEmptyLines As System.Nullable(Of Boolean), _
    ByVal commentPrefix As String, ByVal columnsWidth As Integer(), ByVal columnsType As String()) As String

    Dim connectionString As String

    If String.IsNullOrEmpty(folder) OrElse Not Directory.Exists(folder) Then
        MessageBox.Show("Provided path value is not valid or path does not exist.")
        Return Nothing
    End If

    connectionString = "Data Source=" & folder & ";"

    'Add values only if they are provided, otherwise use default connection string settings 
    If Not [String].IsNullOrEmpty(delimiter) Then
        connectionString += "Delimiter='" & delimiter & "';"
    End If

    If hasQuotes.HasValue Then
        connectionString += "Has Quotes=" & hasQuotes.ToString() & ";"
    End If

    If hasHeader.HasValue Then
        connectionString += "Has Header=" & hasHeader.ToString() & ";"
    End If

    If trimSpaces.HasValue Then
        connectionString += "Trim Spaces=" & trimSpaces.ToString() & ";"
    End If

    If ignoreEmptyLines.HasValue Then
        connectionString += "Ignore Empty Lines=" & ignoreEmptyLines.ToString() & ";"
    End If

    If skipRows.HasValue Then
        connectionString += "Skip Rows=" & skipRows.ToString() & ";"
    End If

    If Not [String].IsNullOrEmpty(commentPrefix) Then
        connectionString += "Comment Prefix='" & commentPrefix & "';"
    End If

    If Not [String].IsNullOrEmpty(encoding) Then
        connectionString += "Encoding=" & encoding & ";"
    End If

    If columnsWidth IsNot Nothing AndAlso columnsWidth.Length > 0 Then
        connectionString += "Column Width=" & String.Join(",", _
            System.Array.ConvertAll(Of Integer, String)(columnsWidth, _
            New Converter(Of Integer, String)(AddressOf ConvertToString))) & ";"
    End If

    If columnsType IsNot Nothing AndAlso columnsType.Length > 0 Then
        connectionString += "Column Type=" & [String].Join(",", columnsType) & ";"
    End If

    Return connectionString

End Function

Private Function ConvertToString(ByVal value As Integer) As String
    Return value.ToString()
End Function

Requirements

Platforms: Windows Server 2008 family, Windows Server 2003 family, Windows 2000 family, Windows 7 family, Windows Vista family, Windows XP family

See Also

TxtConnection Class | TxtConnection Members | xPortTools.TxtClient Namespace