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

来源:互联网 发布:jquery post json解析 编辑:程序博客网 时间:2024/06/05 08:03

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