如何在WCF应用程序中插入,删除和更新? [英] How do I insert, delete and update in a WCF application?

查看:95
本文介绍了如何在WCF应用程序中插入,删除和更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个控制台应用程序,该应用程序使用WCF和SQL CRUD从鱼类数据库中的库存表中检索信息.现在,我还想从控制台应用程序向数据库中插入,删除和更新信息.我将如何做到这一点?我是否需要在服务合同中添加其他运营合同,还是可以将其他CRUD添加到我的其他合同之一中?


这是到目前为止的代码:


I''ve made a console application which uses WCF and SQL CRUD to retrieve information from a stock table in a fish database. What I want to do now is also insert, delete and update information into the database from the console app. How would I be able to do this? Would I need to add additional operation contracts into the service contract or can I just add the additional CRUD into one of my other contracts?


Heres the code I''ve got so far:


using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading;

    // Service Interface
    namespace FishService.Contract
    {
        [ServiceContract]
        public interface IService
        {
            [OperationContract]
            string Ping(string name);

            [OperationContract]
            ArrayList getFish(string type);
        }
    }

    // Actual Service

    namespace FishService.Server
    {
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

        class ServiceImplementation : FishService.Contract.IService
        {

            public string Ping(string name)
            {
                Console.WriteLine("SERVER recieved: {0}", name);
                return "2" + name;
            }

            public ArrayList getFish(string type)
            {

                ArrayList response = new ArrayList();
                try
                {
                    string cs = "Data Source=MASTER\\MASTER;Initial Catalog=TylerHood;Integrated Security=True;Pooling=False";

                    SqlConnection cn = new SqlConnection(cs);
                    SqlCommand cm = cn.CreateCommand();

                   cm.CommandText = @"SELECT InStock FROM [dbo].[Stock] WHERE FishName = ''Cod'';";

                   // string str = "SELECT InStock FROM [dbo].[Stock] WHERE FishName = " + "\''" + type + "\''" + "


                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cm;
                    DataSet ds = new DataSet();

                    ds.Clear();

                    cn.Open();

                    da.Fill(ds, "Stock");

                    DataTable dt = ds.Tables["Stock"];

                    foreach (DataRow dr in dt.Rows)
                    {
                        response.Add(dr["InStock"].ToString());

                    }

                    cn.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error:" + e.Message);
                }
                finally
                {
                }

                return response;
            }

        }

      public class Program
        {
            private static AutoResetEvent stopFlag = new AutoResetEvent(false);

           public static void Main()
            {
                ServiceHost svh = new ServiceHost(typeof(ServiceImplementation));

                svh.AddServiceEndpoint(
                    typeof(FishService.Contract.IService),
                    new NetTcpBinding(),
                    "net.tcp://localhost:8000");
                svh.Open();

                stopFlag.WaitOne();

                
                svh.Close();

            }

            public static void Stop()
            {
                stopFlag.Set();
            }
        }
    } // end of namespace fishservice.server

//Client
    namespace FishStockClient
    {
        class Program
        {
             static void Main()
            {
                System.Threading.Thread hs = new System.Threading.Thread (FishService.Server.Program.Main);
                hs.IsBackground = true;
                hs.Start();
                Thread.Sleep(1000);

                ChannelFactory<FishService.Contract.IService> scf;

                scf = new ChannelFactory<FishService.Contract.IService>(new NetTcpBinding(), "net.tcp://localhost:8000");
                FishService.Contract.IService s;

                s = scf.CreateChannel();

                while (true)
                {
                    Console.Write("Type of Fish: ");
                    string fish = Console.ReadLine();

                    if (fish == "") break;

                    ArrayList la = new ArrayList();
                    la = s.getFish(fish);

                    foreach (var item in la)
                    {
                        Console.WriteLine( item.ToString() );
                    }
                }

                 //---

                (s as ICommunicationObject).Close();

                FishService.Server.Program.Stop();

                hs.Join();
             
             
             }
        }

    }// end of Fishstockclient

推荐答案

以下链接将为您提供帮助:

使用RESTful WCF服务和JavaScript实施CRUD操作
使用WCF REST,以Web服务作为提供者和JQuery,以Javascript作为使用者的企业站点中的数据库CRUD操作:零回发
如何在ASP中执行CRUD操作.net使用WCF?
在WCF中启用REST的服务的分步教程
Following links will help you:

Implement CRUD Operations using RESTful WCF Service and JavaScript
Database CRUD Operations In Business Sites With WCF REST, Web Services As Provider And JQuery, Javascript As Consumer : Zero Postback
How to do CRUD operation in asp.net using WCF?
Step by Step tutorial of REST Enabled Service in WCF


这篇关于如何在WCF应用程序中插入,删除和更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆