OrderData.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:2k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.EnterpriseServices;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. namespace Wrox.ProCSharp.EnterpriseServices
  6. {
  7. public interface IOrderUpdate
  8. {
  9. void Insert(Order order);
  10. void Update(Order order);
  11. }
  12. [Transaction(TransactionOption.Required)]
  13. [EventTrackingEnabled(true)]
  14. [ConstructionEnabled(true, Default="server=localhost;database=northwind;trusted_connection=true")]
  15. public class OrderData : ServicedComponent, IOrderUpdate
  16. {
  17. private string connectionString = null;
  18. public OrderData()
  19. {
  20. }
  21. #region IUpdateOrder Members
  22. [AutoComplete()]
  23. public void Insert(Order order)
  24. {
  25. SqlConnection connection = new SqlConnection(connectionString);
  26. try
  27. {
  28. SqlCommand command = connection.CreateCommand();
  29. command.CommandText = "INSERT INTO Orders (CustomerId, OrderDate, ShipAddress, ShipCity, ShipCountry)" +
  30. "VALUES(@CustomerId, @OrderDate, @ShipAddress, @ShipCity, @ShipCountry)";
  31. command.Parameters.Add("@CustomerId", order.CustomerId);
  32. command.Parameters.Add("@OrderDate", order.OrderDate);
  33. command.Parameters.Add("@ShipAddress", order.ShipAddress);
  34. command.Parameters.Add("@ShipCity", order.ShipCity);
  35. command.Parameters.Add("@ShipCountry", order.ShipCountry);
  36. connection.Open();
  37. int rows = command.ExecuteNonQuery();
  38. if (rows != 1)
  39. throw new Exception("error");
  40. command.CommandText = "SELECT @@IDENTITY AS 'Identity'";
  41. object identity = command.ExecuteScalar();
  42. order.SetOrderId(Convert.ToInt32(identity));
  43. OrderLineData updateOrderLine = new OrderLineData();
  44. foreach (OrderLine orderLine in order.OrderLines)
  45. {
  46. updateOrderLine.Insert(order.OrderId, orderLine);
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. System.Windows.Forms.MessageBox.Show(ex.Message);
  52. throw;
  53. }
  54. finally
  55. {
  56. connection.Close();
  57. }
  58. }
  59. public void Update(Order order)
  60. {
  61. }
  62. #endregion
  63. protected override void Construct(string s)
  64. {
  65. connectionString = s;
  66. }
  67. }
  68. }