1

我正在构建一个与 SQL 服务器数据库通信的简单 ASP.NET 应用程序。在一个单独的项目中,我拥有由 Visual Studio 生成的数据集。我正在尝试公开一个将显示数据库中所有用户的 API,但我遇到了一个异常。

这是代码:

public class UserController : ApiController {

    public IEnumerable<GWDataSet.usersRow> GetAllUsers() {
        GWDataSet gw = new GWDataSet();
        usersTableAdapter adapter = new usersTableAdapter();
        adapter.Fill(gw.users);

        return gw.users.AsEnumerable();
    }
}

这是一个例外:

Type 'System.Data.DataRow' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

除了手动编辑系统生成的代码之外,有没有办法解决这个问题?我担心下次我对数据集进行更改时,它会覆盖我添加的所有数据合同元素。我认为有办法做到这一点,但我似乎找不到。

谢谢!

4

2 回答 2

2

这是因为您不能共享 DataRow 对象。它们不可序列化。如果你想共享它,你应该创建 DTO 对象。从服务、API 等共享对象是一种很好的做法。Tru 将您的 DataRow 转换为 DTO 对象。尝试类似:

创建你的类作为这个 DTO,例如:

[Serializable]
public class UserDTO
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string Birthday { get; set; }
    /* other properties you need */
}

在你的 web api 中,试试这个:

public class UserController : ApiController {

    public IEnumerable<UserDTO> GetAllUsers() {
        GWDataSet gw = new GWDataSet();
        usersTableAdapter adapter = new usersTableAdapter();
        adapter.Fill(gw.users);

        List<UserDTO> list = new List<UserDTO>();

        foreach(DataRow row in gw.users.Rows) 
        {
            UserDTO user = new UserDTO();
            user.FirstName = row["Name"].ToString();
            // fill properties

           list.Add(user);
        }

        return list;
    }
}
于 2012-12-22T21:48:56.700 回答
0

离开 Felipe 的回答(在记住我需要处理可怕的 DBNull 之后),这是我构建的最终方法:

    public IEnumerable<GW.Entities.user> GetAllUsers() {
        try {
            GWDataSet gw = new GWDataSet();
            List<GW.Entities.user> users = new List<user>();
            usersTableAdapter adapter = new usersTableAdapter();

            adapter.Fill(gw.users);

            foreach (GWDataSet.usersRow row in gw.users.Rows) {
                users.Add(new GW.Entities.user {
                    UserId = row.IsNull("UserId") ? 0 : row.UserId,
                    UserName = row.IsNull("UserName") ? "" : row.UserName,
                    EmailAddress = row.IsNull("EmailAddress") ? "" : row.EmailAddress,
                    UserPasswordLastChange = row.IsNull("UserPasswordLastChange") ? DateTime.MinValue : row.UserPasswordLastChange,
                    LastLogin = row.IsNull("LastLogin") ? DateTime.MinValue : row.LastLogin,
                    StatusCd = row.IsNull("StatusCd") ? "" : row.StatusCd,
                    LastTimestamp = row.IsNull("LastTimestamp") ? null : row.LastTimestamp
                });
            }

            return users;
        } catch (Exception ex) {
            Debug.WriteLine(ex.ToString());
            return null;
        }
    }

这给了我一个输出:

<ArrayOfuser xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/GW.Entities">
    <user>
        <EmailAddress>seraieis@gmail.com</EmailAddress>
        <LastLogin>2012-12-19T20:48:26.41</LastLogin>
        <LastTimestamp>AAAAAAAARlI=</LastTimestamp>
        <StatusCd>A</StatusCd>
        <UserId>1</UserId>
        <UserName>seraieis</UserName>
        <UserPasswordLastChange>0001-01-01T00:00:00</UserPasswordLastChange>
    </user>
</ArrayOfuser>
于 2012-12-23T14:19:02.837 回答