Calling stored procedure values into the console a

2019-08-26 02:11发布

I have a stored procedure that its been called by a console application in C#. The stored procedure has some required parameters to be executed. What I am trying to achieve its to be able to enter one of the required parameters into the console once the console is executed.

Something like having a text into the console asking to enter the project name and then hit on enter.

Here's my code:

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

3条回答
Lonely孤独者°
2楼-- · 2019-08-26 02:19

The values will be in order.

var someEnteredValue = Console.ReadLine();

//Hit enter and then:

var nextEnteredValue = Console.ReadLine();
查看更多
爷的心禁止访问
3楼-- · 2019-08-26 02:34

If I understand your question below is the code for you.

    //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.
查看更多
何必那么认真
4楼-- · 2019-08-26 02:34

There are two ways that I know a console application can get input. One is from the ReadLine or ReadKey method. The other is from the command line argument. If you run your executable in command prompt and supply some values, you can get it in your console app.

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

Screenshot of running the console in command prompt. enter image description here

You can also provide the command line arguments in Visual Studio, so you can test it without running it from the command prompt.enter image description here

查看更多
登录 后发表回答