Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

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

Hi,

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.

thanks,

KS
0
Ramesh Srinivas
Asked:
Ramesh Srinivas
  • 4
1 Solution
 
dstanley9Commented:
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);
  da.Fill(ds)
)  
 
0
 
dstanley9Commented:
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.
0
 
dstanley9Commented:
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.

0
 
Ramesh SrinivasTechnical ConsultantAuthor Commented:
dstanley9,

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)?

0
 
dstanley9Commented:
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)



0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now