The ADO.NET provider for JSON supports schema discovery using ADO.NET classes or SQL statements to the system tables. The ADO.NET classes enable access to schema information of the database, connection property, and columns returned and the GetSchema method can be used to retrieve schema of the Database and DataTables.
The following code shows how the GetSchema method is called which returns Tables in the Database. In the second call, the method returns the columns in a specific data table.
C# |
Copy Code |
---|---|
static void Read_Schema() { static string documentConnectionString = $"Data Model=Document;Uri='json_bookstore.json';Json Path='$.bookstore.books'"; Console.WriteLine("Query all Accounts..."); using (var con = new C1JsonConnection(documentConnectionString)) { con.Open(); //Get a list 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[] { "books" }); 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.