Using Multiple Data Readers

前端 未结 5 1500
情书的邮戳
情书的邮戳 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: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 arrPhone = new List();
            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.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题