Đang chuẩn bị liên kết để tải về tài liệu:
Retrieving New Identity Column Values
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Retrieving New Identity Column Values The ProductID column of the Products table is an identity column. In this section, you'll see how to insert a new row into to Products table | Retrieving New Identity Column Values The ProductID column of the Products table is an identity column. In this section you ll see how to insert a new row into to Products table and retrieve the new value generated by the database for the ProductID identity column. Note You ll find a complete program named UsingIdentityColumn.es in the ch11 directory that illustrates the use of the methods shown in this section. This program listing is omitted from this book for brevity. In the examples assume you have a DataTable named productsDataTable that is populated with the rows retrieved by the following SELECT statement SELECT ProductID ProductName UnitPrice FROM Products ORDER BY ProductID The following example sets the PrimaryKey property of productsDataTable productsDataTable.PrimaryKey new DataColumn productsDataTable.Columns ProductID The next example sets the AllowDBNull AutoIncrement AutoIncrementSeed AutoIncrementStep ReadOnly and Unique properties for the ProductID DataColumn of productsDataTable DataColumn productIDDataColumn productsDataTable.Columns ProductID productIDDataColumn.AllowDBNull false productIDDataColumn.AutoIncrement true productIDDataColumn.AutoIncrementSeed -1 productIDDataColumn.AutoIncrementStep -1 productIDDataColumn.ReadOnly true productIDDataColumn.Unique true Because of these settings when you add a new DataRow to productsDataTable the ProductID DataColumn of your new DataRow will initially have the value -1. As in the earlier section Modifying Rows in a DataTable you need to set your DataAdapter object s InsertCommand UpdateCommand and DeleteCommand properties with appropriate Command objects. The CommandText property of the Command object used in the UpdateCommand property is as follows myUpdateCommand.CommandText UPDATE Products SET ProductName @NewProductName UnitPrice @NewUnitPrice WHERE ProductID @OldProductID AND ProductName @OldProductName AND UnitPrice @OldUnitPrice The CommandText property of the Command object used in the .