[英]WCF Service to insert data to database in Visual Studio C#
I have created a WCF service and have a SQL database set up and I want to create a service which allows me to have data inserted into the database. 我已经创建了WCF服务并设置了SQL数据库,并且想创建一个服务,使我可以将数据插入数据库中。
Here is my WCF service and the method I have used to do this: 这是我的WCF服务以及我用来执行此操作的方法:
namespace WcfService1
{
[ServiceContract]
interface IBookingsService1
{
[OperationContract]
bool InsertBooking(Booking obj);
}
[DataContract]
public class Booking
{
[DataMember]
public int bk_BookingID;
[DataMember]
public string bk_StudentID;
[DataMember]
public string bk_Forename;
[DataMember]
public string bk_Surname;
[DataMember]
public string bk_EventID;
[DataMember]
public string bk_EventTitle;
[DataMember]
public string bk_EventDesc;
[DataMember]
public string bk_EventDate;
[DataMember]
public string bk_EventTime;
[DataMember]
public string bk_EventLocation;
}
svc SVC
namespace WcfService1
{
public class BookingsService1 : IBookingsService1
{
public bool InsertBooking(Booking obj)
{
bookingList.Add(obj);
return true;
}
public static List<Booking> bookingList = new List<Booking>()
{
new Booking {bk_BookingID = 1,
bk_StudentID = "11432857",
bk_Forename = "Mani",
bk_Surname = "Singh",
bk_EventTitle = "Bonk",
bk_EventDesc = "sdfsdfsd",
bk_EventDate = "11/11/14",
bk_EventTime = "21.30",
bk_EventLocation = "Forum"}
};
}
public static class LinqUpdates
{
public static void Update<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
action(item);
}
}
At the moment I am guessing when the service is used the data simply gets stored within the public class 'booking' but I want it to go into a table called 'BookingTable1'. 目前,我猜测使用该服务时,数据只是存储在公共类“ booking”中,但我希望它进入名为“ BookingTable1”的表中。
How can I implement this? 我该如何实施? Please help me as I have been trying to figure it out for quite a while now.
请帮助我,因为我已经尝试了很长时间了。 Thank You.
谢谢。
Your implementation is not wrong. 您的实现没有错。 Use any database operation within your insert operation.
在插入操作中使用任何数据库操作。 For example use entity framework with wcf (ef creates tables as contracts or you can force that to do)
例如,将实体框架与wcf一起使用(ef将表创建为合同,或者您可以强制这样做)
public bool InsertBooking(Booking obj)
{
using(var db = new Model1Entities())
{
db.Bookings.Attach(obj);
db.Bookings.Add(obj);
db.SaveChanges();
}
return true; // or return obj
}
Otherwise you can use any orm like that. 否则,您可以使用任何这样的orm。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.