''' <summary>
''' เปิด/ปิด การเชื่อมต่อฐานข้อมูล MySQL อัตโนมัติ
''' </summary>
''' <remarks>Call open_connection()</remarks>
Public Sub open_connection()
Try
If ConnectionDB.State = ConnectionState.Open Then ConnectionDB.Close()
ConnectionDB.Open()
Catch ex As Exception
Throw New Exception("การเชื่อมต่อฐานข้อมูลใช้งานไม่ได้ เกิดข้อผิดพลาด : " & ex.Message)
End Try
End Sub
Public Dr As SqlDataReader
Public cmd As SqlCommand
Code (VB.NET)
sql = "select * from Tbl_Meter_Values where prajum_month_year = '" & combo_month_and_year.Text & "'"
cmd = New SqlCommand(sql, ConnectionDB)
Call open_connection()
Dr = cmd.ExecuteReader
if dr.HasRows then
While Dr.Read
Dim arr(16) As String
arr(0) = listview_detail_room.Items.Count + 1
arr(1) = Dr.Item("room_name")
sql = "select cust_nickname, cust_name from Tbl_Customer where room_name = '" & arr(1) & "'"
Dim dts2 As DataTable = cmd_excuteDataTable()
arr(2) = dts2.Rows(0)("cust_nickname")
arr(3) = dts2.Rows(0)("cust_name")
arr(4) = txt_price.Text
arr(5) = Dr.Item("meter_water_old")
arr(6) = Dr.Item("meter_water_new")
arr(7) = arr(6) - arr(5)
arr(8) = arr(7) * Val(txt_water.Text)
arr(9) = Dr.Item("meter_electric_old")
arr(10) = Dr.Item("meter_electric_new")
arr(11) = arr(10) - arr(9)
arr(12) = arr(11) * Val(txt_electric.Text)
arr(13) = Val(txt_net.Text)
arr(14) = arr(4) + +arr(8) + +arr(12) + +arr(13)
sql = "select room_status from Tbl_Rooms where room_name = '" & arr(1) & "'"
Dim dts3 As DataTable = cmd_excuteDataTable()
arr(15) = dts3.Rows(0)("room_status") '
Dim itm As New ListViewItem(arr)
listview_detail_room.Items.Add(itm)
End While
Else
MessageBox.Show("ยังไม่มีข้อมูลค่าเช่าห้องในเดือนนี้", "ข้อมูลเดือน " & combo_month_and_year.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
end if
Else
sql = "select * from Tbl_Meter_Values where prajum_month_year = '" & combo_month_and_year.Text & "'"
Dim dts As DataTable = cmd_excuteDataTable()
Dim arr(16) As String
if dts.rows.count > 0 then
for i as int = 0 to dts.rows.count - 1
arr(0) = listview_detail_room.Items.Count + 1
arr(1) = dts.Rows(i)("room_name")
'...........
arr(xxx) = dts.Rows(i)("xxxxxx") '
Dim itm As New ListViewItem(arr)
listview_detail_room.Items.Add(itm)
next
end if
End If