Databases Reference
In-Depth Information
The
SqlDataReader
object has a
Read
method that gets each row in turn and a
GetValue
method that gets the value of a column in the row. The particular column whose value it
retrieves is given by the integer parameter indicating the index of the column. Note that
GetValue
uses a zero-based index, so the first column is column 0, the second column is
column 1, and so on. Since the query asked for two columns, FirstName and LastName,
these are the columns numbered 0 and 1 in this query result.
Executing Statements
The
ExecuteNonQuery
method The
ExecuteNonQuery
method of the command executes SQL
statements instead of queries. Let's try it.
Try It Out: Using the ExecuteNonQuery Method
To use the
ExecuteNonQuery
method, follow these steps:
1.
Add a new C# Console Application project named CommandNonQuery to your
Chapter11 solution. Rename
Program.cs
to
CommandNonQuery.cs
.
2.
Replace the code in
CommandNonQuery.cs
with the code in Listing 11-4.
Listing 11-4.
CommandNonQuery.cs
using System;
using System.Data;
using System.Data.SqlClient;
namespace Chapter11
{
class CommandNonQuery
{
static void Main()
{
// create connection
SqlConnection conn = new SqlConnection(@"
server = .\sqlexpress;
integrated security = true;
database = northwind
");
Search WWH ::
Custom Search