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.
C# |
Copy Code |
---|---|
static string csvConnectionString = $"Uri='sampleCSV.csv'"; |
C# |
Copy Code |
---|---|
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(); } } |