【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
- 【C#/.Net】.Net引用web,并提取SQL数据
- .NET中获取HTML页面并提取其中的超链接
- .NET中获取HTML页面并提取其中的超链接
- 引用数据库SQL Server 2008中数据时,如何实现C#.NET中的复选框?
- ADO.net将数据导出到Excel并提供下载
- 【C#.NET】dataGridView查询SQL数据方法
- sql server split 字符并提取
- 使用Selenium渲染页面并提取数据
- 【转】隐函数绘制并提取数据
- ASP.NET取网页数据《数据采集》
- C++/SQL SRVER/.NET
- C#.net访问web URL并处理返回值 && 不加web引用调用webservice
- (C#.net) web页面xml套用xls式样表示数据
- C#.net数据操作
- asp.net(c#) sql插入uniqueidentifier、dateTime类型数据
- dbf数据导入sql server数据库(asp.net C#)
- ASP.Net(C#)完整获取SQL Server的DateTime数据
- 【C#.NET】dataGridView1的数据导入到SQL数据库中
- JavaScript知识笔记(三)——内置对象、浏览器对象
- jquery1.7.1在ie8下调用css('padding')报参数无效
- DataEditors
- K - Jury Compromise POJ 1015 (动态规划 --难)
- Cocos2d-x3.2与OpenGL渲染总结Cocos2d-x3.2的渲染流程
- 【C#/.Net】.Net引用web,并提取SQL数据
- jQuery实现商品飞入购物车效果插件
- CRC16校验
- 黑马程序员+Java一对一映射问题的探索
- __block修饰符的作用域问题
- 用GDB调试程序(五)
- IOS高级动画详解
- IntelliJ IDEA 常用快捷键
- 算法导论2学习整理