0

I'm new to the enterprise library framework. Is there a way to obtain database schema similar to sqlConnection.GetSchema() method in enterprise library.

Any help would be greatly appreciated.

Thanks, shishi

shishi
  • 107
  • 1
  • 2
  • 8

1 Answers1

3

I don't think there is any wrapper for this functionality in the block. You can use the DbConnection.GetSchema() method by getting a DbConnection from the Database:

Database db = DatabaseFactory.CreateDatabase();
DbConnection conn = db.CreateConnection();
DataTable dt = conn.GetSchema();
Randy Levy
  • 22,566
  • 4
  • 68
  • 94
  • Thanks Tuzo for the quick response. Is there a way for me to obtain stored procedure from the conn.GetSchema() i.e conn.GetSchema("Tables") lists all the tables, something similar to that I need to obtain for stored procedure. Do you have any idea about it?? Any help would be appreciated. – shishi Jul 30 '13 at 13:07
  • Are you looking for: `conn.GetSchema("Procedures");` and `conn.GetSchema("ProcedureParameters");`? – Randy Levy Jul 30 '13 at 14:18
  • @Tuzo is there some way to get what the schema of the results of a stored procedure are. What I mean is, if a stored procedure (called "getemployeesalary") gets an employee id from one table, his name from another, and his salary from another, get the datatype for each column (e.g. varchar(15) for the employee id, varchar(20) for the salary, etc)? Its not enough to get the datatype (string) I need to know the exact character limitation of the column in the database. – n00b Feb 21 '14 at 16:02
  • @n00b, You can execute the stored procedure and interrogate the DataReader to retrieve all of the column information. See [How To Retrieve Column Schema by Using the DataReader GetSchemaTable Method and Visual C# .NET](http://support.microsoft.com/kb/310107) for an example. – Randy Levy Feb 21 '14 at 16:37
  • You can use that approach with EntLib as well: `var reader = db.ExecuteReader(CommandType.StoredProcedure, "Test"); var schemaTable = reader.GetSchemaTable();` and then enumerate the results exactly as in the example. – Randy Levy Feb 21 '14 at 16:43