加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService 实例

发布时间:2020-12-17 01:03:15 所属栏目:安全 来源:网络整理
导读:首先新建 “ASP.NET Web 服务应用程序”,之后写个返回string 类型的方法 ,具体实现如下: [WebMethod] public string selectByID(int id) { string name = ""; SqlConnection conn = new SqlConnection ("server=.;database=test;uid=sa;pwd=sa"); DataSet

首先新建 “ASP.NET Web 服务应用程序”,之后写个返回string 类型的方法 ,具体实现如下:

        
       [WebMethod]
        public string selectByID(int id)
        {

            string name = "";
            SqlConnection conn = new SqlConnection   ("server=.;database=test;uid=sa;pwd=sa");
            DataSet ds = new DataSet();
            string sql = "select * from aa where id=" + id + "";
            SqlDataAdapter da = new SqlDataAdapter(sql,conn);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                name = ds.Tables[0].Rows[0]["Name"].ToString();
            }
            return name;

        }


上面就已经写好了一个webservice 方法,之后在你的项目里添加“web”引用,添加引用成功之后,就可以调用ws里面的方法了,实现如下:

        protected void Button1_Click(object sender,EventArgs e)
        {
            //new 一个webservice 实例
               Service.Service1 service = new Service1();
            // selectByID 为webservice 方法
               string value = service.selectByID(3);
            if (value != "")
            {
                this.Label1.Text = value.ToString();
            }
            else
            {
                this.Label1.Text = "Null";
            }
        }

?

实现效果和数据库截图:

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读