2013-08-29 33 views
1

上下文的SQLServer 2012,Visual Studio 2010中SQLCLR動態列?

我不知道這甚至有可能,但我試圖動態生成在返回的表中的列。

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

poodle.getKs()給出了字符串數組中列名的列表。

這是我感到困惑的地方。我試圖將List對象轉換爲SqlMetaData數組,然後將其轉換爲SqlDataRecord以分配給記錄變量。

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

我得到上構建的錯誤是

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

'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 

然而,這是該我的C#技能和理解失敗的地步。我究竟做錯了什麼?

回答

1

好,用數據算出來

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>()); 

加載了很好過。現在看看SQLServer是否會使用它或barf。