Using Multiple Data Readers

前端 未结 5 1498
情书的邮戳
情书的邮戳 2021-01-11 19:16

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



        
相关标签:
5条回答
  • 2021-01-11 19:40

    Sure:

    public void SignUpControllerDay()
    {
        using (var conn = new SqlConnection(ConnectionString))
        using (var cmd = conn.CreateCommand())
        {
            conn.Open();
            cmd.CommandText = "SELECT ...";
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    var phone = reader["Phone_Number"].ToString();
                    Bar(phone);
                }
            }
        }
    }
    
    public void Bar(string phone)
    {
        using (var conn = new SqlConnection(ConnectionString))
        using (var cmd = conn.CreateCommand())
        {
            conn.Open();
            cmd.CommandText = "SELECT ..."; // use phone to prepare statement
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    // Fill the grid
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-11 19:41

    so, it is not possible! as simple as that the only answer!

    multiple connection is a workaroud!

    one connection can not handle multiple recordsets simultaneously

    0 讨论(0)
  • 2021-01-11 19:43

    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.

    0 讨论(0)
  • 2021-01-11 19:44

    You could open multiple database connections with 1 reader per connection

    0 讨论(0)
  • 2021-01-11 19:53

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

    0 讨论(0)
提交回复
热议问题