---
title: "C# 创建WebService并部署在IIS下（入门级例子）"
author: "resembling"
date: "2010-09-03T16:12:12Z"
tags: []
categories: ["编程语言"]
slug: "c-webservice-iis"
draft: false
lastmod: "2010-09-03T16:19:05Z"
id: 754645
url: "https://resembling.iteye.com/blog/754645"
---

工具vs2010,系统windows7

file--new- -web site   将framework选择成3.5.再建立工程：ASP. NET Web Service

 

建立工程后在App\_code下的Service.cs中增加WebMethod

\[WebMethod]  
    public XmlDocument hioye(string arg1)  
    {  
        XmlDocument xmldoc;  
        XmlNode xmlnode;  
        XmlElement xmlelem;  
        XmlElement xmlelem2;  
        XmlText xmltext;  
        int flag = 2;  
        if (arg1 != null && arg1!="")  
        {  
            flag = Int32.Parse(arg1);  
        }

        //Create the connection string.            
        string nwconnect = "server=localhost;database=hr;uid=sa;pwd=hioye";  
        //Create a connection object.  
        SqlConnection conn = new SqlConnection(nwconnect);  
        //Create a command string.  
        String sCommand = "select * from sa\_dept";  
        SqlCommand sqlcmd = new SqlCommand(sCommand, conn);            //设置参数   
        conn.Open();   
        SqlDataReader sdr = sqlcmd.ExecuteReader(); //执行SQL语句   
        int cols = sdr.FieldCount;   //获取结果行中的列数   
        object\[] values = new object\[cols];  
        xmldoc = new XmlDocument();  
        //加入XML的声明段落  
        xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");  
        xmldoc.AppendChild(xmlnode);  
        //加入一个根元素  
        xmlelem = xmldoc.CreateElement("", "ROOT", "");  
        xmltext = xmldoc.CreateTextNode("Root Text");

        xmlelem.SetAttribute("t\_name", "sa\_dept");//添加属性  
        xmlelem.SetAttribute("cols", cols+"");  
        //xmlelem.AppendChild(xmltext);  
        xmldoc.AppendChild(xmlelem);

        /\*/加入另外一个元素  
        xmlelem2 = xmldoc.CreateElement ("recodes" ) ;  
        xmlelem2 = xmldoc.CreateElement("", "recodes", "");  
        xmltext = xmldoc.CreateTextNode ( "The text of the recodes element" ) ;  
        xmlelem2.AppendChild ( xmltext ) ;  
        //xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2);  
        xmlelem.AppendChild(xmlelem2);  
        \*/  
        while (sdr.Read())   
        {   
            sdr.GetValues(values);       //values保存一行数据   
            string linestr = "unknow";  
            if (!sdr.IsDBNull(flag))  
            {  
                linestr = sdr.GetString(flag);  
            }  
            XmlElement xmlelemitem = null;  
            xmlelemitem = xmldoc.CreateElement("recodes");  
            xmlelemitem = xmldoc.CreateElement("", "recodes", "");  
            XmlText xmltextitem = null;  
            xmltextitem = xmldoc.CreateTextNode(linestr);  
            xmlelemitem.AppendChild(xmltextitem);  
            //xmldoc.ChildNodes.Item(1).AppendChild(xmlelemitem);  
            xmlelem.AppendChild(xmlelemitem);  
        }   
        sdr.Close();   
        conn.Close(); 

        return xmldoc;  
    }

 

完成后可先f5运行测试一下，如果没有问题则进行IIS部署

如果你是先安装的vs后再安装的IIS的话先注册一下IIS:

命令行转到目录：C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727 下执行命令：aspnet\_regiis -i

进入IIS管理器选择建立或选择一个网站，再右键添加虚拟目录，将物理路径指向你的WebService工程，完成后选择虚拟目录右键执行“转为应用程序”操作，浏览:http://localhost/虚拟目录的别名/Service.asmx，页面显示调用的方法