当前位置:首页 > NET WebService的创建部署运用
.NET WebService的创建、部署、运用
项目要求:(简单点讲)
1、客户端通过web站点上传XML,XML文件在web站点首先解析成DataSet,所以上传给WebService只是DataSet,你也可以上传XML文件到WebService服务器,这里以前者为例。
2、WebService接收客户端传来的DataSet,将DataSet插入到数据库。 3、客户端查询,通过web引用,查询结果返回客户端。
-------最先要解决的问题是你要阅读和了解WebService相关的知识------ 第一步:创建你的webservice,打开VS2005---新建网站----Asp.Net Web服务---确定 第二步:网站自动给你生成了一个asmx文件----现在你就可以运行这个简单的webservice,之后你就可以看到那个\。
第一步:创建你的WebSite,就是asp.net站点
//当然开发前,你可以先不新建asp.net站点,我就直接在webservice本地操作,我觉得这样比较好。
1:【上传功能】
第一步:在你的web站点拖入一个上传控件FileUpload,这个控件的作用就是选择本地的xml文件,在本地转化为dataset,你要传给webservice的是dataset。具体代码如下: //=================客户端上传Xml文件==================== protected void btnUpload_Click(object sender, EventArgs e) {
//fullfileName获得全路径
string fullfileName = this.fileUpload.PostedFile.FileName; //获得文件名
fileName = fullfileName.Substring(fullfileName.LastIndexOf(\ //文件类型
string fileType = fullfileName.Substring(fullfileName.LastIndexOf(\
if (this.fileUpload.PostedFile.FileName != null) {
if (fileType.ToLower() == \ {
DataSet ds = new DataSet(); ds.ReadXml(fullfileName); //调用webService中的方法. myService.getXml(ds); //成功后提示,代码省略 } else {
//失败后提示,代码省略 } } else {
this.lblMessage.Text = \请选择你要上传的文件,谢谢!\ } }
你在站点上上传了一个dataset,所以webservice就要有一个方法来接收它,代码如下: //================上传XML文件=================== [WebMethod]
public bool getXml(DataSet dataSet) //public DataSet getXml(DataSet dataSet) {
//连接数据库,这里面随你怎么连接数据库,只要能连上就可以了。
DB db = new DB();
SqlConnection con = db.sqlCon(); con.Open();
DataSet c_dataSet = new DataSet();
//将用户传过来的dataSet赋值给c_dataSet. c_dataSet = dataSet;
//这里调用了存储过程.
//读者只要将sp_MVP改为select * from MVP,其它的照例,不用存储管理的话把蓝色那行都删掉.
SqlDataAdapter sda = new SqlDataAdapter(\
sda.SelectCommand.CommandType = CommandType.StoredProcedure; SqlCommandBuilder mysqlcommand = new SqlCommandBuilder(sda); sda.Update(c_dataSet, \
SqlDataAdapter sda1 = new SqlDataAdapter(\ sda1.SelectCommand.CommandType = CommandType.StoredProcedure; SqlCommandBuilder mysqlcommand1 = new SqlCommandBuilder(sda1); sda1.Update(c_dataSet, \
SqlDataAdapter sda2 = new SqlDataAdapter(\ sda2.SelectCommand.CommandType = CommandType.StoredProcedure; SqlCommandBuilder mysqlcommand2 = new SqlCommandBuilder(sda2); sda2.Update(c_dataSet, \
SqlDataAdapter sda3 = new SqlDataAdapter(\ sda3.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder mysqlcommand3 = new SqlCommandBuilder(sda3); sda3.Update(c_dataSet, \
SqlDataAdapter sda4 = new SqlDataAdapter(\ sda4.SelectCommand.CommandType = CommandType.StoredProcedure; SqlCommandBuilder mysqlcommand4 = new SqlCommandBuilder(sda4); sda4.Update(c_dataSet, \ //处理所以的请求(更新). c_dataSet.AcceptChanges(); con.Close(); return true; }
首先要在webservice服务器建立数据库,有五张表MVP,OtherCompetencies......这里webservice要连接数据库了,这里是连接sql2005,o(∩_∩)o...哈哈,我相信这里连接数据库会让你很郁闷的,实在没辙了来找我,^_^
现在就可以试试效果了!当然不可以一下就能成功,多调试!
2:[查询功能]
查询是在webservice里面实现的,代码如下:
//=================用户查询===================== [WebMethod]
//public DataSet searchData(string mvpguid,string fristname,string lastname) public DataSet searchData(string mvpguid) {
//连接数据库
DB dataBase = new DB();
SqlConnection con = dataBase.sqlCon(); con.Open();
共分享92篇相关文档