Solved

GridView DataSource

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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