新建一个web服务:
 
写一个返回XML的dataset转化成XML的方法:
 
[WebMethod(Description = "查询以获取需要的终端信息")]
        public XmlDataDocument GetSiteAData()//参数在这里没用到 
        {
            using( var db = new DatabaseFirst.TestEntities())
            {
                //var list = db.Users.Select(o => o) ;

                //List<User> listMain = list.ToList();
                //DataSet ds = ConvertToDataSet(listMain);


        string strConn = @"uid=sa;pwd=123;database=Test;server=ASIAIT-SERVER\SERVER2008R2"; SqlConnection ConnSql
= new SqlConnection(strConn); //Sql链接类的实例化 ConnSql.Open ();//打开数据库 string strSQL="select * from [user]"; //要执行的SQL语句 SqlDataAdapter da=new SqlDataAdapter(strSQL,ConnSql); //创建DataAdapter数据适配器实例 DataSet ds2=new DataSet();//创建DataSet实例 da.Fill(ds2, "TableName");//使用DataAdapter的Fill方法(填充),调用SELECT命令 ConnSql.Close ();//关闭数据库 XmlDataDocument xd = new XmlDataDocument(ds2); //读取数据集中数据 xd.Save(Console.Out);//输出数据 if (ds2.Tables.Count > 0) { return xd; } else { string xml = "<NewDataSet></NewDataSet>"; xd.LoadXml(xml); return xd; } } }

 另外积累一个List转换DataSet的方法(例子中没有用到)

/// <summary>
        /// list转DataSet
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataSet ConvertToDataSet<T>(List<T> list)
        {
            if (list == null || list.Count <= 0)
            {
                return null;
            }

            DataSet ds = new DataSet();
            DataTable dt = new DataTable(typeof(T).Name);
            DataColumn column;
            DataRow row;

            System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            foreach (T t in list)
            {
                if (t == null)
                {
                    continue;
                }

                row = dt.NewRow();

                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    System.Reflection.PropertyInfo pi = myPropertyInfo[i];

                    string name = pi.Name;

                    if (dt.Columns[name] == null)
                    {
                        column = new DataColumn(name, pi.PropertyType);
                        dt.Columns.Add(column);
                    }

                    row[name] = pi.GetValue(t, null);
                }

                dt.Rows.Add(row);
            }

            ds.Tables.Add(dt);

            return ds;
        }

另外一个项目调用webservice;添加web服务引用:

后台调用WS方法:

localhost.WebServiceTest service = new localhost.WebServiceTest();
            XmlDataDocument xd = new XmlDataDocument();
            XmlNode xmlNode1 = service.GetSiteAData();
            StringBuilder xmlString1 = new StringBuilder(xmlNode1.OuterXml);

            xd.LoadXml(xmlString1.ToString());

            DataSet ds = new DataSet();
            ds.ReadXml(new XmlNodeReader(xd));
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

 

 

posted on 2013-03-27 16:26  过往云烟  阅读(211)  评论(0编辑  收藏  举报