I have written the below code. I want to know if i can improve it any further.
public static DataTable GetDepartments()
{
DataTable dt = new DataTable();
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand())
{
command.CommandText = "proc_GetDepartments";
command.CommandType = CommandType.StoredProcedure;
connection.Open();
using (SqlDataAdapter da = new SqlDataAdapter(command))
{
command.Connection = connection;
da.Fill(dt);
}
}
}
return dt;
}
Here i have used SqlDataAdapter. What would be the SqlDataReader way to write it. Also which one is better. Any help/guidance is appreciated.
:)you can also useSqlDataReaderbut seems pointless to me since you have no modification on the value from the resultset.SqlDataReaderabove.SqlDataAdapteris using with the aDataTable. It can fill aDataTablewith a table from your SQL.SqlDataReaderreads database rows one-by-one.