DataConnector | ComponentOne
ADO.NET provider for CSV / Creating Connection
In This Topic
    Creating Connection
    In This Topic

    To establish a connection to a CSV data source, the ADO.NET Provider can be used through the C1CSVConnection class. This class requires a connection string to be provided as an argument, which can be created in either of the following ways:

    Connection string generation

    The following code shows how a connection string can be configured by the C1CSVConnectionStringBuilder class and consumed by the C1CSVConnection class to create a connection to CSV server. Through that connection, the data source can be queried, updated, etc.

    C#
    Copy Code
    //JSON service uri
    const string csvUri = "sampleCSV.csv";
    
    //configure connection string
    C1CSVConnectionStringBuilder connectionStringBuilder = new C1CSVConnectionStringBuilder();
    connectionStringBuilder.Uri = csvUri;
    connectionStringBuilder.TrimValues = true;
               
    //Setup Connection
    C1CSVConnection con = new C1CSVConnection(connectionStringBuilder.ConnectionString);

    Connection string literal

    Alternatively, the connection string can be written directly as shown in the following code. A similar approach can be implemented in other DataConnectors for ADO.NET provider.

    C#
    Copy Code
    //Create connection string
    static string csvConnectionString = $"Uri='sampleCSV.csv'";
    
    //Setup Connection
    C1CSVConnection con = new C1CSVConnection(csvConnectionString);

    CSV Data Sources

    The ADO.NET provider for CSV allows you to connect to local and https CSV resources. To connect to these resources, you only need to set the Uri property to the CSV resource location. Additionally, you can set other properties such as TrimValues, and ContainsHeaders which can be helpful in the manipulation of the CSV resource.

    Connect to Local Files

    To connect to a local file, you can create a connection string by setting the Uri property to a CSV file as shown in the following code:

    C#
    Copy Code
    static string csvConnectionString = $"Uri='sampleCSV.csv'";

    Connect to HTTP CSV Streams

    To connect to an HTTP file, you can create a connection string by setting the Uri property to the HTTP or HTTPS URL of the CSV resource you want to access as a table, as shown in the following code:

    C#
    Copy Code
    static string documentConnectionString = $"Uri='https://docs.google.com/spreadsheets/d/e/2PACX-1vTgdxvUJhf8i6ri1a_y5gUUwKuFr3zz5J6h0W0gqWev7AT1BK916NJnD3YxB6KRouQXSf6cO5DoUqlm/pub?gid=1155993230&single=true&output=csv'";