将读取器结果分配给多个对象

本文关键字:对象 分配 读取 结果 | 更新日期: 2023-09-27 18:12:10

我试图从数据库中获取数据并将其返回为web服务的JSON。我可以返回数据没有问题,但它似乎只返回数据的第一行,而不是其余的。我使用SqlReader,我的代码是这样的:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string EvidenceLink()
{
    var rootObject = new List<RootObject>();
    var root = new RootObject();
    string sqlQuery = @"SELECT COLUMN A, COLUMN B, COLUMN C FROM MYTABLE";

    using (var connection = new SqlConnection(Common.ConnectionString))
    {
        using (var cmd = new SqlCommand(sqlQuery, connection))
        {
            connection.Open();
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    root = new RootObject
                    {
                        children = new List<Child>
                        {
                            new Child
                            {
                                name = reader["COLUMN A"].ToString(),
                                children = new List<Child2> 
                                {
                                    new Child2
                                    {
                                        name = reader["COLUMN B"].ToString(),
                                        parent = reader["COLUMN A"].ToString(),
                                        children = new List<GrandChild> 
                                        {
                                            new GrandChild 
                                            {
                                                name = reader["COLUMN C"].ToString(),
                                                parent = reader["COLUMN B"].ToString(),
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    };
                    root.name = "ParentRoot";
                    root.parent = "null";
                    rootObject.Add(root);
                }
            }
        }
    }
    JavaScriptSerializer js = new JavaScriptSerializer();
    var strJSON = js.Serialize(rootObject);
    return strJSON;
}

我在我的数据库中有5行,我想要实现的是第一行到一个Child Object,另一个到下一个Child Object,等等。我似乎无法弄清楚为什么它只从数据库返回第一行,而不是其余的。

这是我要生成的JSON

{
"name": "Root",
"parent": "null",
"children": [
    {
        "name": "First Child",
        "children": [
            {
                "name": "Inner Child",
                "parent": "First Child",
                "children": null
            }
        ]
    },
    {
        "name": "Second Child",
        "children": [
            {
                "name": "Inner Child",
                "parent": "Second Child",
                "children": null
            }
        ]
    },
    {
        "name": "Third Child",
        "children": [
            {
                "name": "Inner Child",
                "parent": "Third Child",
                "children": null
            }
        ]
    }
]
}

结构如下:

public class GrandChild
{
public string name { get; set; }
public string parent { get; set; }
}
public class Child2
{
public string name { get; set; }
public string parent { get; set; }
public List<GrandChild> children { get; set; }
}
public class Child
{
public string name { get; set; }
public List<Child2> children { get; set; }
}
public class RootObject
{
public string name { get; set; }
public string parent { get; set; }
public List<Child> children { get; set; }
}

提前感谢你的帮助。

将读取器结果分配给多个对象

好的,让我们试试。

var rootNode = new RootObject();
rootNode.name = null;
rootNode.parent = null;
rootNode.children = new List<RootParent>();
using (var connection = new SqlConnection(Common.ConnectionString))
using (var command = new SqlCommand("SELECT ...", connection)) {
    connection.Open();
    using (var reader = command.ExecuteReader()) {
        rootNode.name = (string) reader["COLUMN A"];
        while (reader.Read()) {
            var parentNode = new RootParent();
            parentNode.name = (string) reader["COLUMN B"];
            parentNode.children = new ParentChild[] {
                new ParentChild {
                    name = (string) reader["COLUMN C"],
                    parent = (string) reader["COLUMN B"],
                    children = null
                };
            };
            rootNode.children.Add(parentNode);
        }
    }
}

实现工作的要求:

public class RootObject {
    public string name;
    public string parent;
    public List<RootParent> children;
}
public class RootParent {
    public string name;
    public List<ParentChild> children;
}
public class ParentChild {
    public string name;
    public string parent;
    public List<object> children;
}

编辑

注意,您必须返回RootObject root。根据属性参数ResponseFormat = ResponseFormat.Json,响应应自动转换/序列化

你使用什么类型的web项目来实现你的逻辑?

忘记添加parentNoderoot实例。确保添加rootNode.children.Add(parentNode); ..

一行