字典(字符串,对象)序列化

时间:2017-02-27 15:54:10

标签: vb.net wcf

我正在尝试编写一个wcf服务,现在对象序列化工作正常。但我必须从我的数据库中获取大量员工,将它们放入字典中,然后将其序列化以将其发送给客户端。 好吧,我使用Newsoft Jsonnet这样做,但它不起作用。我必须使用数据集而不是字典吗?

Public Function Get_All_Employee() As String Implements IService.Get_All_Employee
    Dim Query As String = "SELECT * From employee"
    Dim _Employe As Employe
    Try
        mysql_connection.Open()
        Dim sqlcmd As New MySqlCommand(Query, mysql_connection)
        Dim sqlreader As MySqlDataReader = sqlcmd.ExecuteReader()
        While sqlreader.Read()

            _Employe = New Employe()
            _Employe.id = sqlreader.Item("Id_Employee").ToString()
            _Employe.nom = sqlreader.Item("Nom").ToString()
            _Employe.prenom = sqlreader.Item("Prenom").ToString()
            _Employe.Mail = sqlreader.Item("Mail").ToString()
            _Employe.FonctionId = sqlreader.Item("Id_Fonction").ToString()

            If Not Employee_Dictionary.ContainsKey(_Employe.id) Then
                Employee_Dictionary.Add(_Employe.id, _Employe)
            End If
        End While
    Catch ex As Exception

    End Try
    Return JsonConvert.SerializeObject(Employee_Dictionary, Formatting.Indented)
End Function

编辑:

我使用了数据集而不是字典,它可以正常工作!

Public Function Get_All_Employee() As String Implements IService.Get_All_Employee
    Dim Query As String = "SELECT * From employee"
    Dim employe_dataset As DataSet = New DataSet
    Try
        mysql_connection.Open()

        Dim sqladapter As New MySqlDataAdapter(Query, mysql_connection)

        sqladapter.Fill(employe_dataset, "employee")

    Catch ex As Exception

    End Try
    Return JsonConvert.SerializeObject(employe_dataset, Formatting.Indented)
End Function

0 个答案:

没有答案