Using SqlDataAdapter to fill DataTable in c#

public DataTable List(string sql)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“DefaultConnection”].ConnectionString);
            SqlCommand cmd = new SqlCommand(sql, con);

            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {             
                con.Open();
                da.Fill(dt);
            }
            finally
            {
                con.Close();
            }
            return dt;
        }

Leave a Reply

Your email address will not be published. Required fields are marked *