【C#/.Net】.Net引用web,并提取SQL数据
2015-04-28 16:58
281 查看
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后出现数据
相关文章推荐
- C#主要用于查询sql的web项目:查询以及页面显示数据非常缓慢的改进方案
- C#主要用于查询sql的web项目:gridview在页面上显示数据量大时跳转十分缓慢的解决方案
- C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte
- 在.NET 3.5 平台上使用LINQ to SQL创建三层/多层Web应用系统 (Part 2) (引用)
- SqlDbType 与 .Net 数据类型对照表 和 SqlDbType转换为C#数据类型
- 生成(c#检查输入数据、web界面控件)代码的存储过程
- .net使用mvc模式开发web应用 模型与视图间的数据处理
- 一个用于.NET下动态添加Web引用的简单class
- C#实现SQL全库检索数据-比较使用DataReader与DataAdapter+Datatable效率,差距惊人!
- 【开源.NET】轻量级内容管理框架Grissom.CMS(第三篇解析配置文件和数据以转换成 sql)
- C# HttpWebRequest\HttpWebResponse\WebClient发送请求解析json数据
- 欢迎C#/ASP.NET/MS SQL Server开发Web程序的朋友加入Edrp开发组
- SQLServer\framework启动报异常:Module的类型初始值设定项引发异常 [转]QQ空间、新浪微博、腾讯微博等一键分享API链接代码 window下 mongodb快速安装 数据表查询所有子/父节点 C#学习笔记1
- [转]C# 向web网站GET、POST 数据
- 抓取Web网页数据分析(c#)
- C# web 读取excel并导入数据库
- 【.Net】C# 将Access中时间段条件查询的数据添加到ListView中
- .NET-创建虚拟表,将 SQL 表中的数据导入虚拟表
- 浅析Web数据存储-Cookie、UserData、SessionStorage、WebSqlDatabase
- 非.NET语言调用.NET XML Web Services返回的数据集合的两个方法