Trouble loading SQL Data Reader data into DataTable

后端 未结 1 1478
忘掉有多难
忘掉有多难 2021-01-05 19:27
string query = \"select * from cfo_daily_trans_hist\";
            try
            {
                using (SqlConnection connection = new SqlConnection(
                    


        
相关标签:
1条回答
  • 2021-01-05 19:34

    Instead of a SqlDataReader, use a SqlDataAdapter.

    SqlDataAdapter myAdapter = new SqlDataAdapter(command);
    myAdapter.Fill(datatable);
    

    With a SqlDataAdapter, you don't need to explicitly call SqlConnection.Open() and SqlConnection.Close(). It is handled in the Fill() method.

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