使用WCF REST服务在sql数据库中插入数据? [英] Insert data in sql database using WCF REST services ?
本文介绍了使用WCF REST服务在sql数据库中插入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I want to insert data in SQL database using REST services (JSON response)and I have implemented them.But I couldn't understand how to insert data without adding any client .Can I insert data directly from service-url without using client app.Because my client application is android and I want to invoke that rest service from android and insert in database..How can I insert data without using client application?
My code is :
Iservice1.cs
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(UriTemplate = "Appointments", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
void CreateAppointment(Appointment appointment);
}
[DataContract]
public class Appointment
{
[DataMember]
public string UserName { get; set; }
[DataMember]
public string AppointmentBookedForCompany { get; set; }
[DataMember]
public string Date { get; set; }
[DataMember]
public string TimeStart { get; set; }
[DataMember]
public string TimeEnd { get; set; }
[DataMember]
public string PhoneNo { get; set; }
}
Service1.cs:
public void CreateAppointment(Appointment appointment)
{
string str = "Data Source = . Initial Catalog = Appointment; User ID = sa; Password = 123";
SqlConnection cn = new SqlConnection(str);
SqlCommand cm = new SqlCommand();
string username = appointment.UserName;
string appointmentbookedForcompany = appointment.AppointmentBookedForCompany;
string date = appointment.Date;
string timestart = appointment.TimeStart;
string timeend = appointment.TimeEnd;
string phoneno = appointment.PhoneNo;
cm.CommandText = "insert into BookAppointment values(@username,@appointmentbookedForcompany,@date,@timestart,@timeend,@phoneno)";
cm.Parameters.Add(new SqlParameter("@username", SqlDbType.VarChar,50)).Value = username;
cm.Parameters.Add(new SqlParameter("@appointmentbookedForcompany", SqlDbType.VarChar,50)).Value = appointmentbookedForcompany;
cm.Parameters.Add(new SqlParameter("@date", SqlDbType.VarChar,50)).Value = date;
cm.Parameters.Add(new SqlParameter("@timestart", SqlDbType.VarChar,50)).Value = timestart;
cm.Parameters.Add(new SqlParameter("@timeend", SqlDbType.VarChar,50)).Value = timeend;
cm.Parameters.Add(new SqlParameter("@phoneno", SqlDbType.VarChar,50)).Value = phoneno;
cm.Connection = cn;
cn.Open();
cm.ExecuteNonQuery();
cn.Close();
cm.Dispose();
}}
My config file: <system.serviceModel>
<services>
<service name="RESTAppointments.Service1" behaviorConfiguration="RESTAppointments.hk">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/RESTAppointments/Service1/"/>
</baseAddresses>
</host>
<endpoint address="http://localhost:8732/Design_Time_Addresses/RESTAppointments/Service1/ed" binding="webHttpBinding" contract="RESTAppointments.IService1" behaviorConfiguration="WebHttpBehavior"/>
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="RESTAppointments.hk">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebHttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
How can I proceed further ?
推荐答案
您可以浏览WCF服务和xml中的传递,以在没有客户端的情况下将其保存.
您也可以使用WCF测试客户端工具- http://msdn.microsoft.com/en-us/library/bb552364.aspx [ ^ ].
You can browse the WCF service and the pass in an xml to save it without a client.
You can also use the WCF test client tool - http://msdn.microsoft.com/en-us/library/bb552364.aspx[^].
这篇关于使用WCF REST服务在sql数据库中插入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文