OrderData.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:2k
- using System;
- using System.EnterpriseServices;
- using System.Data;
- using System.Data.SqlClient;
- namespace Wrox.ProCSharp.EnterpriseServices
- {
- public interface IOrderUpdate
- {
- void Insert(Order order);
- void Update(Order order);
- }
- [Transaction(TransactionOption.Required)]
- [EventTrackingEnabled(true)]
- [ConstructionEnabled(true, Default="server=localhost;database=northwind;trusted_connection=true")]
- public class OrderData : ServicedComponent, IOrderUpdate
- {
- private string connectionString = null;
- public OrderData()
- {
- }
- #region IUpdateOrder Members
- [AutoComplete()]
- public void Insert(Order order)
- {
- SqlConnection connection = new SqlConnection(connectionString);
- try
- {
- SqlCommand command = connection.CreateCommand();
- command.CommandText = "INSERT INTO Orders (CustomerId, OrderDate, ShipAddress, ShipCity, ShipCountry)" +
- "VALUES(@CustomerId, @OrderDate, @ShipAddress, @ShipCity, @ShipCountry)";
- command.Parameters.Add("@CustomerId", order.CustomerId);
- command.Parameters.Add("@OrderDate", order.OrderDate);
- command.Parameters.Add("@ShipAddress", order.ShipAddress);
- command.Parameters.Add("@ShipCity", order.ShipCity);
- command.Parameters.Add("@ShipCountry", order.ShipCountry);
- connection.Open();
- int rows = command.ExecuteNonQuery();
- if (rows != 1)
- throw new Exception("error");
- command.CommandText = "SELECT @@IDENTITY AS 'Identity'";
- object identity = command.ExecuteScalar();
- order.SetOrderId(Convert.ToInt32(identity));
- OrderLineData updateOrderLine = new OrderLineData();
- foreach (OrderLine orderLine in order.OrderLines)
- {
- updateOrderLine.Insert(order.OrderId, orderLine);
- }
- }
- catch (Exception ex)
- {
- System.Windows.Forms.MessageBox.Show(ex.Message);
- throw;
- }
- finally
- {
- connection.Close();
- }
- }
- public void Update(Order order)
- {
- }
- #endregion
-
- protected override void Construct(string s)
- {
- connectionString = s;
- }
- }
- }