2014-03-13 87 views
1

我嘗試通過WCF服務創建一個插入操作。這是我所做的。首先,我會告訴你我的項目結構如何通過MYSQL通過WCF服務插入操作?

project structure

每個文件的解釋:

IServiceClient.cs包含服務合同的組

[ServiceContract] 
public interface IServiceClient 
{ 
    [OperationContract] 
    void InsertMaster(); 
} 

Service.cs包含數據合同

[DataContract] 
public class Service 
{ 
    [DataMember] 
    public string Id; 
    [DataMember] 
    public string Submitter; 
    [DataMember] 
    public string Comments; 
    [DataMember] 
    public DateTime TimeSubmitted; 
} 

ServiceClient.cs含有,如插入業務邏輯來操作MySQL

public void InsertMaster() 
{ 
    string query = "INSERT INTO movies (id, submitter, comments, time) VALUES(id, submitter, comments, time)"; 
    //open connection 
    connection.Open(); 
    //create command and assign the query and connection from the constructor 
    MySqlCommand cmd = new MySqlCommand(query, connection); 
    //Execute command 
    cmd.ExecuteNonQuery(); 
    //close connection 
    connection.Close(); 
} 

,我的問題是,如何將數據合約場傳遞給ServiceClient.cs這樣我就可以寫一些MySQL查詢?該「查詢字符串」只是我的演示,我想從合同數據

給我的第二個問題是如何將數據合同區加載到WCF測試客戶端將該值插入?我需要測試它即將工作或不是我用它在我的客戶端項目

+0

公共無效InsertMaster(服務數據)和OperationContract的空隙InsertMaster(服務數據)是相同的; –

+0

爲什麼不讓你的數據合約接受服務類型,在ServiceClient類中實現它。然後,當你運行你的WCF測試客戶端,您應該能夠將其數據填寫服務類別,並提交,如果不是你可以隨時請求XML從XML選項卡底部的複製,然後使用的程序等PostMan在Chrome中填寫您的數據並將其發佈到服務中。 – Bearcat9425

回答

1

該如何需要修改前:一旦你測試使用WCF測試客戶端

[ServiceContract] 
public interface IServiceClient 
{ 
    [OperationContract] 
    void InsertMaster(Service ServiceObj); 
} 

[DataContract] 
public class Service 
{ 
    [DataMember] 
    public string Id; 
    [DataMember] 
    public string Submitter; 
    [DataMember] 
    public string Comments; 
    [DataMember] 
    public DateTime TimeSubmitted; 
} 

public void InsertMaster(Service ServiceObj) 
{ 
    string query = "INSERT INTO movies (id, submitter, comments, time) VALUES(ServiceObj.id, ServiceObj.submitter, ServiceObj.comments, ServiceObj.time)"; 
    //open connection 
    connection.Open(); 
    //create command and assign the query and connection from the constructor 
    MySqlCommand cmd = new MySqlCommand(query, connection); 
    //Execute command 
    cmd.ExecuteNonQuery(); 
    //close connection 
    connection.Close(); 

} 

合同將會出現。