The ADO.NET provider for ServiceNow supports schema discovery using ADO.NET classes or SQL statements to the system tables. The GetSchema method can be used to retrieve the 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 of a specific data table.
C# |
Copy Code |
---|---|
static void SchemaDetails() { //Create and open connection C1ServiceNowConnection conn = new C1ServiceNowConnection(connectionString); conn.Open(); //Get Schema Details: //Get table names DataTable databaseTables = conn.GetSchema("Tables"); Console.WriteLine("List of Tables in database: \n"); foreach (DataRow row in databaseTables.Rows) { //Display table names string tableName = row["TableName"].ToString(); Console.WriteLine(tableName); } //Get column names in a table Console.WriteLine("Columns in table incident: \n"); DataTable dtColumnsSchema = conn.GetSchema("Columns", new string[] { tableName }); foreach (DataRow crow in dtColumnsSchema.Rows) { Console.WriteLine(crow["ColumnName"].ToString() + "\t" + crow["DataType"].ToString()); } //Close Connection conn.Close(); } |
Alternatively 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.