Tải bản đầy đủ (.pdf) (10 trang)

Tài liệu Adding, Updating, and Deleting Related Rows In this section, you''''ll learn how to make changes in docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (37.63 KB, 10 trang )


Adding, Updating, and Deleting Related Rows
In this section, you'll learn how to make changes in DataTable objects that store rows
from the Customers and Orders tables. These tables are related through the CustomerID
foreign key. As you'll see, you must push changes to the underlying database tables in a
specific order. If you don't, your program will throw an exception.

Note You'll find all the code examples shown in this section in the
ModifyingRelatedData.cs program.
Setting Up the DataAdapter Objects
You'll need two DataAdapter objects:

One to work with the Customers table, which will be named customersDA.

One to work with the Orders table, which will be named ordersDA.
Let's take a look at setting up these two DataAdapter objects.
Setting Up the customersDA DataAdapter
The following code creates and sets up a DataAdapter named customersDA that contains
the necessary SELECT, INSERT, UPDATE, and DELETE statements to access the
Customers table:
SqlDataAdapter customersDA = new SqlDataAdapter();

// create a SqlCommand object to hold the SELECT
SqlCommand customersSelectCommand = mySqlConnection.CreateCommand();
customersSelectCommand.CommandText =
"SELECT CustomerID, CompanyName " +
"FROM Customers";

// create a SqlCommand object to hold the INSERT
SqlCommand customersInsertCommand = mySqlConnection.CreateCommand();
customersInsertCommand.CommandText =


"INSERT INTO Customers (" +
" CustomerID, CompanyName " +
") VALUES (" +
" @CustomerID, @CompanyName" +
")";
customersInsertCommand.Parameters.Add("@CustomerID", SqlDbType.NChar,
5, "CustomerID");
customersInsertCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar,
40, "CompanyName");

// create a SqlCommand object to hold the UPDATE
SqlCommand customersUpdateCommand = mySqlConnection.CreateCommand();
customersUpdateCommand.CommandText =
"UPDATE Customers " +
"SET " +
" CompanyName = @NewCompanyName " +
"WHERE CustomerID = @OldCustomerID " +
"AND CompanyName = @OldCompanyName";
customersUpdateCommand.Parameters.Add("@NewCompanyName",
SqlDbType.NVarChar, 40, "CompanyName");
customersUpdateCommand.Parameters.Add("@OldCustomerID",
SqlDbType.NChar, 5, "CustomerID");
customersUpdateCommand.Parameters.Add("@OldCompanyName",
SqlDbType.NVarChar, 40, "CompanyName");
customersUpdateCommand.Parameters["@OldCustomerID"].SourceVersion =
DataRowVersion.Original;
customersUpdateCommand.Parameters["@OldCompanyName"].SourceVersion =
DataRowVersion.Original;

// create a SqlCommand object to hold the DELETE

SqlCommand customersDeleteCommand = mySqlConnection.CreateCommand();
customersDeleteCommand.CommandText =
"DELETE FROM Customers " +
"WHERE CustomerID = @OldCustomerID " +
"AND CompanyName = @OldCompanyName";
customersDeleteCommand.Parameters.Add("@OldCustomerID",
SqlDbType.NChar, 5, "CustomerID");
customersDeleteCommand.Parameters.Add("@OldCompanyName",
SqlDbType.NVarChar, 40, "CompanyName");
customersDeleteCommand.Parameters["@OldCustomerID"].SourceVersion =
DataRowVersion.Original;
customersDeleteCommand.Parameters["@OldCompanyName"].SourceVersion =
DataRowVersion.Original;

// set the customersDA properties
// to the SqlCommand objects previously created
customersDA.SelectCommand = customersSelectCommand;
customersDA.InsertCommand = customersInsertCommand;
customersDA.UpdateCommand = customersUpdateCommand;
customersDA.DeleteCommand = customersDeleteCommand;
Notice that the UPDATE statement modifies only the CompanyName column value; it
doesn't modify the CustomerID primary key column value. You'll learn about the issues
involved with updating a primary key column value later in the section "Issues When
Updating the Primary Key of a Parent Row."
The ModifyingRelatedData.cs program contains a method named SetupCustomersDA()
that performs the previous code.
Setting Up the ordersDA DataAdapter
The following code creates and sets up a DataAdapter object named ordersDA that
contains the necessary SELECT, INSERT, UPDATE, and DELETE statements to access
the Orders table; notice that the ordersInsertCommand contains both an INSERT

statement and a SELECT statement to retrieve the new OrderID column, which is an
identity column that has a value automatically generated by the database:
SqlDataAdapter ordersDA = new SqlDataAdapter();

// create a SqlCommand object to hold the SELECT
SqlCommand ordersSelectCommand = mySqlConnection.CreateCommand();
ordersSelectCommand.CommandText =
"SELECT OrderID, CustomerID, ShipCountry " +
"FROM Orders";

// create a SqlCommand object to hold the INSERT
SqlCommand ordersInsertCommand = mySqlConnection.CreateCommand();
ordersInsertCommand.CommandText =
"INSERT INTO Orders (" +
" CustomerID, ShipCountry " +
") VALUES (" +
" @CustomerID, @ShipCountry" +
");" +
"SELECT @OrderID = SCOPE_IDENTITY();";
ordersInsertCommand.Parameters.Add("@CustomerID", SqlDbType.NChar,
5, "CustomerID");
ordersInsertCommand.Parameters.Add("@ShipCountry", SqlDbType.NVarChar,
15, "ShipCountry");
ordersInsertCommand.Parameters.Add("@OrderID", SqlDbType.Int,
0, "OrderID");
ordersInsertCommand.Parameters["@OrderID"].Direction =
ParameterDirection.Output;

// create a SqlCommand object to hold the UPDATE
SqlCommand ordersUpdateCommand = mySqlConnection.CreateCommand();

ordersUpdateCommand.CommandText =
"UPDATE Orders " +
"SET " +
" ShipCountry = @NewShipCountry " +
"WHERE OrderID = @OldOrderID " +
"AND CustomerID = @OldCustomerID " +
"AND ShipCountry = @OldShipCountry";
ordersUpdateCommand.Parameters.Add("@NewShipCountry",
SqlDbType.NVarChar, 15, "ShipCountry");
ordersUpdateCommand.Parameters.Add("@OldOrderID",
SqlDbType.Int, 0, "OrderID");
ordersUpdateCommand.Parameters.Add("@OldCustomerID",
SqlDbType.NChar, 5, "CustomerID");
ordersUpdateCommand.Parameters.Add("@OldShipCountry",
SqlDbType.NVarChar, 15, "ShipCountry");
ordersUpdateCommand.Parameters["@OldOrderID"].SourceVersion =
DataRowVersion.Original;
ordersUpdateCommand.Parameters["@OldCustomerID"].SourceVersion =
DataRowVersion.Original;
ordersUpdateCommand.Parameters["@OldShipCountry"].SourceVersion =
DataRowVersion.Original;

// create a SqlCommand object to hold the DELETE
SqlCommand ordersDeleteCommand = mySqlConnection.CreateCommand();
ordersDeleteCommand.CommandText =
"DELETE FROM Orders " +
"WHERE OrderID = @OldOrderID " +
"AND CustomerID = @OldCustomerID " +
"AND ShipCountry = @OldShipCountry";
ordersDeleteCommand.Parameters.Add("@OldOrderID", SqlDbType.Int,

0, "OrderID");
ordersDeleteCommand.Parameters.Add("@OldCustomerID",
SqlDbType.NChar, 5, "CustomerID");
ordersDeleteCommand.Parameters.Add("@OldShipCountry",
SqlDbType.NVarChar, 15, "ShipCountry");
ordersDeleteCommand.Parameters["@OldOrderID"].SourceVersion =
DataRowVersion.Original;
ordersDeleteCommand.Parameters["@OldCustomerID"].SourceVersion =
DataRowVersion.Original;
ordersDeleteCommand.Parameters["@OldShipCountry"].SourceVersion =
DataRowVersion.Original;

// set the ordersDA properties
// to the SqlCommand objects previously created
ordersDA.SelectCommand = ordersSelectCommand;
ordersDA.InsertCommand = ordersInsertCommand;
ordersDA.UpdateCommand = ordersUpdateCommand;
ordersDA.DeleteCommand = ordersDeleteCommand;
The ModifyingRelatedData.cs program contains a method named SetupOrdersDA() that
performs the previous code.
Creating and Populating a DataSet
Next, the following example creates and populates a DataSet named myDataSet with the
rows from the Customers and Orders tables using customersDA and ordersDA:
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
customersDA.Fill(myDataSet, "Customers");
ordersDA.Fill(myDataSet, "Orders");
mySqlConnection.Close();
DataTable customersDT = myDataSet.Tables["Customers"];
DataTable ordersDT = myDataSet.Tables["Orders"];

Notice that the DataTable objects are named customersDT and ordersDT.
The following examples set the PrimaryKey properties of customersDT and ordersDT:
customersDT.PrimaryKey =
new DataColumn[]
{
customersDT.Columns["CustomerID"]
};

ordersDT.PrimaryKey =
new DataColumn[]
{
ordersDT.Columns["OrderID"]
};
The following example sets up the OrderID DataColumn of ordersDT as an identity:

×