Solved

GridView DataSource

Posted on 2010-09-19
5
414 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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 R2, need a pivot/cross tab query... 4 48
Parse this column 6 34
Making exceptions for ValidationAttribute 2 23
Set custom font WPF 2 21
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!
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 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