- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 vb.net 中开发一项功能,自动将 mysql 数据库中的项目标记为过期、少于 7 天、少于一个月、少于 2 个月、少于 3 个月.
根据右上角标签中的文本,我知道我使用的循环是正确的。但是,它仅更新数据库中的一行。这是 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`
最佳答案
使用一个简单的存储过程从数据库获取数据
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 条件来处理空异常
关于mysql - Foreach 循环仅更新数据库的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39941377/
这是一个假设性问题。如果我有来自 3 个单独的 sql db 查询的 3 个数组,这些查询都与另一个数组相关。例如…… //db schools id | school_name classes id
在我的应用程序中,我使用 scrape(string url) 方法从网页中抓取链接。可以说它每次都返回我 10 个 url。 我想从每个抓取的 url 中抓取 10 个链接。 长话短说: (第 1
我的java7代码: final Map result = new HashMap<>(); final Set> classes = getClasses(co.glue()); for (fina
是否可以在 SwiftUI 中设置变量,例如在这样的 ForEach 中: struct ContentView: View { var test: Int var body: som
在 D、int、uint 中使用 foreach 时,循环索引的首选类型是什么?或者只是通过省略类型自动实现? 最佳答案 一般来说,索引应该是size_t。与长度相同。如果您尝试使用 int 或 ui
根据 http://dlang.org/statement.html 的“Foreach 限制”部分以下代码 int[] a; int[] b; foreach (int i; a) { a
在什么情况下我们应该在 JDK 8 中使用旧的 foreach 循环遍历新的 collection.forEach() 还是最好的做法是转换 every foreach 循环?是否存在任何重要的性能差
获得类似东西的惯用方法是什么? ((fn [coll] (function-body)) [:a :b :c :d]) -> [[:a :b][:a :c][:a :d][:b :c][:b :d][
我正在创建一个基于 who is it? 的 Java 应用程序。现在我正在制作一种方法,在回答问题时我需要其他卡片。 我有两个列表: 列表是一个 ImageView 列表,其中我有卡片必须代表的 2
我希望有人能在我发疯之前帮助我。 我有 3 张 table : Table A SELECT companypk, companyname, logo, msscope FROM global_com
我正在尝试将多个字符串添加到 C# 中的 MailAddress。 如果我使用ForEach,我的代码会是这样 foreach (var item in GetPeopleList()
我没有太多的 C# 经验,所以如果有人能指出正确的方向,我将不胜感激。我有一个引用对象变量的 foreach 循环。我希望在主循环中创建另一个 foreach 循环,将当前变量与对象数组中的其余变量进
下面的代码每 60 秒删除文件夹“Images”中的文件,它可以工作,但是当文件夹为空时它会显示:警告:为 foreach() 提供的参数无效如果没有文件,如何解决这个问题,说“文件夹为空而不是那个警
我需要在两种不同的模式下运行,因此“if”(第二个稍后构建一个大的 csv) 下面对于单个实例运行正常,但在第二个 (*) 的加载时间上失败,因为在前 7k 行中的每一行上运行。 我想避免可怕的事情
我们可以使用以下两种方法实现类数组对象的迭代: let arrayLike = document.getElementsByClassName('dummy'); [].forEach.call(ar
我有这个代码 ... 它说: Attribute value invalid for tag forEach according to TLD 最佳答案 forEach标签不支持 valu
我在 SwiftUI 中有一个像这样的 ForEach: ForEach(entries) { (e: MyType) in NavigationLinkItem(entry: e) } 现在我
我无法在一个 Foreach 或 Foreach-Object 循环中使用多个命令 我的情况是—— 我有很多文本文件,大约 100 个。 所以他们被阅读 Get-ChildItem $FilePath
我必须从 json 文件(实际上是 2 个 json 文件)执行 ForEach,因此我执行 2 forEach,代码是 table { font-family: arial, sans-
我对编程很陌生,当我执行 forEach 函数时,我的应用程序返回错误。我的controller.js中有以下代码 $scope.ajaxRequest = A.Game.get({action: '
我是一名优秀的程序员,十分优秀!