Link to home
Start Free TrialLog in
Avatar of mawingpui
mawingpuiFlag for Hong Kong

asked on

GridView DataSource

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!
ASKER CERTIFIED SOLUTION
Avatar of ingriT
ingriT
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mawingpui

ASKER

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?
SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial