Calling stored procedure values into the console application C#

狂风中的少年 提交于 2019-12-25 14:22:51

问题


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

回答1:


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.



回答2:


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.

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




回答3:


The values will be in order.

var someEnteredValue = Console.ReadLine();

//Hit enter and then:

var nextEnteredValue = Console.ReadLine();


来源:https://stackoverflow.com/questions/31816049/calling-stored-procedure-values-into-the-console-application-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!