Question

Context SQLServer 2012, Visual Studio 2010.

I don't know if this is even possible, but I'm trying to generate the columns in the returned table dynamically.

List<object> columnsInfo = new List<object>();      
foreach (string entry in poodle.getKs())
{
    columnsInfo.Add( new SqlMetaData(entry, SqlDbType.Text));
}

poodle.getKs() has given the list of column names in an array of string.

This is where I'm getting confused. I'm trying to convert the List of object into an array of SqlMetaData and then casting that to a SqlDataRecord for assignment to the record variable.

SqlDataRecord record = (SqlDataRecord)columnsInfo.ToArray<SqlMetaData>(); 

The errors I'm getting on the build are

Instance argument: cannot convert from 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<Microsoft.SqlServer.Server.SqlMetaData>'    

and

'System.Collections.Generic.List<object>' does not contain a definition for 'ToArray' and the best extension method overload 'System.Linq.Enumerable.ToArray<TSource>(System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments  

however this is the point at which my C# skills and comprehension fail. What am I doing wrong?

Was it helpful?

Solution

Ok, worked it out

List<SqlMetaData> columnsInfo = new List<SqlMetaData>(); 

foreach (string entry in poodle.getKs())
{
    columnsInfo.Add( new SqlMetaData(entry, SqlDbType.Text));
}

SqlDataRecord record = new SqlDataRecord(columnsInfo.ToArray<SqlMetaData>()); 

Loads up with data nicely too. Now to see if SQLServer will consume it or barf.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top