Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CLR函数中的返回数组_C#_Sql Server_Clr - Fatal编程技术网

C# CLR函数中的返回数组

C# CLR函数中的返回数组,c#,sql-server,clr,C#,Sql Server,Clr,我需要定期从下面的链接下载数据,并将其保存到MS数据库。我用WebClient类创建了CLR函数,但是所有的函数都在一行中,我需要将它们分开 我的想法是将数据保存在数组中,使用split并在循环中返回数据,但我不知道如何逐行返回以将其保存在数据库中 public partial class UserDefinedFunctions { private static readonly WebClient webClient = new WebClient(); [Microsoft.SqlSe

我需要定期从下面的链接下载数据,并将其保存到MS数据库。我用WebClient类创建了CLR函数,但是所有的函数都在一行中,我需要将它们分开

我的想法是将数据保存在数组中,使用split并在循环中返回数据,但我不知道如何逐行返回以将其保存在数据库中

public partial class UserDefinedFunctions
{

private static readonly WebClient webClient = new WebClient();

[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString DownloadSynop(string uri)
{
    string synop = webClient.DownloadString(uri);
    string[] lines = synop.Split(new string[] { Environment.NewLine, "\n",  "\"r" }, StringSplitOptions.None);

    for (int i=0; i<lines.Length - 1; i++)
    {
      string kod = lines[i];           

    }

    return new SqlString(kod);  //problem
}
}
公共部分类UserDefinedFunctions
{
私有静态只读WebClient WebClient=新WebClient();
[Microsoft.SqlServer.Server.SqlFunction]
公共静态SqlString下载Synop(字符串uri)
{
stringsynop=webClient.DownloadString(uri);
string[]lines=synop.Split(新字符串[]{Environment.NewLine,“\n”,“\“r”},StringSplitOptions.None);

对于(int i=0;iSQL Server并不真正支持“数组”总的来说,我建议您开发一个单独的服务或应用程序来解析网页,然后简单地将所需的数据插入一个根据您的需要格式化的表中。使用CLR查询网页意味着您必须将CLR发布为SQL Server不安全。某些组织不允许将CLR标记为在他们的服务器上不安全

如上所述,您可以创建一个表值CLR函数。这将允许您像查询标准表一样从函数中查询结果。下面是如何实现此目的的代码示例:

    public partial class UserDefinedFunctions
    {

        private struct Record
        {
            public int RowNr;
            public string SampleValue;
        }


        [SqlFunction(FillRowMethodName = "MyClrTableValuedFunction_FillRow",
            TableDefinition = "[RowNr] INT, [SampleValue] NVARCHAR(50)")]
        public static IEnumerable MyClrTableValuedFunction()
        {
            ArrayList list = new ArrayList();

            for (int sampleRowNr = 0; sampleRowNr < 100; sampleRowNr++)
            {
                Record sampleRecord = new Record();
                sampleRecord.RowNr = sampleRowNr;
                sampleRecord.SampleValue = string.Format("Sample Value: {0}", sampleRowNr);

                list.Add(sampleRecord);
            }

            return list;
        }


        public static void MyClrTableValuedFunction_FillRow(Object obj, out SqlInt32 rowNr, out SqlString sampleValue)
        {
            Record record = (Record)obj;

            rowNr = record.RowNr;
            sampleValue = record.SampleValue;
        }
    }
    SELECT   [RowNr]
            ,[SampleValue]
    FROM    [dbo].[MyClrTableValuedFunction]()