Presentation is loading. Please wait.

Presentation is loading. Please wait.

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 The SqlCommand Object ADO.NET - Lesson 03  Training time: 15 minutes  Author:

Similar presentations


Presentation on theme: "© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 The SqlCommand Object ADO.NET - Lesson 03  Training time: 15 minutes  Author:"— Presentation transcript:

1 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 The SqlCommand Object ADO.NET - Lesson 03  Training time: 15 minutes  Author: CuongNV3

2 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Objectives  Know what a command object is.  Learn how to use the ExecuteReader method to query data.  Learn how to use the ExecuteNonQuery method to insert and delete data.  Learn how to use the ExecuteScalar method to return a single value.

3 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Introduction  A SqlCommand object allows you to specify what type of interaction you want to perform with a database.  The SqlCommand object can be used to support disconnected data management scenarios, but in this lesson we will only use the SqlCommand object alone.  A later lesson on the SqlDataAdapter will explain how to implement an application that uses disconnected data.

4 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Introduction (2)

5 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Creating a SqlCommand Object NameDescription SqlCommand Initializes a new instance of the SqlCommand class. SqlCommand(String) Initializes a new instance of the SqlCommand class with the text of the query. SqlCommand(String, SqlConnection) Initializes a new instance of the SqlCommand class with the text of the query and a SqlConnection.SqlConnection SqlCommand(String, SqlConnection, SqlTransaction) Initializes a new instance of the SqlCommand class with the text of the query, a SqlConnection, and the SqlTransaction.SqlConnection SqlTransaction Constructors Example: SqlCommand command = new SqlCommand(selectFromCustomers, connection);

6 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Querying Data  When using a SQL select command, you retrieve a data set for viewing. To accomplish this with a SqlCommand object, you would use the ExecuteReader method, which returns a SqlDataReader object.  The example below shows how to use the SqlCommand object to obtain a SqlDataReader object:

7 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Inserting Data  To insert data into a database, use the ExecuteNonQuery method of the SqlCommand object.  The following code shows how to insert data into a database table:

8 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Updating Data  The ExecuteNonQuery method is also used for updating data.  The following code shows how to update data:

9 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Deleting Data  You can also delete data using the ExecuteNonQuery method.  The following example shows how to delete a record from a database:

10 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Getting Single values  Sometimes all you need from a database is a single value, which could be a count, sum, average, or other aggregated value from a data set. Performing an ExecuteReader and calculating the result in your code is not the most efficient way to do this. The best choice is to let the database perform the work and return just the single value you need.  The following example shows how to do this with the ExecuteScalar method:

11 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Using SqlCommand  Querying Data  Inserting Data  Updating Data  Deleting Data  Getting Single values Demo

12 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Summary  A SqlCommand object allows you to query and send commands to a database.  It has methods that are specialized for different commands: The ExecuteReader method returns a SqlDataReader object for viewing the results of a select query. For insert, update, and delete SQL commands, you use the ExecuteNonQuery method. If you only need a single aggregate value from a query, the ExecuteScalar is the best choice.

13 © FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Q&A Thanks!


Download ppt "© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 The SqlCommand Object ADO.NET - Lesson 03  Training time: 15 minutes  Author:"

Similar presentations


Ads by Google