简体   繁体   English

SQLCLR动态列?

[英]SQLCLR dynamic columns?

Context SQLServer 2012, Visual Studio 2010. 上下文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. poodle.getKs()给出了字符串数组中的列名列表。

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. 我试图将对象列表转换为SqlMetaData数组,然后将其强制转换为SqlDataRecord以分配给记录变量。

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. 但是,这是我的C#技能和理解能力失败的地方。 What am I doing wrong? 我究竟做错了什么?

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. 现在查看SQLServer是使用它还是使用barf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM