Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:51:59+00:00 2026-06-17T18:51:59+00:00

I have an application(C#) which is inserting data to database(MySQL). The program runs on

  • 0

I have an application(C#) which is inserting data to database(MySQL). The program runs on multithreads with a timer.

The question of mine is the following, what would be the best way to insert data to the database.

Example of the application:

The timer tick each second, I have data which I would like to insert to the database in each second, but I have some which I want to insert rearer, like every half minute. The data always goes to different tables, but same database.

I did always open and close the database connection after each insertion. In that case I had problems when I want to open an already open connection or use a connection which is already closed by the other thread. To fix that problem I open the connection when I started the timer and closed it when I stopped it. It is working the way like that but not so safe.

With this solution my database still get confused sometimes. I just tried it with local database, I do not know if it would be different with remote one or not(be honest it should work with local and remote DB as well).

There was an idea to have an extra method which is collect the data end it uploads after each tick(or after X tick) to the database. I am not sure if that would be a better solution or not, but I do not really have better idea just now. The problem with that solution is that it could get messy, (I guess I would need a global list what could be accessed by all of the threads and insert data in it, It could cause of a problem when I upload/clear the list while one of the threads might want to access to it).

Please let me know if it is not clear enough. Any suggestions will be appreciated.

Thanks

My code for databse insertion is the following:


con.Open();
MySqlCommand cmd = new MySqlCommand(command, con);
cmd.ExecuteNonQuery();
con.Close();

I think I should insert here some pseudo code as well, to make my problem more understandable:


Timer
foreach(sensor)
{
if sensor reading finished
{
check the type of the sensor
run the right thread
}


Example thread:


Thread
Read out data from sensor
insert data to DB

Response of the sensors are different, but still can be the same. This is why I have problem inserting to the database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:52:01+00:00Added an answer on June 17, 2026 at 6:52 pm

    I did always open and close the database connection after each insertion […] It is working the way like that but not so safe.

    It is certainly the best way. Use the Connection in a using () {} block and it will be safe.

    With this solution my database still get confused sometimes.

    You will have to describe that a lot better.

    There was an idea to have an extra method which is collect the data end it uploads after each tick

    Yes, you can (should) probably use a extra Thread to push things to the Db. Look up “Producer/Consumer Pattern”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a flex application which I try to use for inserting data into
I have a web application that relies on a MySQL database, for which I
I have an application which is receiving data for thousands (say 50,000) subjects. Each
I have an application which loads up c# source files dynamically and runs them
I am using a hibernate application to store data in Postgres database I have
I want to accomplish the following task. I have an web application through which
I have one application which uses NHibernate to save entities to a database, and
I have a mysql database which has 3 tables that have to be joined
I have created the application which is based on Sqlite database. So in my
I have application which uses Sherlock ActionBar package. The application uses platform-specific behavior for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.