vb.net - Vb.net NO 值给一个或多个给定参数
问题描述
Dim cmd As OleDbCommand = New OleDbCommand(Sql, con)
Dim strSql As String = "Select EmpName,Count(EmpName) from tblPO where OrderType='" &
"B2B" & "' and POExpireDate < @LogDate Group By EmpName"
Dim tstDate As DateTime = DateTime.Now
Dim dateAsString As String = tstDate.ToString("dd/MM/yy")
cmd.Parameters.AddWithValue("@LogDate", CType(dateAsString, String))
Dim dtb As New DataTable
Using dad As New OleDbDataAdapter(strSql, con)
dad.Fill(dtb)
End Using
con.Close()
我在 VB.NET 中工作
没有为一个或多个给定参数指定值
填充数据表时出现错误......为什么......我该如何解决这个问题。请帮忙
解决方案
您的问题是您正在传递 strSql 和与数据适配器的连接,而不是包含参数的命令。改为传递命令
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Using blocks ensure that your database objects are
'Closed And Disposed even if there Is an error.
Dim dtb As New DataTable
Using con As New OleDbConnection("Your connection string")
Dim strSql As String = "Select EmpName,Count(EmpName) from tblPO where OrderType = 'B2B' and POExpireDate < @LogDate Group By EmpName;"
Using cmd As OleDbCommand = New OleDbCommand(strSql, con)
cmd.Parameters.Add("@LogDate", OleDbType.Date).Value = DateTime.Now
'On the next line pass the command, no need to pass connection
'because it has already been passed to the constructor of the command
Using dad As New OleDbDataAdapter(cmd)
dad.Fill(dtb)
End Using
End Using
End Using
End Sub
推荐阅读
- javascript - Node.js - 使用 PKG 创建可执行文件
- c# - 需要帮助来根据 C# 中的子项计数对我的数据集执行批处理
- docker - Windows10 主页上的 Docker - 在 docker 容器内连接到 docker 引擎
- mysql - 从库存数据库中高效查询产品变体(避免 EAV)
- javascript - 确保用户在更新后获得最新版本网站的正确方法
- python - 根据 pandas 的每日回报计算每月回报(加元)
- html - 更改字体真棒圆圈图标的背景颜色
- apache-nifi - 来自 SplitRecord 处理器 Nifi 的运行记录数
- node.js - 如何使用 google puppeteer 保存完整的网页?
- c# - Windows 窗体 Web 浏览器控制缩放级别