Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.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表值函数调用Web服务_C#_User Defined Functions_Sqlclr - Fatal编程技术网

C# 使用CLR表值函数调用Web服务

C# 使用CLR表值函数调用Web服务,c#,user-defined-functions,sqlclr,C#,User Defined Functions,Sqlclr,我试图将一些TVR整合在一起,但我始终得到以下错误: 从用户定义的表值函数获取新行时出错: System.InvalidCastException:无法将类型为“System.Char”的对象强制转换为类型为“ResultRow” CLR.WorkFunctions.FillRow处的System.InvalidCastException(对象结果OBJ, SqlString和policyURL、SqlString和fileCachePath、SqlString和identity、, SqlSt

我试图将一些TVR整合在一起,但我始终得到以下错误:

从用户定义的表值函数获取新行时出错:

System.InvalidCastException:无法将类型为“System.Char”的对象强制转换为类型为“ResultRow”

CLR.WorkFunctions.FillRow处的System.InvalidCastException(对象结果OBJ, SqlString和policyURL、SqlString和fileCachePath、SqlString和identity、, SqlString和格式、SqlString和用户名、SqlString和密码、SqlString和数据)

我试图调用一个web服务,该服务对传递的数据进行一些处理,然后将返回的值加载到枚举器中。然而,如上所示,我反复遇到一个输入错误,在这一点上我不知道为什么。我的想法是,它会变得不安,因为我试图用字符串而不是SqlStrings来填充行,所以我改变了这一点。但是,web服务只接受作为字符串的参数,因此我必须在web服务调用中转换它们。这是我的密码:

public class WorkFunctions
    {
        private class ResultRow
        {
            public string PolicyURL;
            public string FileCachePath;
            public string Identity;
            public string Format;
            public string UserName;
            public string Password;
            public string Data;
        }
    [Microsoft.SqlServer.Server.SqlFunction(
               FillRowMethodName = "FillRow",
               TableDefinition = "policyName nvarchar(500), fileCache nvarchar(500), 
               identity nvarchar(500), format nvarchar(500), userName nvarchar(500), 
               password nvarchar(500), data nvarchar(500)")]
            public static IEnumerable Work(SqlString serviceURL, SqlString policyURL, SqlString fileCachePath, SqlString identity, SqlString format, SqlString userName, SqlString password, SqlString data)
           {
              IEnumerable values;

              try
                {
                    CLR.Work.WorkService.WorkMethods workMethods = new CLR.Work.WorkService.WorkMethods();
                    workMethods.Url = Convert.ToString(serviceURL);
                    values = workMethods.Work(Convert.ToString(policyURL), Convert.ToString(identity), Convert.ToString(format), Convert.ToString(userName), Convert.ToString(password), Convert.ToString(fileCachePath), Convert.ToString(data));
                }
              catch (Exception ex)
                {
                   return "Failed to return data";
                }

              return values;
          }

            public static void FillRow(Object resultObj, 
              out SqlString policyURL, out SqlString fileCachePath, 
              out SqlString identity, out SqlString format, 
              out SqlString userName, out SqlString password, 
              out SqlString data)
                 {
                   ResultRow resultRow = (ResultRow)resultObj;

                   policyURL = new SqlString(resultRow.PolicyURL);
                   fileCachePath = new SqlString (resultRow.FileCachePath);
                   identity = new SqlString (resultRow.Identity);
                   format = new SqlString (resultRow.Format);
                   userName = new SqlString (resultRow.UserName);
                   password = new SqlString (resultRow.Password);
                   data = new SqlString (resultRow.Data);
            
                }
      }
}
这是我第一次与TVF合作,我现在有点不知所措,不知是什么原因导致了这个问题。如果有人能提供一些想法,我将不胜感激


谢谢。

最直接问题的最可能原因是
workMethods.Work()
方法返回的类型与您定义的
ResultRow
不相同。
workMethods.Work()
返回并分配给
值的任何内容都将作为结果返回,并作为
Object
类型的第一个参数发送到FillRowMethod。沿着这些思路,您可能应该删除
IEnumerable值
返回值行,然后执行以下操作:

ResultRow _Row = new ResultRow();

...

_Row = workMethods.Work(...);

yield return _Row;
另外,在任何
SqlString
输入参数上都不需要使用
Convert.ToString()
。所有的
Sql*
类型都有一个
.Value
属性,该属性返回预期的.NET类型。所以只需使用
serviceURL.Value
policyURL
,等等

最后,为什么输出字段与输入参数几乎相同?似乎是个奇怪的签名


另外,如果您希望/需要有关使用SQLCLR的更多信息,我将在SQL Server Central上就此主题撰写一系列文章:。该网站确实需要免费注册才能查看其内容(但这是值得的,因为在各种文章和论坛中有大量信息)。

谢谢,这很有意义。web服务函数返回一个字符串,更改web服务不是一个选项,因此我也不能将其更改为ResultRow类型。最好的方法是使用字符串列表吗?另外,感谢大家对知识库的了解。听起来非常有用。@user2498668如果它返回一个字符串,它是XML字符串还是JSON字符串?如果是XML,则可以在C代码或T-SQL中解析,如果是JSON,则需要在C中解析,除非您计划使用SQL Server 2016。但是目前你的TVF正在返回一个字段的结果集,如在 ReultRW中定义的,因此你要么需要把数据解析成那个,要么考虑一个新的结构。为什么要列出字符串?除非您的意思是对Web服务的返回值进行简单的拆分,在这种情况下也可以。如果你从网络服务上发布了报税表,我可以说得更具体一些。