技术开发 频道

如何使用webservice作为数据源去生成Microsoft Reporting Services 2


【IT168技术文档】

  新建一个webservice
  第一步是新建一个webservice,稍后我将用这个webservice作为我的报表的数据源。这是非常重要的一步,因为我们要将数据转换成一个XmlDataDocument。如果没有这步转换,我们将不能得到使用webservice的web方法取得数据的结果。实现这个转换的C#代码如下:
[WebMethod] public XmlDataDocument GetPersonAddress(string cityNameID) { // // 定义一些变量 // StringBuilder myQuery = new StringBuilder(); XmlDataDocument resultXMLDocument = new XmlDataDocument(); SqlConnection myConnection = new SqlConnection(); SqlCommand myCommand = new SqlCommand(); SqlDataAdapter myDA = new SqlDataAdapter(); DataSet myDS = new DataSet(); // // 根据参数的不同准备不同的查询语句 // if ((cityNameID != null) && (cityNameID.Trim() != "")) { myQuery.Append("Select City as City, " + "AddressLine1 as Address, " + "PostalCode From Address "); myQuery.Append("Where City Like '" + cityNameID.Trim().Replace("%", "") + "%' Order By City"); } else { myQuery.Append("Select City as City, AddressLine1" + " as Address, PostalCode From Address" + " Order By City"); } // // 得到连接字符串并建立到服务器的连接 // myConnection.ConnectionString = ReadSetting("ConnectionString", ""); myCommand.Connection = myConnection; myCommand.CommandText = myQuery.ToString(); myCommand.CommandType = CommandType.Text; myDA.SelectCommand = myCommand; // // 返回一个DataSet数据 // try { myDA.Fill(myDS, "Address"); // // 转换我们的DataSet到XmlDataDocument // XmlDataDocument temporaryXMLDoc = new XmlDataDocument(myDS); resultXMLDocument = temporaryXMLDoc; temporaryXMLDoc = null; } catch { resultXMLDocument = null; } finally { myDS.Dispose(); myDA.Dispose(); myCommand.Dispose(); myConnection.Dispose(); myQuery = null; } return resultXMLDocument; }
0
相关文章