vb.net - 如何在数据网格视图中添加到期日期
问题描述
我有这个代码:
Dim readers As MySqlDataReader
Dim command As New MySqlCommand
Try
con.Open()
Dim query As String
query = "Select product_code,drug_name,quantity,expiration_date from medicine where expiration_date<=@exp"
command = New MySqlCommand(query, con)
command.Parameters.Add("@exp", MySqlDbType.DateTime).Value = DateTime.Now
readers = command.ExecuteReader
Dim count As Integer
count = 0
While readers.Read
count = count + 1
End While
con.Close()
If count = 0 Then
MsgBox("no expiration")
Else
DataGridView1.Rows.Add(readers)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
解决方案
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim dt As New DataTable
Using con As New MySqlConnection("your connection string")
Using command As New MySqlCommand("Select product_code,drug_name,quantity,expiration_date from medicine where expiration_date<=@exp", con)
command.Parameters.Add("@exp", MySqlDbType.DateTime).Value = DateTime.Now
con.Open()
dt.Load(command.ExecuteReader)
End Using
End Using
If dt.Rows.Count < 1 Then
MsgBox("no expiration")
Else
DataGridView1.DataSource = dt
End If
End Sub
如果您的目标是显示查询的返回值,这应该可以工作。Using...End Using
即使出现错误,这些块也可确保您的数据库对象已关闭和处置。
推荐阅读
- prestashop - 在 prestashop 1.7 中输入错误的 url 时如何直接重定向到 404 错误页面?
- c# - 从 Web 客户端调用 Microsoft Bot Framework 机器人,无需在 Azure 上托管
- animation - Aframe 通过 event-component & proxy-component 触发 Element 的动画
- python - python的概率论:均值,标准差
- csv - Flutter:如何从简单的电子表格中读取数据?
- excel - 如何在vba中将文本框动态定位在另一个下方
- php - 用其他字段插入外键
- bash - 如何在 bash 脚本中同时运行 2 个循环
- c# - 如何在 C# 中释放我的应用程序内存?
- ios - 如何在 tvml (tvOS) 中使用 UISplitViewController