05-12-18, 11:44 PM
لدي قيمه من حساس مربوط في الحاسبة واذا كانت هذه القيمة اكبر من صفر اقوم بخزن هذة القيمة في الجدول الاول واذا كانت هذه القيمة =صفر اقوم بحساب معدل القيم المخزونة في الجدول الاول وخزنها في جدول ثاني وتصفير الجدول الاول ...علما انها في حدث timer والكود الذي كتبته ادناه وهو لا يعمل بشكل جيد حيث يقوم بتكرار القيم في الجدول الثاني
If s1.IsOpen = False Then
s1.PortName = "COM4"
s1.BaudRate = 9600
s1.Open()
Dim reading As String
reading = s1.ReadLine()
'lb_case.ForeColor = Color.Green
'lb_case.Text = "connected"
txt_read.Text = Convert.ToString(reading)
If txt_read.Text <> "" Then
If Val(txt_read.Text) > 0.0 Then
''''''بداية تشغيل المولدة وحساب القراءات
con.Open()
Comm = New SqlClient.SqlCommand("select * FROM first_tab", con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_used")
Dim dr1 As DataRow
dr1 = ds.Tables("ds_used").NewRow
dr1.Item("reading") = Val(txt_read.Text)
dr1.Item("read_date") = Now()
ds.Tables("ds_used").Rows.Add(dr1)
comBild.GetupdateCommand()
SQLAdap.update(ds, "ds_used")
con.Close()
End If
If Val(txt_read.Text) = 0.0 Then
''''فحص جدول القراءات
Comm = New SqlClient.SqlCommand("select * FROM first_tab", con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_used1")
If ds.Tables("ds_used1").Rows.Count > 0 Then
'حساب معدل الوقود الصاعد
Comm = New SqlClient.SqlCommand("select AVG (reading) from first_tab ",
con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_avup")
Dim av_fuel_up As Double
av_fuel_up = ds.Tables("ds_avup").Rows(0).Item(0)
'حساب تاريخ التشغيل
Comm = New SqlClient.SqlCommand("select read_date from first_tab where
id=1", con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_read_date")
Dim work_date As Date
work_date = ds.Tables("ds_read_date").Rows(0).Item(0)
'حساب مدة التشغيل
Comm = New SqlClient.SqlCommand("select DATEDIFF(s,(select read_date
from first_tab where id=1),(select read_date from first_tab where id=(select MAX(id)from
first_tab)))", con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_working_period")
Dim working_period As Integer
working_period = ds.Tables("ds_working_period").Rows(0).Item(0)
'اضافة البيانات الى جدول التشغيل
Comm = New SqlClient.SqlCommand("select * FROM second_table", con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_working")
Dim dr2 As DataRow
dr2 = ds.Tables("ds_working").NewRow
dr2.Item("working_date") = work_date
dr2.Item("read_av") = av_fuel_up
dr2.Item("period") = working_period
ds.Tables("ds_working").Rows.Add(dr2)
comBild.GetupdateCommand()
SQLAdap.update(ds, "ds_working")
con.Close()
'تصفير جدول الاستخدام
Dim cmd As New SqlClient.SqlCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "truncate table first_tab"
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
End If
Exit Sub
Else
Exit Sub
End If
Else
s1.Close()
End If
End Sub
-------------------
If s1.IsOpen = False Then
s1.PortName = "COM4"
s1.BaudRate = 9600
s1.Open()
Dim reading As String
reading = s1.ReadLine()
'lb_case.ForeColor = Color.Green
'lb_case.Text = "connected"
txt_read.Text = Convert.ToString(reading)
If txt_read.Text <> "" Then
If Val(txt_read.Text) > 0.0 Then
''''''بداية تشغيل المولدة وحساب القراءات
con.Open()
Comm = New SqlClient.SqlCommand("select * FROM first_tab", con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_used")
Dim dr1 As DataRow
dr1 = ds.Tables("ds_used").NewRow
dr1.Item("reading") = Val(txt_read.Text)
dr1.Item("read_date") = Now()
ds.Tables("ds_used").Rows.Add(dr1)
comBild.GetupdateCommand()
SQLAdap.update(ds, "ds_used")
con.Close()
End If
If Val(txt_read.Text) = 0.0 Then
''''فحص جدول القراءات
Comm = New SqlClient.SqlCommand("select * FROM first_tab", con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_used1")
If ds.Tables("ds_used1").Rows.Count > 0 Then
'حساب معدل الوقود الصاعد
Comm = New SqlClient.SqlCommand("select AVG (reading) from first_tab ",
con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_avup")
Dim av_fuel_up As Double
av_fuel_up = ds.Tables("ds_avup").Rows(0).Item(0)
'حساب تاريخ التشغيل
Comm = New SqlClient.SqlCommand("select read_date from first_tab where
id=1", con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_read_date")
Dim work_date As Date
work_date = ds.Tables("ds_read_date").Rows(0).Item(0)
'حساب مدة التشغيل
Comm = New SqlClient.SqlCommand("select DATEDIFF(s,(select read_date
from first_tab where id=1),(select read_date from first_tab where id=(select MAX(id)from
first_tab)))", con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_working_period")
Dim working_period As Integer
working_period = ds.Tables("ds_working_period").Rows(0).Item(0)
'اضافة البيانات الى جدول التشغيل
Comm = New SqlClient.SqlCommand("select * FROM second_table", con)
SQLAdap.selectCommand = Comm
comBild = New SqlClient.SqlCommandBuilder(SQLAdap)
SQLAdap.Fill(ds, "ds_working")
Dim dr2 As DataRow
dr2 = ds.Tables("ds_working").NewRow
dr2.Item("working_date") = work_date
dr2.Item("read_av") = av_fuel_up
dr2.Item("period") = working_period
ds.Tables("ds_working").Rows.Add(dr2)
comBild.GetupdateCommand()
SQLAdap.update(ds, "ds_working")
con.Close()
'تصفير جدول الاستخدام
Dim cmd As New SqlClient.SqlCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "truncate table first_tab"
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
End If
Exit Sub
Else
Exit Sub
End If
Else
s1.Close()
End If
End Sub
-------------------