Wcf的使用

l         创建wcf服务库项目,创建接口点与数据类型

namespace WcfServiceLibrary1

{

    // 注意: 如果更改此处的接口名称“IService1,也必须更新 App.config 中对“IService1的引用。

    [ServiceContract]

    public interface IService1

    {

        //[OperationContract]

        //string GetData(int value);

 

        //[OperationContract]

        //CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]

        Boolean TestConnect();

        [OperationContract]

        News GetNewsById(string id);

        [OperationContract]

        News GetNews();

        [OperationContract]

       List<News> GetTopNews(int num);

 

        // 任务: 在此处添加服务操作

    }

 

    // 使用下面示例中说明的数据协定将复合类型添加到服务操作

 

    [DataContract]

   public  class News

    {

        [DataMember]

        public string Id { get; set; }

        [DataMember]

        public string Title { get; set; }

        [DataMember]

        public DateTime Date { get; set; }

    }

}

l         实现接口

namespace WcfServiceLibrary1

{

    // 注意: 如果更改此处的类名“IService1,也必须更新 App.config 中对“IService1的引用。

    public class Service1 : IService1

    {

        string constr = "Server=127.0.0.1,1423; database=DB_news; User ID=sa; password=sa;";   

        #region IService1 成员

        public bool TestConnect()

        {

            SqlConnection con = new SqlConnection(constr);

            con.Open();

            if (con.State == System.Data.ConnectionState.Open)

            {

                con.Close();

                return true;

            }

            else return false;

        }

        #endregion

        #region IService1 成员

        public News GetNewsById(string id)

        {

            SqlConnection con = new SqlConnection(constr);

            SqlCommand cmd = new SqlCommand("select * from N_draft where id =" + id, con);

            con.Open();

            SqlDataReader da = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

            News n = null;

            if (da.Read())

            {

                n= new News();

                n.Date = DateTime.Parse(da["broadcastdate"].ToString());

                n.Id = da["id"].ToString();

                n.Title = da["title"].ToString();

                

            }

            return n;

        }

 

        public News GetNews()

        {

            return GetTopNews(1)[0];

        }

 

      

 

        #endregion

 

        #region IService1 成员

 

 

      public   List<News>  GetTopNews(int num)

        {

            List<News> ns = new List<News>();

            SqlConnection con = new SqlConnection(constr);

            SqlCommand cmd = new SqlCommand("select top " + num + " * from N_draft  ", con);

            con.Open();

            SqlDataReader da = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

            News n = null;

           while (da.Read())

            {

                n = new News();

                n.Date = DateTime.Parse(da["broadcastdate"].ToString());

                n.Id = da["id"].ToString();

                n.Title = da["title"].ToString();

                ns.Add(n);

 

            }

            return ns;

        }

 

        #endregion

    }

}

l         创建应用程序项目,添加服务引用

public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        Service.News[] datas ;

        Service.Service1Client service = new WindowsFormsApplication1.Service.Service1Client();

        private void button1_Click(object sender, EventArgs e)

        {

            datas = null;

            News n=service.GetNewsById(textBox1.Text );

            datas = new News[] { n };

            Bind();

        }

        void Bind()

        {

            dataGridView1.DataSource = datas;

            dataGridView1.Columns[0].HeaderText = "id";

            dataGridView1.Columns[0].DataPropertyName = "id";

            dataGridView1.Columns[1].HeaderText = "title";

            dataGridView1.Columns[1].DataPropertyName = "title";

            dataGridView1.Columns[2].HeaderText = "date";

            dataGridView1.Columns[2].DataPropertyName = "date";

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            datas = null;

            datas = service.GetTopNews(int.Parse(textBox1.Text));

            Bind();

        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值