orderdata.cs

来自「C#高级编程第6版随书源代码 值得下载」· CS 代码 · 共 73 行

CS
73
字号
using System;
using System.Data.SqlClient;
using System.EnterpriseServices;
using System.Runtime.InteropServices;

namespace Wrox.ProCSharp.EnterpriseServices
{
    [ComVisible(true)]
    public interface IOrderUpdate
    {
        void Insert(Order order);
    }

    [Transaction(TransactionOption.Required)]
    [EventTrackingEnabled(true)]
    [ConstructionEnabled(true, Default = "server=(local);" +
        "database=northwind;trusted_connection=true")]
    [ComVisible(true)]
    public class OrderData : ServicedComponent, IOrderUpdate
    {
       private string connectionString;

        protected override void Construct(string s)
        {
            connectionString = s;
        }

        [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.AddWithValue("@CustomerId", order.CustomerId);
                command.Parameters.AddWithValue("@OrderDate", order.OrderDate);
                command.Parameters.AddWithValue("@ShipAddress", order.ShipAddress);
                command.Parameters.AddWithValue("@ShipCity", order.ShipCity);
                command.Parameters.AddWithValue("@ShipCountry", order.ShipCountry);

                connection.Open();

                command.ExecuteNonQuery();

                command.CommandText = "SELECT @@IDENTITY AS 'Identity'";
                object identity = command.ExecuteScalar();
                order.SetOrderId(Convert.ToInt32(identity));

                using (OrderLineData updateOrderLine = new OrderLineData())
                {
                   foreach (OrderLine orderLine in order.OrderLines)
                   {
                      updateOrderLine.Insert(order.OrderId, orderLine);
                   }
                }

                // System.Threading.Thread.Sleep(TimeSpan.FromSeconds(50));
            }

            finally
            {
                connection.Close();
            }
        }
    }
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?