你的位置:首页 > ASP.net教程

[ASP.net教程]WebService的创建和使用——文件名称生成器


简介

      之前做过一个文件名称生成器,通过Webservice读取

涉及到的编程内容

    webservice,

Webservice的创建

      (1)创建空Web应用程序     (2)添加Web服务(asmx)

    (3)在[WebMethod]下写要使用的方法

   (4)通过浏览器查看是否创建好服务

   (5)发现已经建立了该服务

Webservice的使用

  (1)创建一个新窗体,添加服务引用

 

   (2)点击“发现”找到相应的websevice,再点击“高级”

    (3)点击“添加Web引用”

    (4)点击“此解决方案中的Web服务”

    (5)点击“添加引用”

   (6)在窗体中引用即可

数据库表的字段:

Winform程序界面:

            

Webservice调用类的方法:

      (1)在webservice中调用类的方法来完成某些操作,而不是直接写在webservice里。

      (2)从

 public List<string> BackSchemaList()    {      List<string> name = new List<string>();      = new @"F:\Winform Project\Namer\FieldList.");      = doc.SelectSingleNode("FieldList");      = xn.ChildNodes;      foreach (in xnl)     {          string fieldvalue = "";                = (string listnumber = xe.GetAttribute("number").ToString();          string listtype = xe.GetAttribute("type").ToString();          = xe.ChildNodes;        foreach (in xnl0)        {          = (+= xe1.GetAttribute("Value").ToString();                  }        name.Add(fieldvalue);        name.Add(listtype);     }         return name;    }

View Code

      (3)选取一个模板进行模糊查询,给新生成的文件名称加上个序列号:

 public string generatedata(string s1,string s2)    {      string str_provider = "Provider=Microsoft.Ace.OLEDB.12.0;";      string str_source = "Data Source=F:/Winform Project/Namer/Namer.accdb;Persist Security Info=False;"; //数据源路径      string str_connection = str_provider + str_source; //连接字符串      int xuliehao = 1;      OleDbConnection cnn;      OleDbCommand cmd;      OleDbDataReader datar;      string str_sql1 = "SELECT name FROM T_name WHERE name Like '"+s1+"%';";      cnn = new OleDbConnection(str_connection);      cmd = new OleDbCommand(str_sql1, cnn);      cnn.Open();      datar = cmd.ExecuteReader();      while (datar.Read())      {        if (datar["name"].ToString() == null)        {          break;        }        else { xuliehao++; }      }      cnn.Close();      string number = string.Format("{0:000}", xuliehao);      s1 += number;      string dt = DateTime.Now.ToString();      string str_sql2 = "insert into T_name (name,code,timeNow,type) values ('"+s1+"','"+number+"','"+dt+"','"+s2+"');";      cnn = new OleDbConnection(str_connection);      cmd = new OleDbCommand(str_sql2, cnn);      cnn.Open();      int i = cmd.ExecuteNonQuery();      cnn.Close();      string newname = s1;      return newname;       }

View Code

      (4)读取一条信息并显示其详细字段:

 public List<List<string>> Backonelist(int int_index,string str_type)    {      List<List<string>> documentlist = new List<List<string>>();      = new @"F:\Winform Project\Namer\FieldList.");      = doc.SelectSingleNode("FieldList");      = xn.ChildNodes;      foreach (in xnl)      {        = (= xe.ChildNodes;        if (xe.GetAttribute("number").ToString() == (int_index + 1).ToString() && xe.GetAttribute("type").ToString() == str_type)        {          foreach (in xnl0)          {            List<string> field = new List<string>();            = ("Index").ToString());            field.Add(xe1.GetAttribute("Type").ToString());            field.Add(xe1.GetAttribute("Name").ToString());            field.Add(xe1.GetAttribute("Value").ToString());            documentlist.Add(field);          }             }      }                return documentlist;    }

View Code

      (5)对

 public bool insert(string s)    {      bool state = true;      int i = 1;      List<List<string>> one_document = new List<List<string>>();      one_document = JsonConvert.DeserializeObject<List<List<string>>>(s);          = new @"F:\Winform Project\Namer\FieldList.");      = doc.SelectSingleNode("FieldList");       = doc.CreateElement("list");      foreach (in xn.ChildNodes)      {        = (if (xe.GetAttribute("type").ToString() == one_document[0][0])        {          i++;        }      }      element.SetAttribute("number", i.ToString());      element.SetAttribute("type", one_document[0][0]);            foreach (List<string> list in one_document)      {          string fieldindex = list[1];          string fieldtype = list[2];          string fieldname = list[3];          string fieldvalue = list[4];          = doc.CreateElement("Field");          element.AppendChild(element2);          element2.SetAttribute("Index", fieldindex);          element2.SetAttribute("Type", fieldtype);          element2.SetAttribute("Name", fieldname);          element2.SetAttribute("Value", fieldvalue);                }      xn.AppendChild(element);       doc.Save(@"F:\Winform Project\Namer\FieldList.");            return state;    }

View Code

      (6)对

 public bool EditDATA(string str_s)    {      bool flag = true;      int temp = 1;      List<List<string>> one_document = new List<List<string>>();      one_document = JsonConvert.DeserializeObject<List<List<string>>>(str_s);      List<string> basicinfo = new List<string>();      basicinfo=one_document[0];      string typename = basicinfo[0];      string preview = basicinfo[1];      int temp_documentindex = Convert.ToInt32(basicinfo[2]);      int int_documentindex = temp_documentindex + temp;      string documentindex = int_documentindex.ToString();      one_document.Remove(one_document[0]);      = new @"F:\Winform Project\Namer\FieldList.");      = doc.SelectSingleNode("//list[@ number='" + documentindex + "'][@ type='" + typename + "']");      = xn.ChildNodes;      List<new List<();      foreach (in xnl)      {        foreach (in foreach (List<string> list in one_document)      {        string fieldindex = list[0];        string fieldtype = list[1];        string fieldname = list[2];        string fieldvalue = list[3];        = doc.CreateElement("Field");        xn.AppendChild(element2);        element2.SetAttribute("Index", fieldindex);        element2.SetAttribute("Type", fieldtype);        element2.SetAttribute("Name", fieldname);        element2.SetAttribute("Value", fieldvalue);        if (element2.GetAttribute("Name").ToString() == "category")        {          = doc.CreateElement("CodeList");          element2.AppendChild(element3);          for (int i = 1; i <= 4; i++)          {            string[] array = { "personal", "work", "play", "trans" };            = doc.CreateElement("CodeWord");            element3.AppendChild(element4);            element4.SetAttribute("Code", i.ToString());            element4.SetAttribute("Description", array[i - 1]);          }        }      }      doc.Save(@"F:\Winform Project\Namer\FieldList.");       return flag;    }

View Code

最后效果:

本文中对部分相对比较重要的代码进行了交代,希望对大家有点帮助。