How bad is opening and closing a SQL connection for several times? What is the exact effect?

后端 未结 3 1116
独厮守ぢ
独厮守ぢ 2021-01-14 14:51

For example, I need to fill lots of DataTables with SQLDataAdapter\'s Fill() method:

DataAdapter1.Fill(DataTable1);
DataAdapter2.Fill(DataTable2);
DataAdapte         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-14 15:36

    NECRO ANSWER: The best way is to place the connection in a 'using' statement so that it is scoped to the work it needs to do:

    using (SqlConnection conn = new SqlConnection())
    {
        DataAdapter1.Fill(DataTable1);
        DataAdapter2.Fill(DataTable2);
        DataAdapter3.Fill(DataTable3);
        DataAdapter4.Fill(DataTable4);
        DataAdapter5.Fill(DataTable5);
        ...
        ...
    }
    

提交回复
热议问题