【C#/.Net】.Net引用web,并提取SQL数据

1.在VS中,新建一个web application,编写要用到的[webmethod],这里我需要用到的是Product:提取数据库数据。webservice.asmx.cs文件如下。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication2
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    //[System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
#region
        [WebMethod(Description = "产品列表")]
        public DataSet GetProducts()
        {
            string con =
  "server = (local);database = sun;uid = sa;pwd = 123456";
            SqlConnection conn = new SqlConnection(con);
            //打开数据库连接
            conn.Open();
            
            SqlDataAdapter myDataAdapter;
            DataSet myDataSet = new DataSet();

            string cmd = "select *from Product";
            myDataAdapter = new SqlDataAdapter(cmd, conn);
            myDataAdapter.Fill(myDataSet,"Product");
            conn.Close();
            return myDataSet;
            //SqlCommand cmd = new SqlCommand("downloadpic", conn);
            //cmd.CommandType = CommandType.StoredProcedure;
            //cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;
            //SqlDataReader reader = cmd.ExecuteReader();
            //if (reader.Read())
        }
#endregion

#region
        [WebMethod]
        public string  sayhello(string name)
        {
            return "Hello,"+name;
        }
#endregion
        [WebMethod]
        public double addtion(double i, double j)
        {
            return i + j;
        }
        [WebMethod]
        public double substract(double i, double j)
        {
            return i - j;
        }

    }
}
数据库文件如下:

==================================================================

点击浏览器中查看,查看编写的method

==================================================================
效果出现如图:

=================================================================
将写好的web引用到网站中,新建.Net 网站,加入web引用:

=================================================================
配置web选项,并加入,URL是上一步浏览器中的地址:


=================================================================
编写后台程序,调用web:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        localhost.WebService1 ms = new localhost.WebService1();
        if (TextBox1.Text.Length > 0)
            Label1.Text = ms.sayhello(TextBox1.Text);
        else Label1.Text = "等待输入";
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        localhost.WebService1 ms = new localhost.WebService1();
        GridView1.DataSource = ms.GetProducts();
        GridView1.DataBind();
    }
}
运行:


点击Product:(未加入Grid控件之前)

加入Gird控件之后:点击button2后出现数据

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值