如何在客户端应用程序中添加服务。 [英] how to add the service in client application.

查看:83
本文介绍了如何在客户端应用程序中添加服务。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个服务,因为我通过选择employeeser id来编写获取员工详细信息的方法。我们如何将该服务添加到aspx代码中。并且要在gridview中获取emp详细信息,我们需要在服务或代码中写入sql命令。





IService1.cs:

I have created a service in that i wrote the methods to get the employee details by selecting the employeer id . how can we add that service to aspx code. and also to get that emp details in gridview we need to write sql command where it should be in the service or in the code.


IService1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Configuration;

namespace Employee
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        DataSet GetEmployeerid();

        [OperationContract]
        List<CompositeType> GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations
    [DataContract]
    public class CompositeType
    {
          int   empid;
         string ename;
         string address;
         string city ;
         string state ;
         string employeerid;

        [DataMember]
         public int Empid
        {
            get { return empid; }
            set { empid = value; }
        }

        [DataMember]
        public string Ename
        {
            get { return ename; }
            set { ename = value; }
        }
        [DataMember]
        public string Address
        {
            get { return address; }
            set { address = value; }
        }
        [DataMember]
        public string City
        {
            get { return city; }
            set { city = value; }
        }
        [DataMember]
        public string State
        {
            get { return state; }
            set { state = value; }
        }
        [DataMember]
        public string Employeerid
        {
            get { return employeerid; }
            set { employeerid = value; }
        }
    }
}



Service1.cs:


Service1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace Employee
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
    public class Service1 : IService1
    {
        public  DataSet GetEmployeerid()
        {
            String strConnection = ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConnection);
            SqlCommand cmd = new SqlCommand("select * from Employeertable where Employeerid=@Employeerid ", conn);
             conn.Open();
            SqlDataAdapter da=new SqlDataAdapter(cmd);            
             DataSet ds = new DataSet ();
             da.Fill(ds);
             return ds;
    
        }

        public List<compositetype> GetDataUsingDataContract(CompositeType composite)
        {
            List<compositetype> CompositeType = new List<compositetype>();

            {
                String strConnection = ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
                SqlConnection conn = new SqlConnection(strConnection);
                SqlCommand cmd = new SqlCommand("select * from Employeetable ", conn);
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        CompositeType obj = new CompositeType();
                        obj.Empid = Convert.ToInt32(dt.Rows[i]["Empid"].ToString());
                        obj.Ename = dt.Rows[i]["Ename"].ToString();
                        obj.Address = dt.Rows[i]["Address"].ToString();
                        obj.City = dt.Rows[i]["City"].ToString();
                        obj.State = dt.Rows[i]["State"].ToString();
                        obj.Employeerid = dt.Rows[i]["Employeerid"].ToString();
                        CompositeType.Add(obj);
                    }
                }
                conn.Close();
            }
            return CompositeType;
        }

        }
    }



设计视图:


Design view:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
&nbsp;<asp:Label ID="Label1" runat="server" Text="Employeerid">
    </asp:Label>&nbsp;&nbsp;
        <asp:DropDownList ID="DropDownList1" runat="server" Height="20px" Width="192px">
         <asp:ListItem>...Select...</asp:ListItem>
            <asp:ListItem>TCD</asp:ListItem>
            <asp:ListItem>Hmsa</asp:ListItem>
             <asp:ListItem>Dell</asp:ListItem>
        </asp:DropDownList>
&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Search"  />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>

                <asp:BoundField DataField="Empid" HeaderText="Empid" />
                <asp:BoundField DataField="Ename" HeaderText="Ename" />
                <asp:BoundField DataField="Address" HeaderText="Address" />
                <asp:BoundField DataField="City" HeaderText="City" />
                <asp:BoundField DataField="State" HeaderText="State" />
                <asp:BoundField DataField="Employeerid" HeaderText="Employeerid" />
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>
</html>

推荐答案

如果您使用的是Visual Studio并且只是想要要测试此服务,只需添加Web引用并将项目添加为服务。

在更实时的场景中,理想情况下应该在IIS中托管Web服务,然后使用它进行测试你的客户。



查看WCF客户端进行测试。



查看以下链接 -

http: //a1ashiish-csharp.blogspot.com/2012/01/cnet-how-to-test-wcf-web-service-in.html [ ^ ]

http://blogs.msdn.com/b/jmeier/archi ve / 2007/10/15 / how-to-create-a-hello-world-wcf-service-using-visual-studio.aspx [ ^ ]

http://visualstudiomagazine.com/articles/2012/09/11/host-wcf-service-library-in-asp-net.aspx [ ^ ]

http://blogs.msdn。 com / b / wcftoolsteamblog / archive / 2010/01/04 / tips-for-launching-wcf-test-client.aspx [ ^ ]
If you are using Visual Studio and just want to test this service, simply go to add web reference and add the project as service.
In a more real time scenario, you should ideally look at hosting the web service in IIS and then testing against it using your client.

Look at the WCF Client for testing too.

Check out the following links -
http://a1ashiish-csharp.blogspot.com/2012/01/cnet-how-to-test-wcf-web-service-in.html[^]
http://blogs.msdn.com/b/jmeier/archive/2007/10/15/how-to-create-a-hello-world-wcf-service-using-visual-studio.aspx[^]
http://visualstudiomagazine.com/articles/2012/09/11/host-wcf-service-library-in-asp-net.aspx[^]
http://blogs.msdn.com/b/wcftoolsteamblog/archive/2010/01/04/tips-for-launching-wcf-test-client.aspx[^]


这篇关于如何在客户端应用程序中添加服务。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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