0

我想编写一个 C# Web 服务,它通过select查询从 SQL Server 2008 获取数据并将其保存在 XML 文件中。注意:我使用 LinQ。

我怎样才能做到这一点?

我使用了这段代码,但它有更多错误。

[WebMethod]
public bool Search(string XmlSearch)
{
   using (var MyCon = new MainContextDataContext())
   {
      MyCon.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["HQ_TestConnectionString"].ConnectionString;

      XElement SearchCust = XDocument.Parse(XmlSearch).Root.Elements().First();

      string AccountNumber = SearchCust.Element("Customer").Attribute("AccountNumber").Value;
      string Phone = SearchCust.Element("Customer").Attribute("PhoneNumber").Value;
      string Name = SearchCust.Element("Customer").Attribute("FirstName").Value;

      if (MyCon.Customers.Where(i => i.AccountNumber == AccountNumber).Count() > 0)
      {
         SqlConnection MyConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HQ_TestConnectionString"].ConnectionString);
         SqlCommand cmd = new SqlCommand("SELECT customer.AccountNumber, ShipTo.Address, ShipTo.Address2 FROM Customer FULL OUTER JOIN ShipTo ON customer.AutoID = ShipTo.AutoID WHERE customer.AccountNumber = " + AccountNumber, MyConn);
         cmd.Connection.Open();
         object value = cmd.ExecuteScalar();
         MyConn.Close();
         SqlDataReader Reader = cmd.ExecuteReader();

         string SRD = string.Empty;

         if (Reader.HasRows)
         {
            if (Reader.Read())
            {
               SRD = Reader.ToString();
            }
         }

         Reader.Close();
         System.Xml.XmlDocument Xdoc = new System.Xml.XmlDocument();
         Xdoc.InnerXml = SRD;
         Xdoc.Save("d:\\xmltest.xml");
      }
      return false;
   }
}
4

0 回答 0