如何进行列表的Web方法<;字符串[]>;在C#中

本文关键字:gt 字符串 lt 列表 何进行 Web 方法 | 更新日期: 2023-09-27 18:28:22

我有Web方法。

[WebMethod]
public List<string[]> getObjective()
{
    List<string[]> objectiveStringList = new List<string[]>();
    con.dbConnect();
    objectiveStringList = con.getObjective();
    con.dbClose();
    return objectiveStringList;
}

还有查询。

    public List<string[]> getObjective()
{
    string strCMD = "SELECT objective FROM CorrespondingBall WHERE objective IS NOT NULL";
    SqlCommand cmd = new SqlCommand(strCMD, conn);
    SqlDataReader dr = cmd.ExecuteReader();
    List<string[]> objectiveStringList = new List<string[]>();
    while (dr.Read())
    {
        objectiveStringList.Add((string[])dr["objective"]);
    }
    return objectiveStringList;
}

但是,显示了错误消息"HTTP 500内部服务器错误"。我已经用列表字节数组尝试过了,没有错误。有人知道我该怎么解决吗?

仅供参考:我正在为Windows Phone 7做一个应用程序。

如何进行列表的Web方法<;字符串[]>;在C#中

您的代码不正确。

(string[])dr["objective"]

在您的web方法中,您将在这一行得到异常。因为无法将CCD_ 1直接强制转换为CCD_ 2。

编辑:

正如你在下面的评论中提到的,数据类型是NVARCHAR,所以你可以做这个

objectiveStringList.Add((string)dr["objective"]);

试试这个:

    public List<string> getObjective()
    {
        string strCMD = "SELECT objective FROM CorrespondingBall WHERE objective IS NOT NULL";
        SqlCommand cmd = new SqlCommand(strCMD, conn);
        SqlDataReader dr = cmd.ExecuteReader();
        List<string> objectiveStringList = new List<string>();
        while (dr.Read())
        {
            objectiveStringList.Add(dr["objective"].ToString());
        }
        return objectiveStringList;
    }