• 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[0];         
    }     
    } 
    }

      至此一个简单的调用webservice的程序就完成了,感兴趣的朋友可以在WebService中添加更多的方法,比如对数据库的增,删,改,查等供winform调用。

    希望朋友们多多给点意见,大家共同学习进步!

  • 相关阅读:
    。。。Hibernate 查询数据 事务管理。。。
    如何在easyui datagrid 中显示外键的值
    easyui datagrid 中序列化后的日期格式化
    使用Log4net把日志写入到SqlServer数据库
    在ALV点击Key值调用TCode,跳过初始屏幕
    JAVA环境变量设置
    在Jsp中调用静态资源,路径配置问题
    eclipse下项目复制改名注意事项
    HTML5与CSS3基础教程第八版学习笔记16-21章
    HTML5与CSS3基础教程第八版学习笔记11~15章
  • 原文地址:https://www.cnblogs.com/Price/p/3296817.html
Copyright © 2020-2023  润新知