[]
The ADO.NET provider for CSV provides a wide range of features that enable connectivity to CSV from .Net applications. The documentation will help you understand the C1.AdoNet.CSV namespace, which includes all the available classes that can be used to connect and retrieve data from a CSV file.
DataConnectors are mostly used in combination with other ComponentOne components, such as DataEngine and FlexPivot. The procedure below describes how to use the DataConnector in a console application within Visual Studio.
The ADO.NET provider for CSV can be used in any application. In this guide, a console application is created:
Follow the steps provided below to learn and implement data retrieval using ADO.NET provider for CSV.
Create a connection string to set up a connection to a local CSV resource by setting the Uri property.
static string csvConnectionString = $"Uri='sampleCSV.csv'";
Fetch the data using C1CSVConnection class. The connection string using corresponding attributes is passed as an argument. For more information on creating connections, see Creating Connection.
C1CSVConnection implements the ADO.NET DbConnection, similar to standard ADO.NET connection object that retrieves a single result set of all the data that matches a query. Once the connection is established, it retrieves the data from the source as shown in the following code example.
static void ReadData()
{
Console.WriteLine("Query all Accounts...");
//Fetch data
using(var con = new C1CSVConnection(csvConnectionString))
{
con.Open();
var table = con.GetSchema("columns", new string[] { "sampleCSV" });
ShowDataTable(table);
var cmd = con.CreateCommand();
//Provide command
cmd.CommandText = "Select * From sampleCSV";
var reader = cmd.ExecuteReader();
}
}
//Display Table
static void ShowDataTable(DataTable table, int length = 25)
{
foreach (DataColumn col in table.Columns)
{
Console.Write("{0,-" + length + "}", col.ColumnName);
}
Console.WriteLine();
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
if (col.DataType.Equals(typeof(DateTime)))
Console.Write("{0,-" + length + ":d}", row[col]);
else if (col.DataType.Equals(typeof(decimal)))
Console.Write("{0,-" + length + ":C}", row[col]);
else
Console.Write("{0,-" + length + "}", row[col]);
}
Console.WriteLine();
}
}