提问人:roy 提问时间:8/18/2023 更新时间:8/18/2023 访问量:49
在 dapper 中,没有为数据库 MS-ACCESS 中的一个或多个必需参数提供值 VB.NET
No value given for one or more required parameters in dapper with database MS-ACCESS in VB.NET
问:
如果我在 button1 上没有 dapper 运行,则没有错误并成功更新,而如果我在 button2 上运行 dapper,则会出现错误“没有为一个或多个必需参数提供值”。
函数方法代码有问题,即“UpdateSacknumber”有问题吗?请指导我。
谢谢
Public Class Form1
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Private uService As New Stockservice()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cmd As OleDbCommand
Dim con As OleDbConnection
Try
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\dapperdemo.accdb")
cmd = New OleDbCommand("UPDATE Stocks SET SACK_NUMBER=? WHERE CODEPRODUCT=? and BARCODE=? and COLORCODE=? and SIZE=?", con)
con.Open()
cmd.Parameters.AddWithValue("?", TextBox1.Text)
cmd.Parameters.AddWithValue("?", TextBox2.Text)
cmd.Parameters.AddWithValue("?", TextBox3.Text)
cmd.Parameters.AddWithValue("?", TextBox4.Text)
cmd.Parameters.AddWithValue("?", TextBox5.Text)
cmd.ExecuteNonQuery()
MsgBox("Record Updated")
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
uService.UpdateSacknumber(New Stocks() With {
.Sack_Number = TextBox1.Text,
.CodeProduct = TextBox2.Text,
.Barcode = TextBox3.Text,
.Colorcode = TextBox4.Text,
.Size = TextBox5.Text
})
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Public Class Stockservice
Private ReadOnly _conn As OleDbConnection
Private _connectionString As String = GetOledbConnectionString()
Public Sub New()
_conn = New OleDbConnection(_connectionString)
End Sub
Public Function GetOledbConnectionString() As String
Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\dapperdemo.accdb;Persist Security Info=False;"
End Function
Public Sub UpdateSacknumber(ByVal Obj As Stocks)
Dim sql = $"UPDATE `Stocks` Set `SACK_NUMBER`= {Obj.Sack_Number} WHERE `CODEPRODUCT`='{Obj.CodeProduct}' and `BARCODE`='{Obj.Barcode}' and `COLORCODE`='{Obj.Colorcode}' and `SIZE`='{Obj.Size}';"
Using _conn = New OleDbConnection(GetOledbConnectionString())
_conn.Execute(sql)
End Using
End Sub
End Class
Public Class Stocks
Public Property CodeProduct() As String
Public Property Barcode() As String
Public Property Colorcode() As String
Public Property Size() As String
Public Property Qty_Stock() As Integer
Public Property Qty_Stockin() As Integer
Public Property Qty_Stockout() As Integer
Public Property Sack_Number() As String
End Class
错误:未为一个或多个必需参数提供值
答:
0赞
Palle Due
8/18/2023
#1
您不会在 Button1 中使用 Dappper 运行。您正在创建一个命令,这不是 Dapper 的方式。我认为你应该做这样的事情(请原谅我的vb):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim con As OleDbConnection
Dim query As String
Try
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\dapperdemo.accdb")
query = "UPDATE Stocks SET SACK_NUMBER=? WHERE CODEPRODUCT=? and BARCODE=? and COLORCODE=? and SIZE=?"
con.Open()
Dim params As DynamicParameters = New DynamicParameters()
params.Add("?", TextBox1.Text)
params.Add("?", TextBox2.Text)
params.Add("?", TextBox3.Text)
params.Add("?", TextBox4.Text)
params.Add("?", TextBox5.Text)
con.ExecuteNonQuery(query, params)
MsgBox("Record Updated")
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
DynamicParameters 是一种用于保存参数的 Dapper 类型。请务必按照参数在查询中指定的顺序添加参数。 调用,因此您还应该指定参数的类型,如@AndrewMorton建议的链接中所示。params.Add
AddWithValue
评论
0赞
roy
8/18/2023
You don't run with Dappper in Button1
感谢您的回答,但是 button1 中的那个是命令,而 button2 中的那个是 dapper。如果在 dapper 中,是否需要确定参数的类型。 如果你不介意你的答案,它可以根据我在 button2 中的代码更新吗,所以使用我创建的函数方法,然后你可以用 DynamicParameters 添加它。
评论
Sack_Number() As String
{Obj.Sack_Number}