Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

GridView DataSource

Posted on 2010-09-19
5
413 Views
Last Modified: 2012-06-21
I have the following codes.

Code 1
 
            string connectionString = @"Data Source=.\SQLExpress;Initial Catalog=master;Integrated Security=True";
            string sql = "SELECT TOP 10 * FROM [master].[sys].[syslanguages]";

            using (SqlConnection connection = new SqlConnection(connectionString))
            using (SqlCommand command = new SqlCommand(sql, connection))
            {
                connection.Open();

                DataTable dt = new DataTable();
                SqlDataReader reader = command.ExecuteReader();
                dt.Load(reader);
                connection.Close();
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

Open in new window


Code 2
 
            string connectionString = @"Data Source=.\SQLExpress;Initial Catalog=master;Integrated Security=True";
            string sql = "SELECT TOP 10 * FROM [master].[sys].[syslanguages]";

            using (SqlConnection connection = new SqlConnection(connectionString))
            using (SqlCommand command = new SqlCommand(sql, connection))
            {
                connection.Open();

                SqlDataReader reader = command.ExecuteReader();
                GridView1.DataSource = reader;
                GridView1.DataBind();
            }

Open in new window


Both codes work absolutely fine, but which one is better?
Thanks!
0
Comment
Question by:mawingpui
5 Comments
 
LVL 6

Accepted Solution

by:
ingriT earned 125 total points
ID: 33711748
I think the second, why waste memory on a datatable when you don't need it?
0
 

Author Comment

by:mawingpui
ID: 33711780
thanks ingriT, I am also thinking about database performance.
If I use the reader as DataSource, will it hold the connection for a long time when dealing with large amounts of data?
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 125 total points
ID: 33711906
Both your examples do pretty much the same thing. There isn't much point in loading the data into a DataTable unless you are planning on caching the data and allowing filtering/sorting and therefore want to keep a copy of the data available so you don't have to requery the database.
0
 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 125 total points
ID: 33712090
Yes, There is not use of loading that data into DataTable if you are not planning to cache that data. So Please use second example

please refer to http://www.eggheadcafe.com/articles/20030205.asp
0
 
LVL 12

Assisted Solution

by:starlite551
starlite551 earned 125 total points
ID: 33712338
I prefer using Disconnected Architecture in ADO.NET.. DataSets are cool coz they dont need Open Connections as in case of SqlDataReaders..
So I would rather go for the following code :

string connectionString = @"Data Source=.\SQLExpress;Initial Catalog=master;Integrated Security=True";
string sql = "SELECT TOP 10 * FROM [master].[sys].[syslanguages]";

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(sql, connection))
{
       connection.Open();
       SqlDataAdapter da = new SqlDataAdaper(cm);
       DataSet ds = new DataSet();
       da.Fill(ds);
       GridView1.DataSource = ds.Tables[0];
       GridView1.DataBind();  
}

Open in new window

0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

861 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