简体   繁体   English

WCF服务将数据插入Visual Studio C#中的数据库

[英]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.

相关问题 使用C#中的Visual Studio通过MySQL将数据插入数据库 - Insert Data into database via mySQL using visual studio in C# 基于服务的数据库Visual Studio C# - Service based database visual studio c# 如何使用Visual Studio和C#将数据插入数据库并将其保存到数据库中? - How can I insert and save data into database using Visual Studio and C#? 如何使用C#将数据插入SQL Sever而不使用数据库向导(visual studio) - How To insert data into SQL Sever with C# without using the database wizard(visual studio) 使用C#在Visual Studio中插入,更新和删除Microsoft Access数据库 - Microsoft Access Database, Insert, Update and Delete in Visual Studio with C# 无法将数据插入表C#visual studio 2013 - Cannot insert the data into the table C# visual studio 2013 无法使用C#visual Studio 2008将数据插入表中 - Cannot insert the data into the table using C# visual studio 2008 在ASP.NET C中将数据插入Visual Studio LocalDB# - Insert data into Visual Studio LocalDB in ASP.NET C# System.Data.SqlClient.SqlException:&#39;尝试在数据库中插入数据时无效的对象名称(C#,Visual Studio) - System.Data.SqlClient.SqlException: 'Invalid object name when trying to insert data in database (C#, Visual Studio) 无法从Visual C#将数据插入MS Access数据库 - Unable to insert data into MS Access database from Visual C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM