0

嗨,我正在通过 asmx webservices 从 sharepoint 2010 列表中获取数据,但是当我在任何列中都没有数据时,它会在 sql 上显示以下错误:让我们说“ows_ClientContactPerson”列没有任何记录,如果有的话,它会给我下面的错误,然后它会不要给我错误..

Msg 6260, Level 16, State 1, Line 2
An error occurred while getting new row from user defined Table Valued Function : 
System.ArgumentException: Column 'ows_ClientContactPerson' does not belong to table  row.
System.ArgumentException: 
 at System.Data.DataRow.GetDataColumn(String columnName)
at System.Data.DataRow.get_Item(String columnName)
at SharePointClients.GetClientsItemInfo(Object obj, SqlString& ID, SqlString& Title,     SqlString& ParentClientVertical, SqlString& NoOfStores, SqlString& Complexity, SqlString&  Location, SqlString& ClientContactPerson, SqlString& IsActive).

请在我创建程序集时查看我的 C# 代码。

using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Security.Principal;
using System.Net;
using System.Xml;

public class SharePointClients
{
[SqlFunction(SystemDataAccess = SystemDataAccessKind.Read, FillRowMethodName = "GetClientsItemInfo")]
public static IEnumerable GetClientsItems(SqlString url, SqlString listName, SqlString viewName)
{
    DataTable t = new DataTable();
    WindowsImpersonationContext ctx = null;

    WindowsIdentity id = SqlContext.WindowsIdentity;

    try
    {
        ctx = id.Impersonate();

        WSS.Lists svc = new WSS.Lists();
        svc.Url = url.ToString();

        //svc.Credentials = new NetworkCredential("barley", "pass@word1", "VS");
        svc.Credentials = CredentialCache.DefaultNetworkCredentials;
        XmlNode node = svc.GetListItems(listName.ToString(), viewName.ToString(), null, null,"150000", null, null);
        XmlTextReader rdr = new XmlTextReader(node.OuterXml,
                                    XmlNodeType.Element, null);
        DataSet ds = new DataSet();
        ds.ReadXml(rdr);
        t = ds.Tables[1];
    }
    finally
    {
        if (ctx != null)
            ctx.Undo();
    }

    return t.Rows;
}

public static void GetClientsItemInfo(
                    object obj,
                    out SqlString ID,
                    out SqlString Title,
                    out SqlString ParentClientVertical,
                    out SqlString NoOfStores,
                    out SqlString Complexity,
                    out SqlString Location,
                    out SqlString ClientContactPerson,
                    //out SqlString ClientContactEmailID,
                    //out SqlString PhoneNumber,
                    out SqlString IsActive)

{
    DataRow r = (DataRow)obj;
    ID = new SqlString(Convert.ToString(r["ows_ID"]));
    Title = new SqlString(Convert.ToString(r["ows_Title"]));
    ParentClientVertical = new SqlString(Convert.ToString(r["ows_ParentClientVertical"]));
    NoOfStores = new SqlString(Convert.ToString(r["ows_NoOfStores"]));
    Complexity = new SqlString(Convert.ToString(r["ows_Complexity"]));
    Location = new SqlString(Convert.ToString(r["ows_Location"]));
    ClientContactPerson = new SqlString(Convert.ToString(r["ows_ClientContactPerson"]));
    //ClientContactEmailID = new SqlString(Convert.ToString(r["ows_ClientContactEmailID"]));
    //PhoneNumber = new SqlString(Convert.ToString(r["ows_PhoneNumber"]));
    IsActive = new SqlString(Convert.ToString(r["ows_IsActive"]));
}
}

请看下面我的SQL函数创建代码

/****** Object:  UserDefinedFunction [dbo].[fn_GetListItemsClients]    Script Date: 10/19/2012 05:45:43 ******/
CREATE FUNCTION [dbo].[fn_GetListItemsClients](@SiteUrl [nvarchar](256), @ListName [nvarchar](256), @viewName [nvarchar](256))
RETURNS  TABLE (    
[ID] [nvarchar](50) NULL,
[Title] [nvarchar](256) NULL,
[ParentClientVertical] [nvarchar](256) NULL,
[NoOfStores] [nvarchar](256) NULL,
[Complexity] [nvarchar](256) NULL,
[Location] [nvarchar](256) NULL,
[ClientContactPerson] [nvarchar](256) NULL,
--[ClientContactEmailID] [nvarchar](70) NULL,
--[PhoneNumber] [nvarchar](70) NULL,
[IsActive] [nvarchar](256) NULL

) WITH EXECUTE AS CALLER
AS 
EXTERNAL NAME [PMTFunction].[SharePointClients].[GetClientsItems]
GO

请在下面找到我使用选择命令的位置

 select * from    dbo.fn_GetListItemsClients('http://wks10953:1000/_vti_bin/Lists.asmx','Clients','')

这真的很重要,请帮助我..

4

1 回答 1

0

检查 DataRow 是否包含所需的列,如下所示,对每列使用以下代码库以避免将来出现错误。

if (r.Table.Columns.Contains("ows_ClientContactPerson"))

ClientContactPerson = new SqlString(Convert.ToString(r["ows_ClientContactPerson"]));

else

ClientContactPerson = new SqlString(string.Empty);
于 2013-10-24T05:09:42.090 回答