ADO.NET Example Applications: |
|
The SqlDataReader class of the .NET Framework provides a technique of moving in the records of a set in a forward-only one-way many. For example, imagine you get to a record to view it. Once you finish with such a record, you move to the next record. You cannot visit the previous record. This can provide a fast means of accessing record since there is little processing involved. In this example, we will create a database used to process records for the customers of a car repair shop. When it's time to open a previously saved order, we will use a SqlDataReader object to demonstrate how it can be used. |
Practical Learning: Creating the Application |
Column Name | Data Type | Length | Allow Nulls | Other Properties |
RepairOrderID | int | Primary Key Identity: Yes |
||
OrderDate | datetime | Unchecked | ||
OrderTime | datetime | 20 | Unchecked | |
CustomerName | varchar | Unchecked | ||
Address | varchar | |||
City | varchar | |||
State | char | 2 | ||
ZIPCode | varchar | 10 | ||
Make | varchar | 20 | Unchecked | |
Model | varchar | 32 | Unchecked | |
CarYear | smallint | 5 | Unchecked | |
ProblemDescription | text | Unchecked | ||
PartName1 | varchar | |||
UnitPrice1 | decimal | 10 | Default Value: 0.00 Scale: 2 |
|
Quantity1 | tinyint | 3 | Default Value: 0 | |
SubTotal1 | decimal | 10 | Default Value: 0.00 Scale: 2 |
|
PartName2 | varchar | |||
UnitPrice2 | decimal | 10 | Default Value: 0.00 Scale: 2 |
|
Quantity2 | tinyint | 3 | Default Value: 0 | |
SubTotal2 | decimal | Default Value: 0.00 Scale: 2 |
||
PartName3 | varchar | |||
UnitPrice3 | decimal | Scale: 2 | ||
Quantity3 | tinyint | Default Value: 0 | ||
SubTotal3 | decimal | Default Value: 0.00 Scale: 2 |
||
PartName4 | varchar | |||
UnitPrice4 | decimal | Default Value: 0.00 Scale: 2 |
||
Quantity4 | tinyint | Default Value: 0 | ||
SubTotal4 | decimal | Default Value: 0.00 Scale: 2 |
||
PartName5 | varchar | |||
UnitPrice5 | decimal | Default Value: 0.00 Scale: 2 |
||
Quantity5 | tinyint | Default Value: 0 | ||
SubTotal5 | decimal | Default Value: 0.00 Scale: 2 |
||
JobPerformed1 | varchar | 80 | ||
JobPrice1 | decimal | Default Value: 0.00 Scale: 2 |
||
JobPerformed2 | varchar | 80 | ||
JobPrice2 | decimal | Default Value: 0.00 Scale: 2 |
||
JobPerformed3 | varchar | 80 | ||
JobPrice3 | decimal | Default Value: 0.00 Scale: 2 |
||
JobPerformed4 | varchar | 80 | ||
JobPrice4 | decimal | Default Value: 0.00 Scale: 2 |
||
JobPerformed5 | varchar | 80 | ||
JobPrice5 | decimal | Default Value: 0.00 Scale: 2 |
||
TotalParts | decimal | Unchecked | Default Value: 0.00 Scale: 2 |
|
TotalLabor | decimal | Unchecked | Default Value: 0.00 Scale: 2 |
|
TaxRate | decimal | Unchecked | Default Value: 7.75 Scale: 2 |
|
TaxAmount | decimal | Unchecked | Default Value: 0.00 Scale: 2 |
|
OrderTotal | decimal | Unchecked | Default Value: 0.00 Scale: 2 |
|
Recommendations | text |
|
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btnReset.Click dtpOrderDate.Value = DateTime.Today dtpOrderTime.Value = DateTime.Now txtCustomerName.Text = "" txtAddress.Text = "" txtCity.Text = "" txtState.Text = "" txtZIPCode.Text = "" txtMake.Text = "" txtModel.Text = "" txtCarYear.Text = "" txtProblem.Text = "" txtPartName1.Text = "" txtUnitPrice1.Text = "0.00" txtQuantity1.Text = "0" txtSubTotal1.Text = "0.00" txtPartName2.Text = "" txtUnitPrice2.Text = "0.00" txtQuantity2.Text = "0" txtSubTotal2.Text = "0.00" txtPartName3.Text = "" txtUnitPrice3.Text = "0.00" txtQuantity3.Text = "0" txtSubTotal3.Text = "0.00" txtPartName4.Text = "" txtUnitPrice4.Text = "0.00" txtQuantity4.Text = "0" txtSubTotal4.Text = "0.00" txtPartName5.Text = "" txtUnitPrice5.Text = "0.00" txtQuantity5.Text = "0" txtSubTotal5.Text = "0.00" txtJobPerformed1.Text = "" txtJobPrice1.Text = "0.00" txtJobPerformed2.Text = "" txtJobPrice2.Text = "0.00" txtJobPerformed3.Text = "" txtJobPrice3.Text = "0.00" txtJobPerformed4.Text = "" txtJobPrice4.Text = "0.00" txtJobPerformed5.Text = "" txtJobPrice5.Text = "0.00" txtTotalParts.Text = "0.00" txtTotalLabor.Text = "0.00" txtTaxRate.Text = "7.75" txtTaxAmount.Text = "0.00" txtTotalOrder.Text = "0.00" txtRecommendations.Text = "" txtCustomerName.Focus() End Sub |
Private Sub CalculateOrder() Dim part1UnitPrice As Decimal Dim part1SubTotal As Decimal Dim part2UnitPrice As Decimal Dim part2SubTotal As Decimal Dim part3UnitPrice As Decimal Dim part3SubTotal As Decimal Dim part4UnitPrice As Decimal Dim part4SubTotal As Decimal Dim part5UnitPrice As Decimal Dim part5SubTotal As Decimal Dim totalParts As Decimal Dim part1Quantity As Integer = 0 Dim part2Quantity As Integer = 0 Dim part3Quantity As Integer = 0 Dim part4Quantity As Integer = 0 Dim part5Quantity As Integer = 0 Dim job1Price As Decimal = 0.0 Dim job2Price As Decimal = 0.0 Dim job3Price As Decimal = 0.0 Dim job4Price As Decimal = 0.0 Dim job5Price As Decimal = 0.0 Dim totalLabor As Decimal Dim taxRate As Decimal Dim taxAmount As Decimal Dim totalOrder As Decimal ' Don't charge a part unless it is clearly identified If txtPartName1.Text = "" Then txtUnitPrice1.Text = "0.00" txtQuantity1.Text = "0" txtSubTotal1.Text = "0.00" part1UnitPrice = 0.0 Else Try part1UnitPrice = CDbl(txtUnitPrice1.Text) Catch ex As FormatException msgbox("Invalid Unit Price") txtUnitPrice1.Text = "0.00" txtUnitPrice1.Focus() End Try Try part1Quantity = CInt(txtQuantity1.Text) Catch ex As FormatException msgbox("Invalid Quantity") txtQuantity1.Text = "0" txtQuantity1.Focus() End Try End If If txtPartName2.Text = "" Then txtUnitPrice2.Text = "0.00" txtQuantity2.Text = "0" txtSubTotal2.Text = "0.00" part2UnitPrice = 0.0 Else Try part2UnitPrice = CDbl(txtUnitPrice2.Text) Catch ex As FormatException msgbox("Invalid Unit Price") txtUnitPrice2.Text = "0.00" txtUnitPrice2.Focus() End Try Try part2Quantity = CInt(txtQuantity2.Text) Catch ex As FormatException msgbox("Invalid Quantity") txtQuantity2.Text = "0" txtQuantity2.Focus() End Try End If If txtPartName3.Text = "" Then txtUnitPrice3.Text = "0.00" txtQuantity3.Text = "0" txtSubTotal3.Text = "0.00" part3UnitPrice = 0.0 Else Try part3UnitPrice = CDbl(txtUnitPrice3.Text) Catch ex As FormatException msgbox("Invalid Unit Price") txtUnitPrice3.Text = "0.00" txtUnitPrice3.Focus() End Try Try part3Quantity = CInt(txtQuantity3.Text) Catch ex As FormatException msgbox("Invalid Quantity") txtQuantity3.Text = "0" txtQuantity3.Focus() End Try End If If txtPartName4.Text = "" Then txtUnitPrice4.Text = "0.00" txtQuantity4.Text = "0" txtSubTotal4.Text = "0.00" part4UnitPrice = 0.0 Else Try part4UnitPrice = CDbl(txtUnitPrice4.Text) Catch ex As FormatException msgbox("Invalid Unit Price") txtUnitPrice4.Text = "0.00" txtUnitPrice4.Focus() End Try Try part4Quantity = CInt(txtQuantity4.Text) Catch ex As FormatException msgbox("Invalid Quantity") txtQuantity4.Text = "0" txtQuantity4.Focus() End Try End If If txtPartName5.Text = "" Then txtUnitPrice5.Text = "0.00" txtQuantity5.Text = "0" txtSubTotal5.Text = "0.00" part5UnitPrice = 0.0 Else Try part5UnitPrice = CDbl(txtUnitPrice5.Text) Catch ex As FormatException msgbox("Invalid Unit Price") txtUnitPrice5.Text = "0.00" txtUnitPrice5.Focus() End Try Try part5Quantity = CInt(txtQuantity5.Text) Catch ex As FormatException msgbox("Invalid Quantity") txtQuantity5.Text = "0" txtQuantity5.Focus() End Try End If ' Don't bill the customer for a job that is not specified If txtJobPerformed1.Text = "" Then txtJobPrice1.Text = "0.00" job1Price = 0.0 Else Try job1Price = CDbl(txtJobPrice1.Text) Catch ex As FormatException msgbox("Invalid Job Price") txtJobPrice1.Text = "0.00" txtJobPrice1.Focus() End Try End If If txtJobPerformed2.Text = "" Then txtJobPrice2.Text = "0.00" job2Price = 0.0 Else Try job2Price = CDbl(txtJobPrice2.Text) Catch ex As FormatException MsgBox("Invalid Job Price") txtJobPrice2.Text = "0.00" txtJobPrice2.Focus() End Try End If If txtJobPerformed3.Text = "" Then txtJobPrice3.Text = "0.00" job3Price = 0.0 Else Try job3Price = CDbl(txtJobPrice3.Text) Catch ex As FormatException MsgBox("Invalid Job Price") txtJobPrice3.Text = "0.00" txtJobPrice3.Focus() End Try End If If txtJobPerformed4.Text = "" Then txtJobPrice4.Text = "0.00" job4Price = 0.0 Else Try job4Price = CDbl(txtJobPrice4.Text) Catch ex As FormatException MsgBox("Invalid Job Price") txtJobPrice4.Text = "0.00" txtJobPrice4.Focus() End Try End If If txtJobPerformed5.Text = "" Then txtJobPrice5.Text = "0.00" job5Price = 0.0 Else Try job5Price = CDbl(txtJobPrice5.Text) Catch ex As FormatException MsgBox("Invalid Job Price") txtJobPrice5.Text = "0.00" txtJobPrice5.Focus() End Try End If part1SubTotal = part1UnitPrice * part1Quantity part2SubTotal = part2UnitPrice * part2Quantity part3SubTotal = part3UnitPrice * part3Quantity part4SubTotal = part4UnitPrice * part4Quantity part5SubTotal = part5UnitPrice * part5Quantity txtSubTotal1.Text = part1SubTotal.ToString("F") txtSubTotal2.Text = part2SubTotal.ToString("F") txtSubTotal3.Text = part3SubTotal.ToString("F") txtSubTotal4.Text = part4SubTotal.ToString("F") txtSubTotal5.Text = part5SubTotal.ToString("F") totalParts = part1SubTotal + part2SubTotal + part3SubTotal + _ part4SubTotal + part5SubTotal totalLabor = job1Price + job2Price + job3Price + _ job4Price + job5Price Try taxRate = CDbl(txtTaxRate.Text) Catch ex As FormatException MsgBox("Invalid Tax Rate") txtTaxRate.Text = "7.75" txtTaxRate.Focus() End Try Dim totalPartsAndLabor As Decimal = totalParts + totalLabor taxAmount = totalPartsAndLabor * taxRate / 100 totalOrder = totalPartsAndLabor + taxAmount txtTotalParts.Text = totalParts.ToString("F") txtTotalLabor.Text = totalLabor.ToString("F") txtTaxAmount.Text = taxAmount.ToString("F") txtTotalOrder.Text = totalOrder.ToString("F") End Sub |
Private Sub txtQuantity1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles txtQuantity1.Leave CalculateOrder() End Sub |
Private Sub txtQuantity2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles txtQuantity2.Leave CalculateOrder() End Sub |
Private Sub txtQuantity3_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles txtQuantity3.Leave CalculateOrder() End Sub |
Private Sub txtQuantity4_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles txtQuantity4.Leave CalculateOrder() End Sub |
Private Sub txtQuantity5_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles txtQuantity5.Leave CalculateOrder() End Sub |
Private Sub txtJobPrice1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles txtJobPrice1.Leave CalculateOrder() End Sub |
Private Sub txtJobPrice2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles txtJobPrice2.Leave CalculateOrder() End Sub |
Private Sub txtJobPrice3_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles txtJobPrice3.Leave CalculateOrder() End Sub |
Private Sub txtJobPrice4_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles txtJobPrice4.Leave CalculateOrder() End Sub |
Private Sub txtJobPrice5_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles txtJobPrice5.Leave CalculateOrder() End Sub |
Private Sub btnSaveOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles btnSaveOrder.Click Dim strCustomerName As String = txtCustomerName.Text If strCustomerName = "" Then MsgBox("You must provide a name for the customer") Exit Sub End If Dim strOrderDate As String = dtpOrderDate.Value.ToString("d") Dim strOrderTime As String = dtpOrderTime.Value.ToString("t") Dim strNewRepairOrder As String = "INSERT INTO RepairOrders(OrderDate, " & _ "OrderTime, CustomerName, Address, " & _ "City, State, ZIPCode, Make, Model, " & _ "CarYear, ProblemDescription, PartName1, " & _ "UnitPrice1, Quantity1, SubTotal1, " & _ "PartName2, UnitPrice2, Quantity2, " & _ "SubTotal2, PartName3, UnitPrice3, " & _ "Quantity3, SubTotal3, PartName4, " & _ "UnitPrice4, Quantity4, SubTotal4, " & _ "PartName5, UnitPrice5, Quantity5, " & _ "SubTotal5, JobPerformed1, JobPrice1, " & _ "JobPerformed2, JobPrice2, JobPerformed3, " & _ "JobPrice3, JobPerformed4, JobPrice4, " & _ "JobPerformed5, JobPrice5, TotalParts, " & _ "TotalLabor, TaxRate, TaxAmount, " & _ "OrderTotal, Recommendations) " & _ "VALUES('" & strOrderDate & "', '" & strOrderTime & _ "', '" & strCustomerName & "', '" & txtAddress.Text & _ "', '" & txtCity.Text & "', '" & txtState.Text & _ "', '" & txtZIPCode.Text & "', '" & txtMake.Text & _ "', '" & txtModel.Text & "', '" & txtCarYear.Text & _ "', '" & txtProblem.Text & "', '" & txtPartName1.Text & _ "', '" & txtUnitPrice1.Text & "', '" & txtQuantity1.Text & _ "', '" & txtSubTotal1.Text & "', '" & txtPartName2.Text & _ "', '" & txtUnitPrice2.Text & "', '" & txtQuantity2.Text & _ "', '" & txtSubTotal2.Text & "', '" & txtPartName3.Text & _ "', '" & txtUnitPrice3.Text & "', '" & txtQuantity3.Text & _ "', '" & txtSubTotal3.Text & "', '" & txtPartName4.Text & _ "', '" & txtUnitPrice4.Text & "', '" & txtQuantity4.Text & _ "', '" & txtSubTotal4.Text & "', '" & txtPartName5.Text & _ "', '" & txtUnitPrice5.Text & "', '" & txtQuantity5.Text & _ "', '" & txtSubTotal5.Text & "', '" & txtJobPerformed1.Text & _ "', '" & txtJobPrice1.Text & "', '" & txtJobPerformed2.Text & _ "', '" & txtJobPrice2.Text & "', '" & txtJobPerformed3.Text & _ "', '" & txtJobPrice3.Text & "', '" & txtJobPerformed4.Text & _ "', '" & txtJobPrice4.Text & "', '" & txtJobPerformed5.Text & _ "', '" & txtJobPrice5.Text & "', '" & txtTotalParts.Text & _ "', '" & txtTotalLabor.Text & "', '" & txtTaxRate.Text & _ "', '" & txtTaxAmount.Text & "', '" & txtTotalOrder.Text & _ "', '" & txtRecommendations.Text & "');" Dim conDatabase As System.Data.SqlClient.SqlConnection = New _ System.Data.SqlClient.SqlConnection( _ "Data Source=(local);Database='CPAS';Integrated Security=yes") Dim cmdDatabase As System.Data.SqlClient.SqlCommand = New _ System.Data.SqlClient.SqlCommand(strNewRepairOrder, conDatabase) conDatabase.Open() cmdDatabase.ExecuteNonQuery() conDatabase.Close() End Sub |
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles btnClose.Click End End Sub |
Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click Dim strReceiptNumber As String = txtReceiptNumber.Text If strReceiptNumber = "" Then MsgBox("You must provide a receipt number to look for the repair") Exit Sub End If Dim strFindRepair As String = "SELECT * FROM RepairOrders WHERE RepairOrderID = '" & _ strReceiptNumber & "'" Dim conDatabase As System.Data.SqlClient.SqlConnection = New _ System.Data.SqlClient.SqlConnection("Data Source=(local);Database='CPAS';Integrated Security=yes") Dim cmdDatabase As System.Data.SqlClient.SqlCommand = New _ System.Data.SqlClient.SqlCommand(strFindRepair, conDatabase) conDatabase.Open() Dim rdrRepairOrder As System.Data.SqlClient.SqlDataReader rdrRepairOrder = cmdDatabase.ExecuteReader() While rdrRepairOrder.Read() dtpOrderDate.Value = rdrRepairOrder.GetDateTime(1) dtpOrderTime.Value = rdrRepairOrder.GetDateTime(2) txtCustomerName.Text = rdrRepairOrder.GetString(3) txtAddress.Text = rdrRepairOrder.GetString(4) txtCity.Text = rdrRepairOrder.GetString(5) txtState.Text = rdrRepairOrder.GetString(6) txtZIPCode.Text = rdrRepairOrder.GetString(7) txtMake.Text = rdrRepairOrder.GetString(8) txtModel.Text = rdrRepairOrder.GetString(9) txtCarYear.Text = rdrRepairOrder.GetSqlInt16(10).ToString() txtProblem.Text = rdrRepairOrder.GetString(11) txtPartName1.Text = rdrRepairOrder.GetString(12) txtUnitPrice1.Text = rdrRepairOrder.GetSqlDecimal(13).ToString() txtQuantity1.Text = rdrRepairOrder.GetSqlByte(14).ToString() txtSubTotal1.Text = rdrRepairOrder.GetSqlDecimal(15).ToString() txtPartName2.Text = rdrRepairOrder.GetString(16) txtUnitPrice2.Text = rdrRepairOrder.GetSqlDecimal(17).ToString() txtQuantity2.Text = rdrRepairOrder.GetSqlByte(18).ToString() txtSubTotal2.Text = rdrRepairOrder.GetSqlDecimal(19).ToString() txtPartName3.Text = rdrRepairOrder.GetString(20) txtUnitPrice3.Text = rdrRepairOrder.GetSqlDecimal(21).ToString() txtQuantity3.Text = rdrRepairOrder.GetSqlByte(22).ToString() txtSubTotal3.Text = rdrRepairOrder.GetSqlDecimal(23).ToString() txtPartName4.Text = rdrRepairOrder.GetString(24) txtUnitPrice4.Text = rdrRepairOrder.GetSqlDecimal(25).ToString() txtQuantity4.Text = rdrRepairOrder.GetSqlByte(26).ToString() txtSubTotal4.Text = rdrRepairOrder.GetSqlDecimal(27).ToString() txtPartName5.Text = rdrRepairOrder.GetString(28) txtUnitPrice5.Text = rdrRepairOrder.GetSqlDecimal(29).ToString() txtQuantity5.Text = rdrRepairOrder.GetSqlByte(30).ToString() txtSubTotal5.Text = rdrRepairOrder.GetSqlDecimal(31).ToString() txtJobPerformed1.Text = rdrRepairOrder.GetString(32) txtJobPrice1.Text = rdrRepairOrder.GetSqlDecimal(33).ToString() txtJobPerformed2.Text = rdrRepairOrder.GetString(34) txtJobPrice2.Text = rdrRepairOrder.GetSqlDecimal(35).ToString() txtJobPerformed3.Text = rdrRepairOrder.GetString(36) txtJobPrice3.Text = rdrRepairOrder.GetSqlDecimal(37).ToString() txtJobPerformed4.Text = rdrRepairOrder.GetString(38) txtJobPrice4.Text = rdrRepairOrder.GetSqlDecimal(39).ToString() txtJobPerformed5.Text = rdrRepairOrder.GetString(40) txtJobPrice5.Text = rdrRepairOrder.GetSqlDecimal(41).ToString() txtTotalParts.Text = rdrRepairOrder.GetSqlDecimal(42).ToString() txtTotalLabor.Text = rdrRepairOrder.GetSqlDecimal(43).ToString() txtTaxRate.Text = rdrRepairOrder.GetSqlDecimal(44).ToString() txtTaxAmount.Text = rdrRepairOrder.GetSqlDecimal(45).ToString() txtTotalOrder.Text = rdrRepairOrder.GetSqlDecimal(46).ToString() txtRecommendations.Text = rdrRepairOrder.GetString(47) End While rdrRepairOrder.Close() conDatabase.Close() End Sub |
|
||
Home | Copyright © 2005-2016, FunctionX | |
|