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

C# 将存储过程值调用到控制台应用程序C中#,c#,visual-studio-2012,stored-procedures,console-application,readline,C#,Visual Studio 2012,Stored Procedures,Console Application,Readline,我有一个存储过程,它被C#中的控制台应用程序调用。存储过程有一些需要执行的参数。 我试图实现的目标是,一旦控制台被执行,就能够在控制台中输入所需的参数之一 类似于在控制台中输入一条文本,要求输入项目名称,然后按enter键 这是我的密码: try { using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Test"].Connec

我有一个存储过程,它被C#中的控制台应用程序调用。存储过程有一些需要执行的参数。 我试图实现的目标是,一旦控制台被执行,就能够在控制台中输入所需的参数之一

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

这是我的密码:

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();

您好,是的,但我不知道如何添加Console.ReadLine(),调用中没有值您好您的解决方案工作得很好,但问题是输出文件中的test.csv是字符串outCsvFile中的内容。有一种方法可以将文件名作为项目标题添加到控制台?中,再次感谢您的帮助。我刚刚尝试了它,并使用了word。对不起,最后一件事。我注意到,如果在控制台中没有输入任何项目名称(只需按enter键),它将显示错误消息,但是如果我想在输入项目名称之前将其置于给出消息的方式,那么最好的方法是什么?再次感谢你用while条件来更新答案,而不是我刚刚尝试过并且成功了。你是最棒的!!!!非常感谢你的耐心和时间!!!对不起,我不知道现在问你以下问题是否太晚了。我想问用户多少次,他想输入一个项目名称,直到他点击X关闭窗口而不完成应用程序。这是可能的吗?再次感谢