Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GridView DataSource

Posted on 2010-09-19
5
Medium Priority
?
424 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
[X]
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
5 Comments
 
LVL 6

Accepted Solution

by:
ingriT earned 500 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 500 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 500 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 500 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

721 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