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# |
コードのコピー
|
---|---|
static void GetSchema() { static string csvConnectionString = $"Uri='sampleCSV.csv'"; Console.WriteLine("Query all Accounts..."); using (var con = new C1CSVConnection(csvConnectionString)) { con.Open(); //テーブルのリストを取得します。 DataTable databaseTables = con.GetSchema("Tables"); Console.WriteLine("List of Tables in database:"); foreach (DataRow row in databaseTables.Rows) { //テーブル名を表示します。 Console.WriteLine(row["TableName"]); } //テーブル内の列名を取得します。 DataTable datatableColumns = con.GetSchema("Columns", new string[] { "sampleCSV" }); Console.WriteLine("\n Books Table columns:"); foreach (DataRow column in datatableColumns.Rows) { //列のプロパティを表示します。 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 GetSchemaTable method of the C1DataReader class. The GetSchemaTable method returns a DataTable that defines the column metadata.