using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WCFRestExample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface ISurvey
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetSurveys")]
string GetSurveyData();
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "SetAnswer")]
SurveyData InsertingAnswer(SurveyData rData);
[OperationContract]
[WebInvoke(UriTemplate = "{id}/{name}", Method = "PUT")]
bool UpdateData(string id, string name);
[OperationContract]
[WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
bool DeleteData(string id);
}
[DataContract(Namespace = "http://onlinenettrainings.blogspot.in/")]
//[DataContract]
public class SurveyData
{
[DataMember]
public string SurveyID { get; set; }
[DataMember]
public string SurveyName { get; set; }
[DataMember]
public string QuestionID { get; set; }
[DataMember]
public string QuestionText { get; set; }
[DataMember]
public string QuestionTypeID { get; set; }
[DataMember]
public string QuestionType { get; set; }
[DataMember]
public string AnswerID { get; set; }
[DataMember]
public string Answers { get; set; }
}
[DataContract(Namespace = "http://onlinenettrainings.blogspot.in/")]
//[DataContract]
public class SData
{
[DataMember]
public string SID { get; set; }
}
[DataContract(Namespace = "http://onlinenettrainings.blogspot.in/")]
public class SDataResponse
{
[DataMember]
public string name { get; set; }
}
}
Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
using System.IO;
using System.Diagnostics;
namespace WCFRestExample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Survey : ISurvey
{
int value;
public string GetSurveyData()
{
string strXML = null;
XmlDocument doc = null;
SurveyData objSurveyData = new SurveyData();
DataSet objDataSet = null;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("select * from SurveyTable", con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
cmd.CommandType = CommandType.Text;
SqlDataAdapter objDataAdaptor = new SqlDataAdapter();
objDataAdaptor.SelectCommand = cmd;
objDataSet = new DataSet();
objDataAdaptor.Fill(objDataSet);
objDataSet.GetXmlSchema();
strXML = objDataSet.GetXml();
doc = new XmlDocument();
doc.LoadXml(strXML);
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
con.Close();
}
return strXML;
}
/*
* Insert into SurveyTable(answer) values(@answer)
*/
public SurveyData InsertingAnswer(SurveyData rData)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("sp_InsertAnswers", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@AnswerDesc", SqlDbType.VarChar).Value = rData.Answers;
try
{
con.Open();
value = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
// strSurveyName = ex.Message.ToString();
}
finally
{
con.Close();
}
var response = new SurveyData
{
SurveyID = rData.SurveyID,
SurveyName = rData.SurveyName
};
return response;
}
public bool UpdateData(string id, string name)
{
bool result = false;
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SampleDB;Data Source=localhost;");
SqlCommand cmd = new SqlCommand("update SurveyTable set SurveyName='" + name + "' where SurveyID='" + id + "'", con);
try
{
cmd.CommandType = CommandType.Text;
con.Open();
value = cmd.ExecuteNonQuery();
if (value == 1)
result = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return result;
}
public bool DeleteData(string id)
{
bool result = false;
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SampleDB;Data Source=localhost;");
SqlCommand cmd = new SqlCommand("delete from SurveyTable where SurveyID='" + id + "'", con);
try
{
cmd.CommandType = CommandType.Text;
con.Open();
value = cmd.ExecuteNonQuery();
if (value == 1)
result = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return result;
}
}
}