0

I am using an insert statement as below, and I want to implement a basic 'multi thread' for it. I'm new to programming, could someone please explain how would I do that? I dont want to do it in just the display, but in the actual insert part.

I also wanna know is it possible to display the data in a normal listBox?


private void btnInsert_Click(object sender, EventArgs e)
        {
try
            {
                using (SqlConnection connection = new SqlConnection(c.getConnection()))
                {
                   SqlCommand command = new
                   SqlCommand("INSERT INTO Weather_Details " +
                   "VALUES (@CityName, @CityDate, @MiniTemp, @MaxiTemp," +
                   " @Precipitation, @Humidity, @Windspeed)", connection);

                    
                    //dont use ID field cos auto generated
                    command.Parameters.AddWithValue("@CityName", this.comboBox1.Text);
                    command.Parameters.AddWithValue("@CityDate", this.dateTimePicker1.Value);
                    command.Parameters.AddWithValue("@MiniTemp", this.numMini.Value);
                    command.Parameters.AddWithValue("@MaxiTemp", this.numMaxi.Value);
                    command.Parameters.AddWithValue("@Precipitation", this.numPrecip.Value);
                    command.Parameters.AddWithValue("@Humidity", this.numHumid.Value);
                    command.Parameters.AddWithValue("@Windspeed", this.numWind.Value);

                    connection.Open();
                    int temp = command.ExecuteNonQuery();
                    if (temp > 0)
                    {
                        MessageBox.Show("City has been captured successfully!",
                        "New City Added");
                    }
                    clear();
                }
            }
            catch (SqlException ex)
            {
                //If displayed like this,
                MessageBox.Show(ex.Message, "Error Message");
                //instead of this,
                //MessageBox.Show("Error Connecting to the Database", "Connection Error");
                //it shows the actual error and not the hardcoded error of it not connecting to DB
            }
        }
    }

It doesn't have to be complicated, I have this code but don't know how to implement it.


    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"Main Thread: {Thread.CurrentThread.ManagedThreadId} Started");


            Task task1 = Task.Run(() =>
            {
                PrintCounter();
            });
            task1.Wait();
            Console.WriteLine($"Main Thread: {Thread.CurrentThread.ManagedThreadId} Completed");
            Console.ReadKey();

        }
        static void PrintCounter()
        {
            Console.WriteLine($"Cild Thread: {Thread.CurrentThread.ManagedThreadId} Started");
            for (int count = 1; count <=5; count++)
            {
                Console.WriteLine($"Count: {count}");
            }
            Console.WriteLine($"Cild Thread: {Thread.CurrentThread.ManagedThreadId} Completed");
        }
    }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Coder1989
  • 17
  • 1
  • 6
  • 1
    Is there any special reason to go "multi threaded"? It may be too advanced for someone "new to programming". Start with the basics! – Hans Kesting Oct 10 '20 at 11:23
  • Develop good habits. Don't use [addwithvalue](http://www.dbdelta.com/addwithvalue-is-evil/). – SMor Oct 10 '20 at 12:03
  • Rather than parallelism, consider inserting all of them in one go. https://www.learndapper.com/bulk-insert https://stackoverflow.com/questions/38521908/insert-multiple-rows-with-parameters-sql-server – mjwills Oct 10 '20 at 12:19
  • Alternatively, consider using asynchrony. [ExecuteNonQueryAsync](https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executenonqueryasync?view=dotnet-plat-ext-3.1) – Alexander Petrov Oct 10 '20 at 13:12
  • @HansKesting Yes cos the exercise says we should use it, but I cannot get it into my insert statement. – Coder1989 Oct 10 '20 at 18:44
  • @SMor, so what do I use? – Coder1989 Oct 10 '20 at 18:44
  • Did you read the [AddWithValue is Evil](http://www.dbdelta.com/addwithvalue-is-evil/) link? It tells you in there why AddWithValue is bad and demonstrates how you should be adding parameters instead. – AlwaysLearning Oct 11 '20 at 01:15

0 Answers0