Solved

GridView DataSource

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

12 Experts available now in Live!

Get 1:1 Help Now