我正在尝试在vb.net中开发一个功能,以便在过期时自动标记mysql数据库中的项目,少于7天,不到一个月,少于2个月,少于3个月< / em>的。
我知道我使用的循环是正确的,基于右上方标签中的文字。但是,它只更新数据库中的一行。这是我得到的image。 所以问题是:我如何编码以便更新数据库中的所有行?
Public Sub insertStatus()
Dim tDate As Date
Dim eDate As Date
Dim reader As MySqlDataReader
Dim query, query2, updateQ1, updateQ2, updateQ3, updateQ4, updateQ5, updateQ6 As String
Dim adpt As New MySqlDataAdapter
Dim dSet As New DataSet
Dim dRow As DataRow
connection = New MySqlConnection
connection.ConnectionString = "server=localhost;userid=luce;password=root;database=chemicalinventoryrecord;Allow Zero Datetime = True"
Try
connection.Open()
tDate = Date.Today
query = "select * from chemicalinventory"
query2 = "select expiryDate from chemicalinventory"
command = New MySqlCommand(query, connection)
command1 = New MySqlCommand(query2, connection)
adpt.SelectCommand = command
adpt.Fill(dSet, "chemicalinventory")
reader = command1.ExecuteReader
While reader.Read
For Each dRow In dSet.Tables(0).Rows
eDate = CType(reader.GetMySqlDateTime("expiryDate"), Date)
If (eDate - tDate).Days < 7 And (eDate - tDate).Days >= 1 Then
lblTest2.Text = "Less than 7 days"
updateQ1 = "update chemicalinventoryrecord.chemicalinventory set remark ='" & lblTest2.Text & "' where DateDiff(expiryDate, CURDATE())<7 and DateDiff(expiryDate, CURDATE())>=1 "
command = New MySqlCommand(updateQ1, connection)
Exit For
ElseIf (tDate - eDate).Days >= 0 Then
lblTest1.Text = "Expired"
updateQ2 = "update chemicalinventoryrecord.chemicalinventory set remark = '" & lblTest1.Text & "' where DateDiff(CURDATE(), expiryDate)>=0"
command2 = New MySqlCommand(updateQ2, connection)
Exit For
ElseIf (eDate - tDate).Days < 30 And (eDate - tDate).Days >= 7 Then
updateQ3 = "update chemicalinventoryrecord.chemicalinventory set remark = 'Expiring in less than 1 month' where DateDiff(expiryDate, CURDATE())<30 and DateDiff(expiryDate, CURDATE())>=7"
command4 = New MySqlCommand(updateQ3, connection)
Exit For
ElseIf (eDate - tDate).Days < 60 And (eDate - tDate).Days >= 30 Then
lblTest3.Text = "Less than 2 month"
updateQ4 = "update chemicalinventoryrecord.chemicalinventory set remark = '" & lblTest3.Text & "'where DateDiff(expiryDate, CURDATE())<60 and DateDiff(expiryDate, CURDATE())>=30"
command5 = New MySqlCommand(updateQ4, connection)
Exit For
ElseIf (eDate - tDate).Days < 90 And (eDate - tDate).Days >= 60 Then
updateQ5 = "update chemicalinventoryrecord.chemicalinventory set remark = 'Expiring in less than 3 months' where DateDiff(expiryDate, CURDATE())<90 and DateDiff(expiryDate, CURDATE())>=60"
command6 = New MySqlCommand(updateQ5, connection)
Exit For
Else
updateQ6 = "update chemicalinventoryrecord.chemicalinventory set remark =' ' where DateDiff(expiryDate, CURDATE())>90"
command3 = New MySqlCommand(updateQ6, connection)
Exit For
End If
Next
End While
reader.Close()
command.ExecuteNonQuery()
connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
connection.Dispose()
End Try
End Sub`
答案 0 :(得分:1)
使用简单的存储过程从数据库中获取数据
Vb方面:
Dim conn As New MySql.Data.MySqlClient.MySqlConnection
Dim cmd As MySqlCommand = conn.CreateCommand()
Dim myConnectionString As String
Dim ds As New DataSet
myConnectionString = "server=127.0.0.1;" _
& "uid=Monica;" _
& "pwd=12345;" _
& "database=Monica;"
Try
conn.ConnectionString = myConnectionString
conn.Open() '' open connection
If conn.State = ConnectionState.Open Then '' check connection state
cmd.CommandText = "selectDataWithRemark"
cmd.CommandType = CommandType.StoredProcedure
Dim adap As MySqlDataAdapter = New MySqlDataAdapter(cmd)
adap.Fill(ds)
End If
Try
If Not ds Is Nothing Then '' check ds before assigning to datagridview
If ds.Tables.Count > 0 Then
If ds.Tables(0).Rows.Count > 0 Then
dgvChemicalTable.DataSource = ds.Tables(0)
End If
End If
End If
Catch ex As Exception
MessageBox.Show("DataGridView Error : " & ex.Message)
End Try
conn.Close() '' close connection
Catch ex As MySql.Data.MySqlClient.MySqlException
MessageBox.Show("Database Coonection Error : " & ex.Message)
End Try
MySql方面:
CREATE PROCEDURE `Monica`.`selectDataWithRemark` ()
BEGIN
SELECT Serial as Serial_Number,Name as Chemical_Name,
MolecularFormula as Formula, DateReceived as Received_Date,
DateExpired as Expiry_Date,
DATEDIFF(DateExpired, DateReceived) as Days_left,
CASE
WHEN DATEDIFF(DateExpired, DateReceived) <= 0 THEN 'Expired'
WHEN DATEDIFF(DateExpired, DateReceived) < 7 THEN 'Expiring in less Less than 7 days'
WHEN DATEDIFF(DateExpired, DateReceived) < 30 THEN 'Expiring in less than 1 month'
WHEN DATEDIFF(DateExpired, DateReceived) < 60 THEN 'Expiring in less than 2 months'
WHEN DATEDIFF(DateExpired, DateReceived) < 90 THEN 'Expiring in less than 3 months'
ELSE 'Plenty of days left'
END AS Remark
FROM test;
END $$
PC @Tomalak使用了您的代码,但添加了else条件来处理null异常
答案 1 :(得分:0)
创建一个列,其内容不是基于另一个列的值以外的其他因素,这是一个非常糟糕的主意。别这么做。
你想要的是这样的数据库查询:
SELECT
col1, col2, etc, expiryDate,
CASE
WHEN DateDiff(CURDATE(), expiryDate) < 0 THEN 'Expired'
WHEN DateDiff(CURDATE(), expiryDate) < 7 THEN 'Expiring in less Less than 7 days'
WHEN DateDiff(CURDATE(), expiryDate) < 30 THEN 'Expiring in less than 1 month'
WHEN DateDiff(CURDATE(), expiryDate) < 60 THEN 'Expiring in less than 2 months'
WHEN DateDiff(CURDATE(), expiryDate) < 90 THEN 'Expiring in less than 3 months'
END AS remark
FROM
chemicalinventoryrecord.chemicalinventory
您可以将其存储为数据库视图,也可以直接在应用程序中使用它。
这样remark
列始终是正确的,您不会需要每天运行的任何外部代码来保持最新状态。
从MySQL 5.7.5开始,如果您不想使用单独的视图,您甚至可以将CASE
表达式添加为原始表格中的calculated column。