9

I have a couple of stored procedures in T-SQL where each stored procedure has a fixed schema for the result set.

I need to map the result sets for each procedure to a POCO object and need the column name and type for each column in the result set. Is there a quick way of accessing the information?

The best way I have found so far is accessing each stored procedure from .NET and writing my own extension method on a IDataReader/IDataRecord for dumping the information (column names and types) out.

Example, a stored procedure executing the following query:

SELECT Id, IntField, NullableIntField, VarcharField, DateField FROM SomeTable

would require me to have the mapping information:

Id - Guid
IntField - System.Int32
NullableIntField - Nullable<System.Int32>
VarcharField - String
DateField - DateTime

4 Answers 4

10

I think you should be able to use SqlDataReader.GetSchemaTable method to access the schema.

More information can be found here.

http://support.microsoft.com/kb/310107

Example from above source

SqlConnection cn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
DataTable schemaTable; 
SqlDataReader myReader; 

//Open a connection to the SQL Server Northwind database.
cn.ConnectionString = "Data Source=server;User ID=login;
                       Password=password;Initial Catalog=DB";
cn.Open();

//Retrieve records from the Employees table into a DataReader.
cmd.Connection = cn;
cmd.CommandText = "SELECT Id, IntField, NullableIntField, VarcharField, DateField FROM SomeTable";

myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo);

//Retrieve column schema into a DataTable.
schemaTable = myReader.GetSchemaTable();

//For each field in the table...
foreach (DataRow myField in schemaTable.Rows){
    //For each property of the field...
    foreach (DataColumn myProperty in schemaTable.Columns) {
    //Display the field name and value.
    Console.WriteLine(myProperty.ColumnName + " = " + myField[myProperty].ToString());
    }
    Console.WriteLine();

    //Pause.
    Console.ReadLine();
}

//Always close the DataReader and connection.
myReader.Close();
cn.Close();
1
  • Yes, that's how I planned to extract the information in my extension method. Thanks for the suggestion
    – PHeiberg
    Apr 30, 2010 at 12:13
1

I think what you're doing is probably the best approach. There's no magic repository that holds all that information, really. You can find out about the stored proc parameters from the system catalog views, but the shape and field names and types of the result set aren't stored anywhere in SQL Server system views, unfortunately.

1
  • Thanks for the confirmation of my theory.
    – PHeiberg
    Apr 30, 2010 at 13:23
1

Another option (might be better or worse than GetSchemaTable() depending on your needs).
The following code gives you a DataTable with column structure identical to your resultset:

DataTable table = new DataTable();
var cmd = new SqlCommand("...");
using (var reader = cmd.Execute(CommandBehaviour.SchemaOnly))
    table.Load(reader);
2
  • This uses SET FMTONLY ON, which is deprecated (since 2012??). See my answer above. Apr 16, 2015 at 14:57
  • I'd say, it's up to MS to change the implementation of Execute(CommandBehaviour.SchemaOnly) accordingly.
    – VladV
    Apr 17, 2015 at 8:44
0

if this is a one time thing you need to do, and not something you'll do at runtime each time the procedure is called, then just modify the result set query to dump the rows into a new table using INTO ThrowArayTable:

SELECT
    Col1, col2, col3, ...
    INTO ThrowArayTable     ----<<<add this line to existing result set query
    FROM ...
    WHERE ...

Run the application or call the procedure manually to generate the ThrowArayTable. You can now look up the column aames and datatypes using any method, SSMS or INFORMATION_SCHEMA.COLUMNS

Just remember to change the procedure back (remove the INTO ThrowArayTable value) so it will actually return the results set.

2
  • Yes, good suggestion. I didn't know that method for accessing the type information. The downsides are that I don't have access to editing the stored procedures, since they are not under my control. Also I would have to map the column types manually into the corresponding .NET types, including nullable types.
    – PHeiberg
    Apr 30, 2010 at 12:19
  • 1
    Thanks. Sometimes the simplest approach is best. This is exactly what I wanted. I don't know why the solution eluded me. I guess because I was thinking about it in the context of a stored procedure.
    – Darrel Lee
    Jul 2, 2015 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.