![]() |
Reading Data Using a Data Reader |
|
As reviewed in the previous lesson, a data adapter allows you to retrieve records from a database and make them available to your Windows Forms Application. To make reading data of a database a little faster, the .NET Framework provides a class used to read data from a database. For s SQL Server database, this class is called SqlDataReader
The .NET Framework provided XML-supported classes used to read from, and write to, an XML file going forth but without back. To support a unidirectional approach to reading data from a SQL Server database, the .NET Framework provides the SqlDataReader. This class reads data in a top-down direction without referring back to a record it passed already:
In other words, the SqlDataReader reads the first record, moves down, reads the second record, moves down, and so on, until it gets to the last record. Once it has passed a record, it doesn't and cannot refer back to it. To create a data reader, you can declare a pointer to SqlDataReader. This class doesn't have a constructor. This means that, to use it, you must (directly) specify where it would read its data. To provide data to the reader, the SqlCommand class is equipped with the ExecuteReader() method that is overloaded with two versions. The simplest version of this method uses the following syntax: public: SqlDataReader* ExecuteReader(); Based on this, before using a data reader, you should first create a command that would specify how data would be acquired. Once the data is reader, you can pass it to the data reader by assigning the result of a call to a SqlCommand::ExecuteReader() method to a SqlDataReader object.
Once data is supplied to the reader, you can access it, one record at a time, from top to bottom. To access data that the reader acquired, you can call its Read() method whose syntax is: public: virtual bool Read(); As you can see, the Read() method simply reads a record and moves on. When reading the records of a table, as mentioned already many times, the data reader reads one record at a time and moves to the next. Before moving to the next record, you can access the values stored in the current record. To help with this, the columns of the table being read are stored in a collection and each column can be referred to with a numeric index. The first column has an index of 1. The second column has an index of 2, and so on. To retrieve the actual data stored in a column, you may need to know the type of information that column is holding so you can read it accurately. Depending on the data type that a column was created with, you can access it as follows:
When using one of the Get... or GetSql... methods, the compiler doesn't perform any conversion. This means that, before sending the data, you have two responsibilities. First you must convert the value read to the appropriate (and probably exact) format. For example, if you read a natural number from a column created with the tinyint data type, even though C++ allows a short to be implicitly converted to an int, the compiler you use for your application would not perform or assume the conversion: the value of a column created with tinyint must be read with GetByte() or GetSqlByte() and trying to use GetInt32() or GetSqlInt32() would throw an error. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
private: System::Void btnOpen_Click(System::Object * sender, System::EventArgs * e)
{
String *strReceiptNumber = this->txtReceiptNumber->Text;
if( strReceiptNumber->Equals(S"") )
{
MessageBox::Show(S"You must provide a receipt number to look for the repair");
return;
}
String *strFindRepair = String::Concat(S"SELECT * FROM RepairOrders WHERE RepairOrderID = '",
strReceiptNumber, S"'");
System::Data::SqlClient::SqlConnection *conDatabase = new
System::Data::SqlClient::SqlConnection(S"Data Source=(local);Database='CPAS';Integrated Security=yes");
System::Data::SqlClient::SqlCommand *cmdDatabase = new
System::Data::SqlClient::SqlCommand(strFindRepair, conDatabase);
conDatabase->Open();
System::Data::SqlClient::SqlDataReader *rdrRepairOrder;
rdrRepairOrder = cmdDatabase->ExecuteReader();
while(rdrRepairOrder->Read())
{
this->dtpOrderDate->Value = rdrRepairOrder->GetDateTime(1);
this->dtpOrderTime->Value = rdrRepairOrder->GetDateTime(2);
this->txtCustomerName->Text = rdrRepairOrder->GetString(3);
this->txtAddress->Text = rdrRepairOrder->GetString(4);
this->txtCity->Text = rdrRepairOrder->GetString(5);
this->txtState->Text = rdrRepairOrder->GetString(6);
this->txtZIPCode->Text = rdrRepairOrder->GetString(7);
this->txtMake->Text = rdrRepairOrder->GetString(8);
this->txtModel->Text = rdrRepairOrder->GetString(9);
this->txtCarYear->Text = rdrRepairOrder->GetSqlInt16(10).ToString();
this->txtProblem->Text = rdrRepairOrder->GetString(11);
this->txtPartName1->Text = rdrRepairOrder->GetString(12);
this->txtUnitPrice1->Text = rdrRepairOrder->GetSqlDecimal(13).ToString();
this->txtQuantity1->Text = rdrRepairOrder->GetSqlByte(14).ToString();
this->txtSubTotal1->Text = rdrRepairOrder->GetSqlDecimal(15).ToString();
this->txtPartName2->Text = rdrRepairOrder->GetString(16);
this->txtUnitPrice2->Text = rdrRepairOrder->GetSqlDecimal(17).ToString();
this->txtQuantity2->Text = rdrRepairOrder->GetSqlByte(18).ToString();
this->txtSubTotal2->Text = rdrRepairOrder->GetSqlDecimal(19).ToString();
this->txtPartName3->Text = rdrRepairOrder->GetString(20);
this->txtUnitPrice3->Text = rdrRepairOrder->GetSqlDecimal(21).ToString();
this->txtQuantity3->Text = rdrRepairOrder->GetSqlByte(22).ToString();
this->txtSubTotal3->Text = rdrRepairOrder->GetSqlDecimal(23).ToString();
this->txtPartName4->Text = rdrRepairOrder->GetString(24);
this->txtUnitPrice4->Text = rdrRepairOrder->GetSqlDecimal(25).ToString();
this->txtQuantity4->Text = rdrRepairOrder->GetSqlByte(26).ToString();
this->txtSubTotal4->Text = rdrRepairOrder->GetSqlDecimal(27).ToString();
this->txtPartName5->Text = rdrRepairOrder->GetString(28);
this->txtUnitPrice5->Text = rdrRepairOrder->GetSqlDecimal(29).ToString();
this->txtQuantity5->Text = rdrRepairOrder->GetSqlByte(30).ToString();
this->txtSubTotal5->Text = rdrRepairOrder->GetSqlDecimal(31).ToString();
this->txtJobPerformed1->Text = rdrRepairOrder->GetString(32);
this->txtJobPrice1->Text = rdrRepairOrder->GetSqlDecimal(33).ToString();
this->txtJobPerformed2->Text = rdrRepairOrder->GetString(34);
this->txtJobPrice2->Text = rdrRepairOrder->GetSqlDecimal(35).ToString();
this->txtJobPerformed3->Text = rdrRepairOrder->GetString(36);
this->txtJobPrice3->Text = rdrRepairOrder->GetSqlDecimal(37).ToString();
this->txtJobPerformed4->Text = rdrRepairOrder->GetString(38);
this->txtJobPrice4->Text = rdrRepairOrder->GetSqlDecimal(39).ToString();
this->txtJobPerformed5->Text = rdrRepairOrder->GetString(40);
this->txtJobPrice5->Text = rdrRepairOrder->GetSqlDecimal(41).ToString();
this->txtTotalParts->Text = rdrRepairOrder->GetSqlDecimal(42).ToString();
this->txtTotalLabor->Text = rdrRepairOrder->GetSqlDecimal(43).ToString();
this->txtTaxRate->Text = rdrRepairOrder->GetSqlDecimal(44).ToString();
this->txtTaxAmount->Text = rdrRepairOrder->GetSqlDecimal(45).ToString();
this->txtTotalOrder->Text = rdrRepairOrder->GetSqlDecimal(46).ToString();
this->txtRecommendations->Text = rdrRepairOrder->GetString(47);
}
rdrRepairOrder->Close();
conDatabase->Close();
}
|
|
|
||
| Previous | Copyright © 2005 FunctionX, Inc. | Next |
|
|
||