C# WinForm 调用WebService
在Winform中对数据库进行操作缺乏安全性,因而可以使用Winform调用WebService来实现对数据库的各种操作。
在VS2010中,创建一个Web服务程序,第一:创建一个空的Web应用程序,名字自己起。第二:鼠标右击刚刚创建的工程,选择添加,在弹出的框中选择Web服务,自己起好名字,确定即可,这样就创建好一个Web服务程序了。
经过上上面的步骤,我们就可以添加方法来实现数据库的操作。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq; using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
namespace WebServerSQL {
/// <summary>
/// WebServerGetSqlData 的摘要说明
/// </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 WebServerGetSqlData : System.Web.Services.WebService {
//定义数据库连接对象
private SqlConnection con;
[WebMethod]
public DataSet GetInfo(string strSql){
string sqlConnect = "initial catalog=数据库名称;server=服务器名称;uid=sa;pwd=密码";
con = new SqlConnection(sqlConnect);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(strSql, con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
[WebMethod]
public bool testConnect(){
try{
//数据库连接,定义连接对象和连接字符串并打开
string sqlConnect = "initial catalog=数据库名称;server=服务器名称;uid=sa;pwd=密码";
con = new SqlConnection(sqlConnect);
con.Open();
return true;
}
catch
{
return false;
}
}
}
}
下面看看Winfrom下怎么调用WebService:
首先要添加Web服务引用,在VS2010中,直接右击工程,在弹出的菜单中看不到添加Web引用,难道Winform中不能添加Web引用吗?通过验证,是可以添加的。我们只需要选择选择Web服务引用,然后在弹出的对话框中点击高级,然后你就会看到新弹出一个对话框里面就有添加Web引用,然后输入WebService服务的URL地址按照步骤完成即可。如下图:
然后在Winfrom中我们可以调用WebService提供的方法了,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WinformGetWebServerSQL {
public partial class FrmWebserver : Form{
public FrmWebserver(){
InitializeComponent();
} //定义web服务对象
localhost.WebServerGetSqlData webData = new localhost.WebServerGetSqlData();
//测试数据库是否连接成功
private void btnTest_Click(object sender, EventArgs e){
if (webData.testConnect()){
label2.Text = "成功";
}else{
label2.Text = "失败";
}
} //显示数据库中表的数据
private void btnShow_Click(object sender, EventArgs e){
string strsql = "select * from item";
DataSet ds=webData.GetInfo(strsql);
DVShowInfo.DataSource = ds.Tables[];
}
}
}
至此一个简单的调用webservice的程序就完成了,感兴趣的朋友可以在WebService中添加更多的方法,比如对数据库的增,删,改,查等供winform调用。
希望朋友们多多给点意见,大家共同学习进步!
C# WinForm 调用WebService的更多相关文章
- C#winForm调用WebService的远程接口
Web Service 的创建简单编码.发布和部署 上一篇详细概述了WebService的创建,编码,发布和部署,那么作为客户端的程序如何访问远程端的WebService 接下来看一下具体步骤: ...
- C# winForm调用WebService
C#winForm调用WebService的远程接口 创建一个WebService工程用例 添加服务引用 添加webService服务地址 输入命名空间名称 主要代码 测试成功截图 工程代码下载 新建 ...
- winform调用webservice假死怎么解决
主线程调用外部web service,没有返回时,主线程阻塞了,界面肯定假死耗时操作都是要在工作线程里面执行的.一般情况下winform调用webservice时步骤1添加服务引用---高级----添 ...
- c#调用WebService实例
在Winform中对数据库进行操作缺乏安全性,因而可以使用Winform调用WebService来实现对数据库的各种操作.在VS2010中,创建一个Web服务程序,第一:创建一个空的Web应用程序,名 ...
- 一个简单的webservice的demo(下)winform异步调用webservice
绕了一大圈,又开始接触winform的项目来了,虽然很小吧.写一个winform的异步调用webservice的demo,还是简单的. 一个简单的Webservice的demo,简单模拟服务 一个简单 ...
- winform客户端程序第一次调用webservice方法很慢的解决方法
.net2.0的winform客户端最常用的与服务端通信方式是通过webservice,最近在用dottrace对客户端做性能测试的时候发现,客户端程序启动以后,第一次调用某一个webservice的 ...
- winform下调用webservice,传参List<string>
用c#做了一个webservice,其中一个接口是public bool AddReturns(List<string> SQLStringList). 然后在另一个c#做的winform ...
- WinForm客户端调用 WebService时 如何启用Session
WinForm客户端调用 WebService时 如何启用Session 摘自: http://www.cnblogs.com/swtseaman/archive/2011/04/18/2020176 ...
- C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用
1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL.这个比较常见也很简单 即有完整的webservice文件目录如下图 ...
随机推荐
- ASP.NET中上传并读取Excel文件数据
在CSDN中,经常有人问如何打开Excel数据库文件.本文通过一个简单的例子,实现读取Excel数据文件. 首先,创建一个Web应用程序项目,在Web页中添加一个DataGrid控件.一个文件控件和一 ...
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论
D. Vitaly and Cycle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...
- 网站性能扩展案例:每天30-50亿请求,300K QPS是如何炼成的
Reduce Data广告服务网站(http://reducedata.com)如何扩展到每天300K QPS请求?分享经验如下: 1. 为大规模而设计,广告服务平台从一开始增长就很惊人,因此,系统开 ...
- Android开发 设置开机自动启动
原文:http://blog.csdn.net/kevinmeng_ini58/article/details/7700786 片段一: <!-- 开机启动 --> <receive ...
- Android平台上长连接的实现
Android 平台上长连接的实现 为了不让 NAT 表失效,我们需要定时的发心跳,以刷新 NAT 表项,避免被淘汰. Android 上定时运行任务常用的方法有2种,一种方法用 Timer,另一种是 ...
- [AngularJS] Using Services in Angular Directives
Directives have dependencies too, and you can use dependency injection to provide services for your ...
- [CoffeeScript] Level 4 Arrays, Objects, Iterations -- Ex
Coffee on the Range Create an array with numbers 1 until 10 using the inclusive (two dot) range synt ...
- Xcode和IOS模拟器
Xcode和IOS模拟器 目录 概述 Xcode常用操作 学会用Instrument IOS模拟器 概述 Xcode常用操作 整体缩进或者缩退 command+“[” .command+“]” 在同一 ...
- ClamAV
ClamAV 简介以及适用范围 ClamAV是一个在命令行下查毒软件,因为它不将杀毒作为主要功能,默认只能查出您计算机内的病毒,但是无法清除,至多删除文件.ClamAV可以工作很多的平台上,但是有少数 ...
- Linux上安装使用boost入门指导
Data Mining Linux上安装使用boost入门指导 获得boost boost分布 只需要头文件的库 使用boost建立一个简单的程序 准备使用boost二进制文件库 把你的程序链接到bo ...