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);
}
The values will be in order.
//Hit enter and then:
If I understand your question below is the code for you.
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.
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.