The ADO.NET provider for Dynamics 365 Sales supports schema discovery using ADO.NET classes or SQL statements to the system tables. This is done through the GetSchema method of the C1D365SConnection 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 ReadSchema() { using (var connection = new C1D365SConnection(connectionString)) { connection.Open(); // テーブルのリストを取得します。 DataTable databaseTables = connection.GetSchema("Tables"); Console.WriteLine("List of Tables in database:\n"); foreach (DataRow row in databaseTables.Rows) { // テーブル名を出力します。 Console.WriteLine(row["TableName"]); } // テーブル内の列名を取得します。 DataTable datatableColumns = connection.GetSchema("Columns", new string[] { "Accounts" }); Console.WriteLine("\n Accounts table columns:"); Console.WriteLine(String.Format("{0}\t\t\t\t {1}", "Column Name", "DataType")); foreach (DataRow column in datatableColumns.Rows) { // 列のプロパティを出力します。 Console.WriteLine(String.Format("{0}\t\t\t\t {1}", column["ColumnName"], column["DataType"])); } } } |
Alternatively to the GetSchema method, the GetSchemaTable method of the C1DataReader class can be used, which returns a DataTable with the definitions of the column metadata.