Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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
Posted

1 solution

 
Share this answer
 
v2
Comments
Uday P.Singh 11-Dec-11 11:37am    
5ed for very nice links
Monjurul Habib 11-Dec-11 20:00pm    
thank you Uday
thatraja 11-Dec-11 11:50am    
Nice bunch, 5!
Monjurul Habib 11-Dec-11 20:00pm    
thank you Raja

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900