加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

.NET WebService的创建、部署、运用

发布时间:2020-12-17 01:14:26 所属栏目:安全 来源:网络整理
导读:项目要求:(简单点讲) ??????????????????????? 1、客户端通过web站点上传XML,XML文件在web站点首先解析成DataSet,所以上传给WebService只是DataSet,你也可以上传XML文件到WebService服务器,这里以前者为例。 ??????????????????????? 2、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,之后你就可以看到那个"Hello World"。
????????????????
第一步:创建你的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("") + 1);
???????? //文件类型
???????? string fileType = fullfileName.Substring(fullfileName.LastIndexOf(".") + 1);
???????? if (this.fileUpload.PostedFile.FileName != null)
???????? {
???????????? if (fileType.ToLower() == "xml")
???????????? {
???????????????? 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("sp_MVP",con);
????????sda.SelectCommand.CommandType = CommandType.StoredProcedure;
???????? SqlCommandBuilder mysqlcommand = new SqlCommandBuilder(sda);
???????? sda.Update(c_dataSet,"MVP");

???????? SqlDataAdapter sda1 = new SqlDataAdapter("sp_OtherCompetencies",con);
????????sda1.SelectCommand.CommandType = CommandType.StoredProcedure;
???????? SqlCommandBuilder mysqlcommand1 = new SqlCommandBuilder(sda1);
???????? sda1.Update(c_dataSet,"OtherCompetencies");

???????? SqlDataAdapter sda2 = new SqlDataAdapter("sp_OtherCompetency",con);
????????sda2.SelectCommand.CommandType = CommandType.StoredProcedure;
???????? SqlCommandBuilder mysqlcommand2 = new SqlCommandBuilder(sda2);
???????? sda2.Update(c_dataSet,"OtherCompetency");

???????? SqlDataAdapter sda3 = new SqlDataAdapter("sp_Publications",con);
????????sda3.SelectCommand.CommandType = CommandType.StoredProcedure;
???????? SqlCommandBuilder mysqlcommand3 = new SqlCommandBuilder(sda3);
???????? sda3.Update(c_dataSet,"Publications");

???????? SqlDataAdapter sda4 = new SqlDataAdapter("sp_Publication",con);
????????sda4.SelectCommand.CommandType = CommandType.StoredProcedure;
???????? SqlCommandBuilder mysqlcommand4 = new SqlCommandBuilder(sda4);
???????? sda4.Update(c_dataSet,"Publication");

???????? //处理所以的请求(更新).
???????? 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();

???????? //用户查询得到的将是myDataSet.
???????? DataSet myDataSet = new DataSet();
???????? try
???????? {
???????????? SqlDataAdapter myAdapter = new SqlDataAdapter();

???????????? if (mvpguid != "")
???????????? {
???????????????? //按照用户输入的mvpguid进行查询.
???????????????? myAdapter.SelectCommand = new SqlCommand("select * from MVP where MVPGUID='"+mvpguid+"'",con);
???????????????? SqlCommandBuilder myCB = new SqlCommandBuilder(myAdapter);
???????????????? myAdapter.Fill(myDataSet,"MVP");

???????????????? //建立表间关系.
???????????????? SqlDataAdapter myAdapter1 = new SqlDataAdapter("select OtherCompetencies.OtherCompetencies_Id,OtherCompetencies.MVP_Id from MVP,OtherCompetencies where MVP.MVP_Id=OtherCompetencies.MVP_Id and MVP.MVPGUID='" + mvpguid + "'",con);
???????????????? SqlCommandBuilder myCB1 = new SqlCommandBuilder(myAdapter1);
???????????????? myAdapter1.Fill(myDataSet,"OtherCompetencies");

???????????????? SqlDataAdapter myAdapter2 = new SqlDataAdapter("select OtherCompetency.OtherCompetencies_Id,OtherCompetency.OtherCompetency_Text from MVP,OtherCompetencies,OtherCompetency where MVP.MVP_Id=OtherCompetencies.MVP_Id and OtherCompetencies.OtherCompetencies_Id=OtherCompetency.OtherCompetencies_Id and MVP.MVPGUID='" + mvpguid + "'",con);
???????????????? SqlCommandBuilder myCB2 = new SqlCommandBuilder(myAdapter2);
???????????????? myAdapter2.Fill(myDataSet,"OtherCompetency");

???????????????? SqlDataAdapter myAdapter3 = new SqlDataAdapter("select Publications.Publications_Id,Publications.MVP_Id from Publications,MVP where MVP.MVP_Id=Publications.MVP_Id and MVP.MVPGUID='" + mvpguid + "'",con);
???????????????? SqlCommandBuilder myCB3 = new SqlCommandBuilder(myAdapter3);
???????????????? myAdapter3.Fill(myDataSet,"Publications");

???????????????? SqlDataAdapter myAdapter4 = new SqlDataAdapter("select Publication.Publications_Id,Publication.Id,Publication.Title,Publication.Publisher,Publication.DatePublished,Publication.Abstract,Publication.UrlLink from MVP,Publications,Publication where MVP.MVP_Id=Publications.MVP_Id and Publications.Publications_Id=Publication.Publications_Id and MVP.MVPGUID='" + mvpguid + "'",con);
???????????????? SqlCommandBuilder myCB4 = new SqlCommandBuilder(myAdapter4);
???????????????? myAdapter4.Fill(myDataSet,"Publication");

???????????????? //Create DataRelation,and add it to the DataSet.
???????????????? DataRelation dr = new DataRelation("MVPToOtherCompetencies",myDataSet.Tables["MVP"].Columns["MVP_Id"],

myDataSet.Tables["OtherCompetencies"].Columns["MVP_Id"]);
???????????????? DataRelation dr1 = new DataRelation("OtherCompetenciesToOtherCompetency",myDataSet.Tables["OtherCompetencies"].Columns

["OtherCompetencies_Id"],myDataSet.Tables["OtherCompetency"].Columns["OtherCompetencies_Id"]);
???????????????? DataRelation dr2 = new DataRelation("MVPToPublications",myDataSet.Tables

["Publications"].Columns["MVP_Id"]);
???????????????? DataRelation dr3 = new DataRelation("PublicationsToPublication",myDataSet.Tables["Publications"].Columns["Publications_Id"],

myDataSet.Tables["Publication"].Columns["Publications_Id"]);
????????????????
???????????????? //获取或设置一个指示DataRelation对象是否嵌套的值.
???????????????? dr.Nested = true;
???????????????? dr1.Nested = true;
???????????????? dr2.Nested = true;
???????????????? dr3.Nested = true;
????????????????
???????????????? //创建关系到集合中
???????????????? myDataSet.Relations.Add(dr);
???????????????? myDataSet.Relations.Add(dr1);
???????????????? myDataSet.Relations.Add(dr2);
???????????????? myDataSet.Relations.Add(dr3);
???????????? }
???????? }
???????? catch (SqlException exception)
???????? {
???????????? return DataError(exception);
???????? }
???????? return myDataSet;
???? }
???? //====================错误信息=======================
???? public DataSet DataError(Exception ex)
???? {
???????? DataSet errDS = new DataSet("Errors");
???????? DataTable errTable = errDS.Tables.Add("Error");
???????? errTable.Columns.Add("Message");
???????? errTable.Rows.Add(new Object[] { ex.Message });
???????? return errDS;
???? }
代码有点多,呵呵,然后你在客户端只要引用它的接口就可以了,这样会更好理解一点,代码如下:
//====================客户端查询函数=====================
???? protected void btnSearch_Click1(object sender,EventArgs e)
???? {
???????? string c_mvpGuid = "";
????????
try
???????? {
???????????? c_mvpGuid = this.txtByMVPGUID.Text.ToString();

???????????? //getDataSet对象获得查询返回的ds.
???????????? DataSet getDataSet = myService.searchData(c_mvpGuid);
???????
???????????? ///查询默认显示为MVP表.测试的时候在index.aspx页面上显示.
???????????? this.dgResult.DataSource = getDataSet.Tables["MVP"].DefaultView;
???????????? this.dgResult.DataBind();
?????????
}
???????? catch (Exception ex)
???????? {
???????????? throw ex;
???????? }
???? }
要是有什么不懂的地方,你可以留言,我会给你答复。^_^
呵呵,忘了一件事,如果你用自己的xml文件,自己的数据库,那就没事了,不是的话看看:
<?xml version="1.0" encoding="utf-8"?>
<MVPData>
?? <MVP>
???? <MVPGUID>f8433e20-c9af-4b48-9c83-2fc235393f31</MVPGUID>
???? <FirstName>Rafael Andrada</FirstName>
???? <LastName>[McPegasus]</LastName>
???? <Photo>
???? </Photo>
???? <Bio>
???? </Bio>
???? <StartAnivDate>10/1/2002 12:00:00 AM</StartAnivDate>
???? <Subsidiary>Spain</Subsidiary>
???? <Region>EMEA</Region>
???? <Certifications>
???? </Certifications>
???? <Language>Spanish</Language>
???? <Product>Microsoft Office Access</Product>
???? <KbTitle>Community Solutions KB Articles written by Rafael Andrada [McPegasus]</KbTitle>
???? <KbUrl>http://support.microsoft.com/search/default.aspx?Queryc=kbpubmvpKbUrl>
???? <OtherCompetencies>
??????? <OtherCompetency>Microsoft Office FrontPage</OtherCompetency>
???? </OtherCompetencies>
???? <SpeakingEngagements>
???? </SpeakingEngagements>
???? <Publications>
???????? <Publication>
???????? <Id>db845d0e-1e66-474e-a6c8-5f8a8225c703</Id>
???????? <Title>http://ava4os.webcindario.com</Title>
???????? <Publisher />
???????? <DatePublished />
???????? <Abstract />
???????? <UrlLink>http://ava4os.webcindario.com</UrlLink>
?????? </Publication>
???? </Publications>
???? <Conferences>
???? </Conferences>
?? </MVP>
<MVP>
..........
</MVP>
</MVPData>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读