WCF接收对象列表并在数据库

时间:2016-07-11 16:29:33

标签: c# web-services wcf listobject

我是WCF网络服务的新手,我需要创建一个方法,接收一个包含非常客户端的列表,并在数据库中插入所有客户端之后。

我已经实施了一些搜索

IService.cs

public interface IService1
{
   [OperationContract]
    int InsertClients(MyListofClients clients);
}

[DataContract]
public class MyListofClients
{
    [DataMember]
    List<Client> Clients { get; set; }
}

[DataContract]
public class Client
{
    [DataMember]
    public int clientId { get; set; }
    [DataMember]
    public string ClientName { get; set; }
    [DataMember]
    public List<Client> Clients { get; set; }
}
}

Servic1.svc

   public int InsertClients(MyListofClients clients)
    {
        int res, result;
        using (NpgsqlConnection conn = new NpgsqlConnection(connStringFarm))
        {
            conn.Open();
            List<MyListofClients> firstStringList = new        List<MyListofClients>();
            string cmdStr = String.Format("Insert Into table (x1,x2)" +
                                           " VALUES(@x1,@x2)");

            foreach (var item in firstStringList)
            {
                NpgsqlCommand cmd = new NpgsqlCommand(cmdStr, conn);
                result = cmd.ExecuteNonQuery();
            }

            conn.Close();
            return 0;
           }

App Client

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles    Button1.Click
    Dim db_comm As New NpgsqlCommand
    Dim Reader As NpgsqlDataReader


    Dim query As String = "Select code1,name1 from clientes"
    db_comm.CommandText = query
    db_comm.Connection = conn
    conn.open
    Reader = db_comm.ExecuteReader

    Dim list As New List(Of String)

    While Reader.Read
        list.Add(Reader.GetString("code1"))
        list.Add(Reader.GetString("name1"))
    End While

    Dim API As APICS.Service1Client = New APICS.Service1Client()
    API.InsertClientsAsync(list)
End Sub

错误     类型的值&#39; List(Of String)&#39;无法转换为&#39; MyListofClients&#39;。

有人可以帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

您的服务方法期待MyListOfClient对象,但您传递的是字符串列表。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles    Button1.Click
Dim db_comm As New NpgsqlCommand
Dim Reader As NpgsqlDataReader


Dim query As String = "Select code1,name1 from clientes"
db_comm.CommandText = query
db_comm.Connection = conn
conn.open
Reader = db_comm.ExecuteReader

Dim list As New List(Of String) -->  wrong; this should be MyListOfClient

While Reader.Read
    list.Add(Reader.GetString("code1"))
    list.Add(Reader.GetString("name1"))
End While

Dim API As APICS.Service1Client = New APICS.Service1Client()
API.InsertClientsAsyn(list) --> wrong; this should be MyListOfClient