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 6790083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:40:59+00:00 2026-05-26T17:40:59+00:00

I have a service which continuously writes data in a separate thread into SQL

  • 0

I have a service which continuously writes data in a separate thread into SQL database.Now from the same service if i am trying to read from the same table, since i already am writing into it,I get this exception : There is already an open DataReader associated with this Command which must be closed first.
So can anyone help me how to do this simultaneously?

Here s my code for reading data:

public Collection ReadData(string query)
{

        {

            _result = new Collection<string[]>();
            string[] tempResult;
            SqlDataReader _readerRead;
            using (_command = new SqlCommand(query, _readConnection))
            {
                _readerRead = _command.ExecuteReader();
                while (_readerRead.Read())
                {
                    tempResult = new string[4];
                    tempResult[0] = _reader[0].ToString();
                    tempResult[1] = _reader[1].ToString();
                    tempResult[2] = _reader[2].ToString();
                    tempResult[3] = _reader[3].ToString();
                    _result.Add(tempResult);
                    //Console.WriteLine("Name : {0} Type : {1} Value : {2} timestamp : {3}", _reader[0], _reader[1], _reader[2], _reader[3]);

                }
                if (_readerRead != null)
                {
                    _readerRead.Close();
                }
                _readConnection.Close();
                return _result;

            }
        }
    }

and here it is for writing to it :

public void WriteData(Collection<TagInfo> tagInfoList)
    {

        int i = 0;
        for (i = 0; i < tagInfoList.Count; i++)
        {
           using( _command = new SqlCommand(insert statement here)
            {
                _command.Parameters.AddWithValue("Name", tagInfoList[i].Name);
                _command.Parameters.AddWithValue("Type", tagInfoList[i].TagType);
                _command.Parameters.AddWithValue("Value", tagInfoList[i].Value);
                _reader = _command.ExecuteReader();
                if (_reader != null)
                {
                    _reader.Close();
                }
            }
        }

    }
  • 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-05-26T17:40:59+00:00Added an answer on May 26, 2026 at 5:40 pm

    Although its possible to do, using a separate connection I would question why you need to do this.

    If you are reading and writing data to one table in the same service you will be placing unnecessary load on one SQL table, and depending on the number of queries you intend to make this could cause you problems. If you already have this data (in a different thread) why not Marshall the data from the background thread to where you need it as you write it into the database, and you don’t need to read the data anymore.

    However…. it is difficult to give an fair answer without seeing the code/what you are looking to achieve.

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

Sidebar

Related Questions

I have a windows service which fetches data from various datasources and build a
I have background service which access my SQL Server database. My background service working
i have a service which fetches some data from network.i am sending data download
I have a WCF Service which returns 1000 records from database to the client.
I have a WCF service which will be hosted under IIS. Now I have
I have service which is not invoked by user request, but by task. Now
In C# VS2008 I have a service which can run from command line and
I have server service which releases the table database if a certain criterian is
I have WCF service which launches the remote process from Process.Start successfully on stand
I have a WCF service which sits in front of a data collection. I

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.