The ADO.NET provider for CSV supports schema discovery using ADO.NET classes or SQL statements to the system tables. This is done through the GetSchema method of the C1CSVConnection class, optionally specifying schema name and restriction values.
In the following code, the GetSchema method is first called to return the Tables of the database, and then it is called again to return the columns of a specific datatable.
C# |
Copy Code |
---|---|
static void GetSchema() { static string csvConnectionString = $"Uri='sampleCSV.csv'"; Console.WriteLine("Query all Accounts..."); using (var con = new C1CSVConnection(csvConnectionString)) { con.Open(); //Get list a of tables DataTable databaseTables = con.GetSchema("Tables"); Console.WriteLine("List of Tables in database:"); foreach (DataRow row in databaseTables.Rows) { //Display Tablename Console.WriteLine(row["TableName"]); } //Get column names in a table DataTable datatableColumns = con.GetSchema("Columns", new string[] { "sampleCSV" }); Console.WriteLine("\n Books Table columns:"); foreach (DataRow column in datatableColumns.Rows) { //Display column properties Console.Write(column["ColumnName"]); Console.Write("\t" + column["DataType"]); } } } 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(); } } |
Similar to the GetSchema method, you can also use the GetSchemaTable method of the C1DataReader class. The GetSchemaTable method returns a DataTable that defines the column metadata.