1

我试图解决这个问题,但是当我这样做时我得到了一个疯狂的错误?

这是我的第一个方法 --> 其中包含 SQLQUERY

[WebMethod]
 public ArrayList getAllaEgenskaperforenmall(string mall_id)
 {
     ArrayList dbFetch;
    // string parameter1 = "mall_namn";
     string sqlReadQuery = "SELECT DISTINCT d.egenskaper_namn FROM(SELECT egen.egenskaper_id, egen.egenskaper_namn, kopp.mall_id, kopp.egenskap_id, emall.mall_id as egenskapensmallid, emall.mall_namn FROM t_egenskaper as egen, t_kopplingmallegenskaper as kopp, t_egenskapsmall as emall  WHERE kopp.mall_id = 1  AND kopp.egenskap_id = egen.egenskaper_id) as d";

     dbFetch = executeReadSqlQueryArray(sqlReadQuery);

     return dbFetch;

 }

然后我尝试建立一个可以为我获取数据的通用网络方法。

 public ArrayList executeReadSqlQueryArray(string sqlToFetchData, string parameter1 = "", string parameter2 = "", string parameter3 = "")
{
     SqlConnection conn = new SqlConnection(mConnectionstring);
     SqlCommand command = new SqlCommand();
     SqlDataReader sqlReader;
     ArrayList dataReadFromDatabase = new ArrayList();

     command.Connection = conn;
     command.Connection.Open();
     command.CommandText = sqlToFetchData;


     sqlReader = command.ExecuteReader();

    while(sqlReader.Read())
    {

        object[] values = new object[sqlReader.FieldCount];
        sqlReader.GetValues(values);
        dataReadFromDatabase.Add(values);
    }
    command.Connection.Close();
    return dataReadFromDatabase;

}

我得到的错误是这个?有趣的是,昨天我想我设法让它如我所愿!

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Object[] may not be used in this context.
   at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write8_ArrayOfAnyType(Object o)
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayListSerializer2.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()
4

1 回答 1

1

问题不在于您的代码从数据库中获取,而是 WebMethod 试图将其转换为字符串,但它不知道它是什么数据类型。目前它被转换为对象(这可能意味着一百万种类型中的一种)。

如果数据类型是简单的奇异数据类型,请尝试从 ArrayList 交换并使其成为通用数组,例如

List<String>

问题是您的数组列表当前包含一个对象列表,而序列化处理器不知道如何处理它们。您可以将它们从对象硬转换为一种serializable格式,以帮助或切换到一个类(并实现该类serializable)。

例如

[Serializable]
public class ProfileBasics
{
    /// <summary>
    ///   Gets or sets the about me section
    /// </summary>
    [XmlElement("AboutMe")]
    public string AboutMe {get; set;}

    /// <summary>
    ///   Gets or sets the city name for the zip code
    /// </summary>
    [XmlElement("City")]
    public string City {get; set;}
 }

然后你可以这样做List<ProfileBasics>,你的网络服务应该仍然可以工作。

于 2012-07-06T22:18:21.107 回答