Quickest method to load a datatable... .Net 2.0

Posted on 2006-05-19
Last Modified: 2008-02-01

What is the quickest way to load data from a database (sqlserver) into a dataTable.

And also, what is the role of the BindingSource?

Bear in mind that the data is to be displayed in a datagrid.


Question by:saleek
    LVL 25

    Expert Comment

    Using weakly-typed data table:

    DataSet ds = new DataSet();
    using (SqlConnection conn = new SqlConnection("{insert connection string here}")
      SqlCommand command = new SqlCommand("SELECT * FROM {insert table name here}");
      command.Connection = conn;
      SqlDataAdapter da = new SqlDataAdapter(conn);
    LVL 25

    Expert Comment

    If you're using VS2005, You can locate the table in Server Explorer, add a DataSet to the project, and drag the table into the new DataSet.  It will create the adapter for you with a Fill() method that has the SQL pre-defined.  It's not any better than the above in terns of performance, but it saves a bit of coding.
    LVL 25

    Expert Comment

    From MSDN:

    The BindingSource component serves two purposes. First, it simplifies binding controls on a form to data by providing a layer of indirection, currency management, change notification, and other services This is accomplished by attaching the BindingSource component to your data source, and then binding the controls on your form to the BindingSource component. All further interaction with the data, including navigating, sorting, filtering, and updating, is accomplished with calls to the BindingSource component.

    Second, the BindingSource component can act as a strongly typed data source. Typically the type of the underlying data source is fixed through one of the following mechanisms:

    Use the Add method to add an item to the BindingSource component.

    Set the DataSource property to a list, single object, or type.

    Both of these mechanisms create a strongly-typed list. BindingSource supports both simple and complex data binding as indicated by its DataSource and DataMember properties.

    LVL 11

    Author Comment


    Thanks, i was actually looking for programmatic binding.

    So it's the same as 1.1 then?

    Have you used the DataTableReader (or whatever its called)?

    LVL 25

    Accepted Solution

    Not sure what you mean by programmatic binding, but you can use 1.1-style databinding.  The BindingSource just adds a layer of indirection to the datasource.

    I have not used the DataTableReader, but here's what I've read about it:

    * The DataTableReader is just a read-only ("firehose") style way of accessing a DataTable.  Just like a SQLDataReader

    * It does not seem to be significanlty faster (if at all) that using foreach(DataRow row in table.Rows).

    * You can pass it from one application layer to another (unlike SqlDataReader)

    * It can be used to provide a safe way for clients to access the DataTAble without having to worry about the client making changes (they cannot modify or delete rows)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now