background preloader

SqlCommand sqlDataReader sqlDataAdapter

Facebook Twitter

Populate DropDownList. DropDownList Class (System.Web.UI.WebControls) Represents a control that allows the user to select a single item from a drop-down list. [ValidationPropertyAttribute("SelectedItem")] public class DropDownList : ListControl, IPostBackDataHandler The DropDownList type exposes the following members. In this topic: Introduction The DropDownList control also supports data binding. To bind the control to a data source, create a data source, such as a System.Collections.ArrayList object, that contains the items to display in the control. Then, use the Control.DataBind method to bind the data source to the DropDownList control. Accessibility Declarative Syntax A Visual Studio Web site project with source code is available to accompany this topic: Download.

The following code example demonstrates how to create a DropDownList control that contains four items. <%@ Page Language="C#" AutoEventWireup="True" %><! The following code example demonstrates how to create a DropDownList control though data binding. .NET Framework. SqlCommand.ExecuteNonQuery Method (System.Data.SqlClient) Executes a Transact-SQL statement against the connection and returns the number of rows affected. Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data. For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers.

For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1. private static void CreateCommand(string queryString, string connectionString) { using (SqlConnection connection = new SqlConnection( connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); command.Connection.Open(); command.ExecuteNonQuery(); } } .NET Framework Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0. C# - SqlDataAdapter vs SqlDataReader. SqlDataReader Class (System.Data.SqlClient) Changes made to a result set by another process or thread while data is being read may be visible to the user of the SqlDataReader. However, the precise behavior is timing dependent. SqlDataAdapter Class (System.Data.SqlClient) When an instance of SqlDataAdapter is created, the read/write properties are set to initial values.

For a list of these values, see the SqlDataAdapter constructor. SqlCommand Class (System.Data.SqlClient) Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database. This class cannot be inherited. public sealed class SqlCommand : DbCommand, ICloneable The SqlCommand type exposes the following members. When an instance of SqlCommand is created, the read/write properties are set to their initial values. For a list of these values, see the SqlCommand constructor. SqlCommand features the following methods for executing commands at a SQL Server database: private static void ReadOrderData(string connectionString) { string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders;"; using (SqlConnection connection = new SqlConnection( connectionString)) { SqlCommand command = new SqlCommand( queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); try { while (reader.Read()) { Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1])); } } finally { // Always call Close when done reading. reader.Close(); } } }

What is the difference between data reader and data adapter ? Achint Kishore Answered On : Aug 6th, 2006 DataReader directly access the table and it is only used for reading the records from it whereas dataadapter, used to maintain connection, is always used with dataset as dataset is of disconnected-architecture in nature. 1 User has rated as useful. Login to rate this answer. Madhukar Singh Answered On : Aug 11th, 2006 DateReader is an forward only and read only cursor type if you are accessing data through DataRead it shows the data on the web form/control but you can not perform the paging feature on that record(because it's forward only type). DataAdapter is not only connect with the Databse(through Command object) it provide four types of command (InsertCommand, UpdateCommand, DeleteCommand, SelectCommand), It supports to the disconnected Architecture of .NET show we can populate the records to the DataSet. where as Dataadapter is best fit to work on data. 2 Users have rated as useful.

Datareader is connected mode. and read and forward-only Data. 1. Retrieving Data Using a DataReader. Retrieving data using a DataReader involves creating an instance of the Command object and then creating a DataReader by calling Command.ExecuteReader to retrieve rows from a data source. The following example illustrates using a DataReader where reader represents a valid DataReader and command represents a valid Command object. reader = command.ExecuteReader(); The following code example iterates through a DataReader object, and returns two columns from each row. static void HasRows(SqlConnection connection) { using (connection) { SqlCommand command = new SqlCommand( "SELECT CategoryID, CategoryName FROM Categories;", connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { Console.WriteLine("{0}\t{1}", reader.GetInt32(0), reader.GetString(1)); } } else { Console.WriteLine("No rows found.

"); } reader.Close(); } } You should always call the Close method when you have finished using the DataReader object.