NET-WebService的创建部署运用

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

.NETWebService的创建、部署、运用项目要求:(简单点讲)1、客户端通过web站点上传XML,XML文件在web站点首先解析成DataSet,所以上传给WebService只是DataSet,你也可以上传XML文件到WebService服务器,这里以前者为例。2、WebService接收客户端传来的DataSet,将DataSet插入到数据库。3、客户端查询,通过web引用,查询结果返回客户端。-------最先要解决的问题是你要阅读和了解WebService相关的知识------第一步:创建你的webservice,打开VS2005---新建网站----Asp.NetWeb服务---确定第二步:网站自动给你生成了一个asmx文件----现在你就可以运行这个简单的webservice,之后你就可以看到那个HelloWorld。第一步:创建你的WebSite,就是asp.net站点//当然开发前,你可以先不新建asp.net站点,我就直接在webservice本地操作,我觉得这样比较好。1:【上传功能】第一步:在你的web站点拖入一个上传控件FileUpload,这个控件的作用就是选择本地的xml文件,在本地转化为dataset,你要传给webservice的是dataset。具体代码如下://=================客户端上传Xml文件====================protectedvoidbtnUpload_Click(objectsender,EventArgse){//fullfileName获得全路径stringfullfileName=this.fileUpload.PostedFile.FileName;//获得文件名fileName=fullfileName.Substring(fullfileName.LastIndexOf(\\)+1);//文件类型stringfileType=fullfileName.Substring(fullfileName.LastIndexOf(.)+1);if(this.fileUpload.PostedFile.FileName!=null){if(fileType.ToLower()==xml){DataSetds=newDataSet();ds.ReadXml(fullfileName);//调用webService中的方法.myService.getXml(ds);//成功后提示,代码省略}else{//失败后提示,代码省略}}else{this.lblMessage.Text=请选择你要上传的文件,谢谢!;}}你在站点上上传了一个dataset,所以webservice就要有一个方法来接收它,代码如下://================上传XML文件===================[WebMethod]publicboolgetXml(DataSetdataSet)//publicDataSetgetXml(DataSetdataSet){//连接数据库,这里面随你怎么连接数据库,只要能连上就可以了。DBdb=newDB();SqlConnectioncon=db.sqlCon();con.Open();DataSetc_dataSet=newDataSet();//将用户传过来的dataSet赋值给c_dataSet.c_dataSet=dataSet;//这里调用了存储过程.//读者只要将sp_MVP改为select*fromMVP,其它的照例,不用存储管理的话把蓝色那行都删掉.SqlDataAdaptersda=newSqlDataAdapter(sp_MVP,con);sda.SelectCommand.CommandType=CommandType.StoredProcedure;SqlCommandBuildermysqlcommand=newSqlCommandBuilder(sda);sda.Update(c_dataSet,MVP);SqlDataAdaptersda1=newSqlDataAdapter(sp_OtherCompetencies,con);sda1.SelectCommand.CommandType=CommandType.StoredProcedure;SqlCommandBuildermysqlcommand1=newSqlCommandBuilder(sda1);sda1.Update(c_dataSet,OtherCompetencies);SqlDataAdaptersda2=newSqlDataAdapter(sp_OtherCompetency,con);sda2.SelectCommand.CommandType=CommandType.StoredProcedure;SqlCommandBuildermysqlcommand2=newSqlCommandBuilder(sda2);sda2.Update(c_dataSet,OtherCompetency);SqlDataAdaptersda3=newSqlDataAdapter(sp_Publications,con);sda3.SelectCommand.CommandType=CommandType.StoredProcedure;SqlCommandBuildermysqlcommand3=newSqlCommandBuilder(sda3);sda3.Update(c_dataSet,Publications);SqlDataAdaptersda4=newSqlDataAdapter(sp_Publication,con);sda4.SelectCommand.CommandType=CommandType.StoredProcedure;SqlCommandBuildermysqlcommand4=newSqlCommandBuilder(sda4);sda4.Update(c_dataSet,Publication);//处理所以的请求(更新).c_dataSet.AcceptChanges();con.Close();returntrue;}首先要在webservice服务器建立数据库,有五张表MVP,OtherCompetencies......这里webservice要连接数据库了,这里是连接sql2005,o(∩_∩)o...哈哈,我相信这里连接数据库会让你很郁闷的,实在没辙了来找我,^_^现在就可以试试效果了!当然不可以一下就能成功,多调试!2:[查询功能]查询是在webservice里面实现的,代码如下://=================用户查询=====================[WebMethod]//publicDataSetsearchData(stringmvpguid,stringfristname,stringlastname)publicDataSetsearchData(stringmvpguid){//连接数据库DBdataBase=newDB();SqlConnectioncon=dataBase.sqlCon();con.Open();//用户查询得到的将是myDataSet.DataSetmyDataSet=newDataSet();try{SqlDataAdaptermyAdapter=newSqlDataAdapter();if(mvpguid!=){//按照用户输入的mvpguid进行查询.myAdapter.SelectCommand=newSqlCommand(select*fromMVPwhereMVPGUID='+mvpguid+',con);SqlCommandBuildermyCB=newSqlCommandBuilder(myAdapter);myAdapter.Fill(myDataSet,MVP);//建立表间关系.SqlDataAdaptermyAdapter1=newSqlDataAdapter(selectOtherCompetencies.OtherCompetencies_Id,OtherCompetencies.MVP_IdfromMVP,OtherCompetencieswhereMVP.MVP_Id=OtherCompetencies.MVP_IdandMVP.MVPGUID='+mvpguid+',con);SqlCommandBuildermyCB1=newSqlCommandBuilder(myAdapter1);myAdapter1.Fill(myDataSet,OtherCompetencies);SqlDataAdaptermyAdapter2=newSqlDataAdapter(selectOtherCompetency.OtherCompetencies_Id,OtherCompetency.OtherCompetency_TextfromMVP,OtherCompetencies,OtherCompetencywhereMVP.MVP_Id=OtherCompetencies.MVP_IdandOtherCompetencies.OtherCompetencies_Id=OtherCompetency.OtherCompetencies_IdandMVP.MVPGUID='+mvpguid+',con);SqlCommandBuildermyCB2=newSqlCommandBuilder(myAdapter2);myAdapter2.Fill(myDataSet,OtherCompetency);SqlDataAdaptermyAdapter3=newSqlDataAdapter(selectPublications.Publications_Id,Publications.MVP_IdfromPublications,MVPwhereMVP.MVP_Id=Publications.MVP_IdandMVP.MVPGUID='+mvpguid+',con);SqlCommandBuildermyCB3=newSqlCommandBuilder(myAdapter3);myAdapter3.Fill(myDataSet,Publications);SqlDataAdaptermyAdapter4=newSqlDataAdapter(selectPublication.Publications_Id,Publication.Id,Publication.Title,Publication.Publisher,Publication.DatePublished,Publication.Abstract,Publication.UrlLinkfromMVP,Publications,PublicationwhereMVP.MVP_Id=Publications.MVP_IdandPublications.Publications_Id=Publication.Publications_IdandMVP.MVPGUID='+mvpguid+',con);SqlCommandBuildermyCB4=newSqlCommandBuilder(myAdapter4);myAdapter4.Fill(myDataSet,Publication);//CreateDataRelation,andaddittotheDataSet.DataRelationdr=newDataRelation(MVPToOtherCompetencies,myDataSet.Tables[MVP].Columns[MVP_Id],myDataSet.Tables[OtherCompetencies].Columns[MVP_Id]

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功