将存储过程值调用到控制台应用程序 C#

Calling stored procedure values into the console application C#

我有一个存储过程,它由 C# 中的控制台应用程序调用。存储过程有一些需要执行的参数。 我正在努力实现的目的是在执行控制台后能够将所需参数之一输入控制台。

类似于在控制台中输入一条文本,要求输入项目名称,然后按回车键。

这是我的代码:

try
{
    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString))
    {
        string outCsvFile = @"D:\test\test.csv";

        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Connection = conn;
        sqlCmd.CommandText = "projects";
        //Adding values to the stored procedure
        sqlCmd.Parameters.AddWithValue("@ProjectTitle", "Test Project 1"); //this value I would like to add it when the console application is executed when I will hit on enter.
        sqlCmd.Parameters.AddWithValue("@Category", "");

        conn.Open();

        SqlDataReader reader = sqlCmd.ExecuteReader();
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(outCsvFile)) 
            {
                file.WriteLine(reader.GetName(0) + ',' + reader.GetName(1));

            while (reader.Read())
                file.WriteLine(reader[0].ToString() + ',' + reader[1].ToString());
               }

        conn.Close();
    }

}
catch (Exception e) 
{ 
    Console.WriteLine(e.Message);
    System.Threading.Thread.Sleep(8000);
}

如果我理解你的问题,下面是适合你的代码。

    //Adding values to the stored procedure
                Console.WriteLine("Please enter project title and press Enter");

                string projectTitle = Console.ReadLine();
                while (String.IsNullOrWhiteSpace(projectTitle))
                {
                    Console.WriteLine("Please enter project title and press Enter");
                    projectTitle = Console.ReadLine();
                }


                string outCsvFile = string.Format(@"D:\test\{0}.csv", projectTitle);
                if (System.IO.File.Exists(outCsvFile))
                {
                    throw new ApplicationException("File already exist Name " + outCsvFile);
                }
                sqlCmd.Parameters.AddWithValue("@ProjectTitle", projectTitle); //this value I would like to add it when the console application is executed when I will hit on enter.

据我所知,控制台应用程序有两种获取输入的方法。一种来自 ReadLine 或 ReadKey 方法。另一个来自命令行参数。如果您 运行 在命令提示符下执行您的可执行文件并提供一些值,您可以在您的控制台应用程序中获取它。

using System;

namespace ConsoleParameter
{
    class Program
    {
        static void Main(string[] args)
        {
            // get from the command line parameter
            Console.WriteLine("Parameter in command line");
            foreach (var arg in args)
            {
                Console.WriteLine(arg);
            }

            // get from keyboard input when the console is running
            Console.WriteLine("Please input something");
            var value = Console.ReadLine();
            Console.WriteLine("You entered {0}", value);

            Console.ReadKey(true);
        }
    }
}

运行在命令提示符下打开控制台的屏幕截图。

您还可以在 Visual Studio 中提供命令行参数,这样您就可以在不从命令提示符 运行 启动它的情况下对其进行测试。

值将按顺序排列。

var someEnteredValue = Console.ReadLine();

//回车然后:

var nextEnteredValue = Console.ReadLine();