You can SELECT the query result to a temporary table, and metadata from this table. If you choose this solution, you need to make sure that the temporary table name is UNIQUE for each call, otherwise the metadata in the information_schema views will be shared in sessions.
if OBJECT_ID('tempdb..
select *
into
from Customer where 1 = 0
select * from
select COLUMN_NAME, DATA_TYPE from tempdb.information_schema.columns where TABLE_NAME like '#tmp%'
Result:
CustomerId int
CustomerType int
Name nvarchar
IsActive bit
, , #, , , . :
static void Main(string[] args)
{
string connStr = "Data Source=localhost;Initial Catalog=AdventureWorks;Integrated Security=True";
SqlCommand cmd = new SqlCommand("select * from Orders where 1 = 0", new SqlConnection(connStr));
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.FillSchema(ds, SchemaType.Mapped);
var metaTable = ds.Tables[0];
foreach (DataColumn col in metaTable.Columns)
{
Console.WriteLine("{0} : {1}", col.DataType, col.ColumnName);
}
}
:
System.Int32 : OrderId
System.Int32 : CustomerId
System.Int32 : ArticleId
System.Decimal : TotalAmount
System.DateTime : OrderDate