2016-07-11 72 views
0

我是WCF webservices的新成員,我需要創建一個方法,該方法可以在客戶端和數據庫中插入所有客戶端後接收一個列表。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; 
      } 

應用程序客戶端

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 

ERROR 值類型的 '列表(字符串)' 不能被轉換爲 'MyListofClients'。

有人可以幫助我?

謝謝

+0

這看起來好,你的問題是什麼? –

+0

當你不告訴我們問題是什麼時,無法幫助你。你有錯誤嗎?什麼都沒有發生?你如何主辦這項服務?你使用的是什麼DBMS? – Tim

+0

現在來自vb.net的應用程序客戶端,我也需要爲我的WCF服務傳遞一個列表。基本上我也需要從其他數據庫獲取數據,並將數據發送到List中的這個wcf。我如何做到這一點?蒂姆我使用IIS太主機和postgresql –

回答

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 
+0

但MyListOfClient其列表。我應該放哪裏? –