我试图解决这个问题,但是当我这样做时,我遇到了一个疯狂的错误?
这是我的第一种方法 - >其中包含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;
}
然后我尝试设置一个可以为我提取数据的常规web方法。
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()
答案 0 :(得分:1)
问题不在于您的代码是否从数据库中获取,而是WebMethod试图将其转换为字符串,但它不知道它是什么数据类型。目前它的演员阵容为Object(可能意味着百万种类型中的一种)。
如果数据类型是一个简单的单数据类型,请尝试从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>
,您的网络服务仍然有用。