工具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");