Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Multiple Data Readers

Tags:

c#

sql-server

I am developing a WinForm Application in C Sharp on the .net framework. The database string I am using as of now is

<add key="Conn" value="Data Source=MNTCON016; Database=Overtime_Calculator;Trusted_Connection=True;MultipleActiveResultSets=true" />

As I am using Microsoft SQL Server 2005 for development, I can use 2 data readers simultaneously using the MultipleActiveResultSets property to true as mentioned above.

The Method used to invoke the 2 data readers is as follows:

    public static void SignUpControllerDay(DateTime Date, System.Windows.Forms.DataGridView PassedGrid)
    {
        string sql_SignUp = String.Format(@"SELECT Emp_ID as Emp_ID, Name as Name, Sum(Sum) as Sum FROM
                                            (SELECT DISTINCT o.Date, e.Emp_ID as Emp_ID,
                                            e.First_Name+ ' ' +e.Last_Name as Name,
                                            o.Quantity as Sum
                                            FROM Employee e,OT_Hours o,Position p,Signup_Sheet s
                                            WHERE e.Emp_ID=o.Emp_ID
                                            and e.Emp_ID = s.Employee_ID
                                            and s.Day_Shift = 1
                                            and e.Position_ID = p.Position_ID
                                            and p.Position_Name = 'Controller'
                                            and o.Quantity NOT IN(0.3)
                                            and s.Date = '{0}'
                                            and o.Date <= CONVERT(VARCHAR,'{0}',101) AND o.Date > CONVERT(VARCHAR,DATEADD(YYYY,-1,'{0}'),101) )
                                            as OVERTIME
                                            GROUP BY Emp_ID,Name
                                            ORDER BY Sum", Date);

        SqlConnection sqlConn = null;
        SqlCommand cmd_SignUp;
        SqlDataReader dr_SignUp;
        try
        {
            sqlConn = new SqlConnection(databaseConnectionString);
            sqlConn.Open();
            cmd_SignUp = new SqlCommand(sql_SignUp, sqlConn);
            dr_SignUp = cmd_SignUp.ExecuteReader();

            while (dr_SignUp.Read())
            {
                ArrayList arrPhone = new ArrayList();
                string sql_Phone = String.Format("SELECT Phone_Number FROM Contact_Details WHERE Emp_ID = {0}", dr_SignUp["Emp_ID"]);
                SqlCommand cmd_Phone = new SqlCommand(sql_Phone, sqlConn);
                SqlDataReader dr_Phone = cmd_Phone.ExecuteReader();
                while (dr_Phone.Read())
                {
                    arrPhone.Add(dr_Phone["Phone_Number"].ToString());
                }
                //--Retrieving Sectors
                ArrayList arrSector = new ArrayList();
                string sql_Sector = String.Format(@"SELECT e1.EMP_ID,
                                                ( SELECT cast(Sector_ID as varchar(10)) + ';'
                                                FROM Employee_Sector_relationship e2
                                                WHERE e2.Emp_ID = e1.Emp_ID
                                                ORDER BY Sector_ID
                                                FOR XML PATH('') ) AS Sectors
                                                FROM Employee_Sector_Relationship e1
                                                WHERE Emp_ID = {0}
                                                GROUP BY Emp_ID ", dr_SignUp["Emp_ID"]);
                SqlCommand cmd_Sector = new SqlCommand(sql_Sector, sqlConn);
                SqlDataReader dr_Sector = cmd_Sector.ExecuteReader();
                while (dr_Sector.Read())
                {
                    arrSector.Add(dr_Sector["Sectors"].ToString());
                }
                if (arrSector.Count == 0)
                { arrSector.Add(" "); }
                if (arrPhone.Count == 0)
                { arrPhone.Add(" "); }
                //--
                if (arrPhone.Count == 2)
                {
                    PassedGrid.Rows.Add(dr_SignUp["Emp_ID"].ToString(), dr_SignUp["Name"].ToString(), arrSector[0], dr_SignUp["Sum"], arrPhone[0], arrPhone[1]);
                }

                else
                {
                    PassedGrid.Rows.Add(dr_SignUp["Emp_ID"].ToString(), dr_SignUp["Name"].ToString(), arrSector[0], dr_SignUp["Sum"], arrPhone[0]);
                }
            }

        }
        catch (Exception e)
        {
            MessageBox.Show("Error found in SignUpControllerDay..." + Environment.NewLine + e.ToString());
        }
        finally
        {
            if (sqlConn != null)
            {
                sqlConn.Close();
            }
        }
    }

Everything works fine. Now the real problem. I have been informed that the production SQL server for the application to go live is Microsoft SQL server 2000. After doing a bit research, I came to know that Microsoft server 2000 does not support multiple active results sets propery. In short, it does not allow me to use 2 data readers simultaneously.

I need to know how to read the data from 2 different tables, simultaneously, with regards to SQL Server 2000.

Are there any other ways that i can read data as I have mentioned in the code..

Please help.. the application is almost done and is ready to go to production. but MS server 2000 doesnt allow the applcaition to work accordingly...

please help

like image 480
reggie Avatar asked Jan 13 '10 14:01

reggie


2 Answers

You can have two active datareaders in Sql Server 2000 by simply creating two connections.

To demonstrate this, I must first berate you for using two very poor practices: dynamic sql and arraylists. Neither have any place in your code. You should also read up on the using construct, though you have my apologies and condolences on "using" and "arraylists" if you're still using .net 1.1.

That said, here's how the code should look:

string sql_Phone = "SELECT Phone_Number FROM Contact_Details WHERE Emp_ID = @EmpID";
using (SqlConnection cn2 = new Sqlconnection(databaseConnectionString))
using (SqlCommand cmd_Phone = new SqlCommand(sql_Phone, cn2))
{
    cmd_Phone.Parameters.Add("@EmpID", SqlDbType.Int);
    cn2.Open();

    while (dr_SignUp.Read())
    {
        List<string> arrPhone = new List<string>();
        cmd_Phone.Parameters[0].Value = dr_SignUp["Emp_ID"];

        using (SqlDataReader dr_Phone = cmd_Phone.ExecuteReader())
        {
            while (dr_Phone.Read())
            {
                arrPhone.Add(dr_Phone["Phone_Number"].ToString());
            }
        }

Also, looking at your code I suspect what you really need to do is re-write your sql. You can combine all those into a single query that you just bind directly to the grid.

like image 154
Joel Coehoorn Avatar answered Oct 19 '22 04:10

Joel Coehoorn


You could also add MultipleActiveResultSets=True; to the connection string, even though it's not recommended.

like image 40
Brian Bergh Avatar answered Oct 19 '22 03:10

Brian Bergh