orderlinedata.cs

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

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

namespace Wrox.ProCSharp.EnterpriseServices
{

    [ComVisible(true)]
    public interface IOrderLineUpdate
    {
        void Insert(int orderId, OrderLine orderDetail);
    }

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

        protected override void Construct(string s)
        {
            connectionString = s;
        }
        public void Insert(int orderId, OrderLine orderDetail)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandText = "INSERT INTO [Order Details] (OrderId, " +
                   "ProductId, UnitPrice, Quantity)" +
                   "VALUES(@OrderId, @ProductId, @UnitPrice, @Quantity)";
                command.Parameters.AddWithValue("@OrderId", orderId);
                command.Parameters.AddWithValue("@ProductId", orderDetail.ProductId);
                command.Parameters.AddWithValue("@UnitPrice", orderDetail.UnitPrice);
                command.Parameters.AddWithValue("@Quantity", orderDetail.Quantity);

                connection.Open();

                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                ContextUtil.SetAbort();
                throw;
            }
            finally
            {
                connection.Close();
            }
            ContextUtil.SetComplete();
        }
    }
}

⌨️ 快捷键说明

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