控制台应用程序中的存储过程

Stored procedure in console application

我已经创建了控制台应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Objects;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            NorthwindEntities nwe = new NorthwindEntities();

        var customerOrdersDetail = nwe.CustOrdersDetail(10248);

        foreach (var ordersDetail in customerOrdersDetail)
            {
            Console.WriteLine(ordersDetail.ProductName);
            Console.WriteLine(ordersDetail.UnitPrice);
            Console.WriteLine(ordersDetail.Quantity);
            Console.WriteLine(ordersDetail.ExtendedPrice);
            Console.WriteLine(ordersDetail.Discount);
            Console.ReadLine();
            }
        }
    }
}

它显示了我在 nwe.CustOrdersDetail(); 中输入 ID 的产品的所有详细信息,但我很感兴趣,我可以在应用程序启动时直接输入 ID 吗?当我想显示一些其他产品的信息时,不要重新启动应用程序会更有用。

解析 args 数组并将其传递,如下所示:

var customerOrdersDetail = nwe.CustOrdersDetail(int.Parse(args[0]));

这样你就可以像这样从命令行使用它:

ConsoleApplication3 10248
ConsoleApplication3 10249
ConsoleApplication3 10250

您需要隔离获取订单详细信息项目的功能并将其放入您可以重复调用的内容中,因为这只是一个控制台应用程序,类似这样的内容应该可以用来演示:

private static void Main(string[] args)
{
    FetchOrderDetail();
}

private static void FetchOrderDetail()
{
    string input = string.Empty;

    while (input != "exit")
    {
        Console.WriteLine("Enter order detail ID: ");
        input = Console.ReadLine();

        int orderId = 0;
        if (Int32.TryParse(input, out orderId))
        {
            var customerOrdersDetail = nwe.CustOrdersDetail(orderId);

            foreach (var ordersDetail in customerOrdersDetail)
            {
                Console.WriteLine(ordersDetail.ProductName);
                Console.WriteLine(ordersDetail.UnitPrice);
                Console.WriteLine(ordersDetail.Quantity);
                Console.WriteLine(ordersDetail.ExtendedPrice);
                Console.WriteLine(ordersDetail.Discount);
                Console.ReadLine();
            }
        }
    }
}

you could write in a loop until something is encountered (or not) that just takes in IDs with a Console.ReadLine() Or you could use your string[] args to pass in an ID at start (though this seems like it would be more difficult to "change" the ID over the first

public class Program
{
    public static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine("Enter an ID, q to quit");
            string line = Console.ReadLine();

            if (line == "q")
                break;

            int myId = int.Parse(line); // note this will throw if you enter a non int (and not q)
            OutputInformation(myId);

        }

        Console.WriteLine("Done");
    }

    public static void OutputInformation(int Id)
    {
            NorthwindEntities nwe = new NorthwindEntities();

            var customerOrdersDetail = nwe.CustOrdersDetail(myId);

            foreach (var ordersDetail in customerOrdersDetail)
            {
                Console.WriteLine(ordersDetail.ProductName);
                Console.WriteLine(ordersDetail.UnitPrice);
                Console.WriteLine(ordersDetail.Quantity);
                Console.WriteLine(ordersDetail.ExtendedPrice);
                Console.WriteLine(ordersDetail.Discount);
                Console.ReadLine();
            }
    }
}

您为什么不简单地输入您想要 运行 的 ID 列表并从 args 数组访问它们:

foreach (var input in args)
{
    Console.WriteLine(int.Parse(input));
}

这样称呼它:ConsoleApplication3 10248 10249 10250