Ke Vin
Ke Vin

Reputation: 3760

How to insert operation through WCF Service with MYSQL?

I try to create a insert operation through WCF Service. Here is what i have done. First of all i will show you my project structure

project structure

Each file explanation :

IServiceClient.cs contain, group of Service Contract

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

Service.cs contain group of Data Contract

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

ServiceClient.cs contain, business logic like Insert to MYSQL operation

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();
}

and my problem is, how to pass Data Contract field to ServiceClient.cs so i can write some MYSQL query? that "string query" is just my demo that i want to insert from the value that give by data contract

and my second problem is how to load data contract field into WCF Test Client? i need to test it about to work or not before i do use it in my Client side project

Upvotes: 1

Views: 1267

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222682

This how you need to modify :

[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();

}

Contract will appear once you test using WCF test client.

Upvotes: 1

Related Questions