![]() |
Data Entry Using a Data Row |
With ADO.NET, you can perform data entry using a DataGrid or a Windows control. Another option you can use is through the data set system. Because a database is first of all a list of items with in a two-dimensional arrangement, the Microsoft .NET Framework provides the DataTable class. A DataTable object can be used to create any two-dimensional array of items as done in a flat or file-based database (a database that is not based on a database system such as SQL Server, Paradox, or MS Access but with its own ability to create and save records as files, as done in file processing). To arrange its list of items into records, the DataTable class has a Rows property. A row is simply a record. A row is of type DataRow. When you create a new record in a DataTable object, you in fact add a new DataRow object. Because a DataSet object is made of DataTable objects, you can retrieve a table from a DataSet, add a new row to it, and then pass the table back to the DataSet to update. This is another way to perform data entry and can be valuable in a client/server multiple workstations environment.
|
|
IF EXISTS (SELECT * FROM master..sysdatabases WHERE name = N'DaniloPizza') DROP DATABASE DaniloPizza GO CREATE DATABASE DaniloPizza; GO |
USE DaniloPizza
GO
IF EXISTS (SELECT name
FROM sysobjects
WHERE name = N'CustomerOrder'
AND type = 'U')
DROP TABLE CustomerOrder
GO
create table CustomerOrder (
CustomerOrderID int IDENTITY(1, 1) PRIMARY KEY NOT NULL,
PizzaSize smallint Default 2 NOT NULL,
PriceSmall Decimal(4,2) Default 8.95,
PriceMedium Decimal(4,2) Default 10.75,
PriceLarge Decimal(4,2) Default 12.55,
Pepperoni Bit,
Sausage Bit,
ExtraCheese Bit,
Onions Bit,
Olives Bit
)
GO
INSERT INTO CustomerOrder (PizzaSize, PriceSmall, PriceMedium, PriceLarge, Pepperoni, Sausage, ExtraCheese, Onions, Olives)
VALUES (2, 8.95, 9.55, 12.55, 1, 1, 0, 0, 0);
INSERT INTO CustomerOrder (PizzaSize, PriceSmall, PriceMedium, PriceLarge, Pepperoni, Sausage, ExtraCheese, Onions, Olives)
VALUES (1, 6.75, 8.95, 12.55, 1, 0, 1, 0, 0);
INSERT INTO CustomerOrder (PizzaSize, PriceSmall, PriceMedium, PriceLarge, Pepperoni, Sausage, ExtraCheese, Onions, Olives)
VALUES (3, 8.95, 9.55, 11.25, 0, 1, 0, 0, 1);
INSERT INTO CustomerOrder (PizzaSize, PriceSmall, PriceMedium, PriceLarge, Pepperoni, Sausage, ExtraCheese, Onions, Olives)
VALUES (2, 8.95, 8.55, 12.55, 0, 0, 1, 1, 0);
GO
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{
this->sqlDataAdapter1->Fill(this->dsDaniloPizza1);
}
|
private: System::Void btnSubmit_Click(System::Object * sender, System::EventArgs * e)
{
Int16 pizzaSize;
Double priceSmall, priceMedium, priceLarge;
Boolean pepperoni, sausage, extraCheese, onions, olives;
// Find out what radio button was clicked
if( this->rdoSmall->Checked == true )
pizzaSize = 1;
else if( this->rdoMedium->Checked == true )
pizzaSize = 2;
else if( this->rdoLarge->Checked == true )
pizzaSize = 3;
// Retrieve the values of pizza prices
priceSmall = this->txtPriceSmall->Text->ToDouble(0);
priceMedium = this->txtPriceMedium->Text->ToDouble(0);
priceLarge = this->txtPriceLarge->Text->ToDouble(0);
// Retrieve the state of each check box
pepperoni = this->chkPepperoni->Checked;
sausage = this->chkSausage->Checked;
extraCheese = this->chkExtraCheese->Checked;
onions = this->chkOnions->Checked;
olives = this->chkOlives->Checked;
// Now that a new record has been created, prepare to save it
// First create a DataTable variable of the table used in the current DataSet
DataTable *tblCustOrder = this->dsDaniloPizza1->Tables->Item[S"CustomerOrder"];
// Create a new row from the current table
DataRow *rowRecord = tblCustOrder->NewRow();
// Fill the new row with values entered on the form
rowRecord->Item[S"PizzaSize"] = pizzaSize.ToString();
rowRecord->Item[S"PriceSmall"] = priceSmall.ToString();
rowRecord->Item[S"PriceMedium"] = priceMedium.ToString();
rowRecord->Item[S"PriceLarge"] = priceLarge.ToString();
rowRecord->Item[S"Pepperoni"] = pepperoni.ToString();
rowRecord->Item[S"Sausage"] = sausage.ToString();
rowRecord->Item[S"ExtraCheese"] = extraCheese.ToString();
rowRecord->Item[S"Onions"] = onions.ToString();
rowRecord->Item[S"Olives"] = olives.ToString();
// Pass the new record to the table
tblCustOrder->Rows->Add(rowRecord);
// Update the DataSet with information in the new record
this->sqlDataAdapter1->Update(this->dsDaniloPizza1);
// In case the user wants to create a new record, reset the controls
this->rdoMedium->Checked = true;
this->txtPriceSmall->Text = S"8.95";
this->txtPriceMedium->Text = S"10.75";
this->txtPriceLarge->Text = S"12.55";
this->chkPepperoni->Checked = false;
this->chkSausage->Checked = false;
this->chkExtraCheese->Checked = false;
this->chkOnions->Checked = false;
this->chkOnions->Checked = false;
}
private: System::Void btnClose_Click(System::Object * sender, System::EventArgs * e)
{
Close();
}
|
SELECT * FROM CustomerOrder GO; |
|
|
||
| Home | Copyright © 2004-2010 FunctionX, Inc. | |
|
|
||